searchkick 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d97f04a7f7678a643933cfd0045ec2e6043ebf02
4
- data.tar.gz: c817fb9f49a0b6526beb3a2a2d5266c451258cfe
3
+ metadata.gz: dfbf6c27b23821e31c3de0338259ee8c2adf5130
4
+ data.tar.gz: f3b47e7a6d98f990f53d95410c88e2a2fc791b36
5
5
  SHA512:
6
- metadata.gz: c19441dd8e684d5c1d0b4613dce000535dbd527ff35fce233bcdaba960037c9ec2853c1539ce8b038f3880a30c0a2be7717363f87be93b0789440718509c8c8c
7
- data.tar.gz: 247238fb70ade1d5ce6eeff5ddd3c0bf590bec9bb52e8e746fe12db58ffb7bf5bdde5f6385bc4167b8dd67bc151724b215253b15755e1cb75f30ec07fac1c025
6
+ metadata.gz: 1b0d6d2d11bde9c7a3f316a6ac6dc4886f639b8a02ddc6b49e280ae0535b38f7f43e53e1507528587cdc578b0b1ad165816aa23b89d4e21043a4e480790f181c
7
+ data.tar.gz: 39b962185cf9b507be764e1175e8bbe2d27822eed2005ad02923849577237c348d80b77e3ee8b3128676f7191ac43f23b2044075051751b532260a32597a877b
@@ -14,5 +14,6 @@ notifications:
14
14
  on_failure: change
15
15
  gemfile:
16
16
  - Gemfile
17
+ - gemfiles/mongoid2.gemfile
17
18
  - gemfiles/mongoid3.gemfile
18
19
  - gemfiles/mongoid4.gemfile
@@ -1,3 +1,9 @@
1
+ ## 0.8.1
2
+
3
+ - Added `search_method_name` option
4
+ - Fixed `order` for array of hashes
5
+ - Added support for Mongoid 2
6
+
1
7
  ## 0.8.0
2
8
 
3
9
  - Added support for Elasticsearch 1.2
data/README.md CHANGED
@@ -651,6 +651,8 @@ For the best performance, add [Patron](https://github.com/toland/patron) to your
651
651
  gem 'patron'
652
652
  ```
653
653
 
654
+ Searchkick will automatically use it.
655
+
654
656
  **Note:** Patron is not available for Windows.
655
657
 
656
658
  ### Automatic Failover
@@ -757,6 +759,12 @@ Product.enable_search_callbacks # or use Searchkick.enable_callbacks for all mod
757
759
  Product.reindex
758
760
  ```
759
761
 
762
+ Change the search method name in `config/initializers/searchkick.rb`
763
+
764
+ ```ruby
765
+ Searchkick.search_method_name = :lookup
766
+ ```
767
+
760
768
  Eager load associations
761
769
 
762
770
  ```ruby
@@ -822,7 +830,7 @@ class Product < ActiveRecord::Base
822
830
  end
823
831
  ```
824
832
 
825
- Reindex all models (Rails only)
833
+ Reindex all models - Rails only
826
834
 
827
835
  ```sh
828
836
  rake searchkick:reindex:all
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in searchkick.gemspec
4
+ gemspec path: "../"
5
+
6
+ gem "mongoid", "~> 2"
7
+ gem "bson_ext"
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in searchkick.gemspec
4
4
  gemspec path: "../"
5
5
 
6
- gem "mongoid", "4.0.0.beta1"
6
+ gem "mongoid", "~> 4.0.0"
@@ -6,7 +6,6 @@ require "searchkick/index"
6
6
  require "searchkick/reindex"
7
7
  require "searchkick/results"
8
8
  require "searchkick/query"
9
- require "searchkick/search"
10
9
  require "searchkick/similar"
11
10
  require "searchkick/model"
12
11
  require "searchkick/tasks"
@@ -17,6 +16,11 @@ module Searchkick
17
16
  class UnsupportedVersionError < StandardError; end
18
17
  class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
19
18
 
19
+ class << self
20
+ attr_accessor :search_method_name
21
+ end
22
+ self.search_method_name = :search
23
+
20
24
  def self.client
21
25
  @client ||= Elasticsearch::Client.new(url: ENV["ELASTICSEARCH_URL"])
22
26
  end
@@ -19,7 +19,14 @@ module Searchkick
19
19
  Searchkick::Index.new(index)
20
20
  end
21
21
 
22
- extend Searchkick::Search
22
+ define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}|
23
+ query = Searchkick::Query.new(self, term, options)
24
+ if options[:execute] == false
25
+ query
26
+ else
27
+ query.execute
28
+ end
29
+ end
23
30
  extend Searchkick::Reindex
24
31
  include Searchkick::Similar
25
32
 
@@ -228,7 +228,8 @@ module Searchkick
228
228
  # order
229
229
  if options[:order]
230
230
  order = options[:order].is_a?(Enumerable) ? options[:order] : {options[:order] => :asc}
231
- payload[:sort] = Hash[ order.map{|k, v| [k.to_s == "id" ? :_id : k, v] } ]
231
+ # TODO id transformation for arrays
232
+ payload[:sort] = order.is_a?(Array) ? order : Hash[ order.map{|k, v| [k.to_s == "id" ? :_id : k, v] } ]
232
233
  end
233
234
 
234
235
  # filters
@@ -27,9 +27,14 @@ module Searchkick
27
27
  records = records.includes(options[:includes])
28
28
  end
29
29
  results[type] =
30
- if records.respond_to?(:primary_key)
30
+ if records.respond_to?(:primary_key) and records.primary_key
31
+ # ActiveRecord
31
32
  records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a
33
+ elsif records.respond_to?(:all) and records.all.respond_to?(:for_ids)
34
+ # Mongoid 2
35
+ records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
32
36
  else
37
+ # Mongoid 3+
33
38
  records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
34
39
  end
35
40
  end
@@ -12,7 +12,7 @@ module Searchkick
12
12
  options[:where][:_id][:not] = id.to_s
13
13
  options[:limit] ||= 10
14
14
  options[:similar] = true
15
- self.class.search(like_text, options)
15
+ self.class.send(Searchkick.search_method_name, like_text, options)
16
16
  end
17
17
 
18
18
  end
@@ -1,3 +1,3 @@
1
1
  module Searchkick
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -5,8 +5,8 @@ class TestFacets < Minitest::Unit::TestCase
5
5
  def setup
6
6
  super
7
7
  store [
8
- {name: "Product Show", latitude: 37.7833, longitude: 12.4167, store_id: 1, in_stock: true, color: "blue", price: 21},
9
- {name: "Product Hide", latitude: 29.4167, longitude: -98.5000, store_id: 2, in_stock: false, color: "green", price: 25},
8
+ {name: "Product Show", latitude: 37.7833, longitude: 12.4167, store_id: 1, in_stock: true, color: "blue", price: 21, created_at: 2.days.ago},
9
+ {name: "Product Hide", latitude: 29.4167, longitude: -98.5000, store_id: 2, in_stock: false, color: "green", price: 25, created_at: 2.days.from_now},
10
10
  {name: "Product B", latitude: 43.9333, longitude: -122.4667, store_id: 2, in_stock: false, color: "red", price: 5},
11
11
  {name: "Foo", latitude: 43.9333, longitude: 12.4667, store_id: 3, in_stock: false, color: "yellow", price: 15}
12
12
  ]
@@ -39,6 +39,15 @@ class TestFacets < Minitest::Unit::TestCase
39
39
  assert_equal 2, facet["ranges"][2]["count"]
40
40
  end
41
41
 
42
+ def test_ranges_dates
43
+ ranges = [{to: 1.day.ago}, {from: 1.day.ago, to: 1.day.from_now}, {from: 1.day.from_now}]
44
+ facet = Product.search("Product", facets: {created_at: {ranges: ranges}}).facets["created_at"]
45
+
46
+ assert_equal 1, facet["ranges"][0]["count"]
47
+ assert_equal 1, facet["ranges"][1]["count"]
48
+ assert_equal 1, facet["ranges"][2]["count"]
49
+ end
50
+
42
51
  def test_where_no_smart_facets
43
52
  assert_equal ({2 => 2}), store_facet(where: {color: "red"}, facets: {store_id: {where: {in_stock: false}}})
44
53
  end
@@ -205,6 +205,11 @@ class TestSql < Minitest::Unit::TestCase
205
205
  assert_order "product", [], order: {not_mapped: {ignore_unmapped: true}}, conversions: false
206
206
  end
207
207
 
208
+ def test_order_array
209
+ store [{name: "San Francisco", latitude: 37.7833, longitude: -122.4167}]
210
+ assert_order "francisco", ["San Francisco"], order: [{_geo_distance: {location: "0,0"}}], conversions: false
211
+ end
212
+
208
213
  def test_partial
209
214
  store_names ["Honey"]
210
215
  assert_search "fresh honey", []
@@ -281,7 +286,21 @@ class TestSql < Minitest::Unit::TestCase
281
286
 
282
287
  def test_select
283
288
  store [{name: "Product A", store_id: 1}]
284
- assert_equal %w[id name store_id], Product.search("product", load: false, select: [:name, :store_id]).first.keys.reject{|k| k.start_with?("_") }.sort
289
+ result = Product.search("product", load: false, select: [:name, :store_id]).first
290
+ assert_equal %w[id name store_id], result.keys.reject{|k| k.start_with?("_") }.sort
291
+ assert_equal ["Product A"], result.name # this is not great
292
+ end
293
+
294
+ def test_select_array
295
+ store [{name: "Product A", user_ids: [1, 2]}]
296
+ result = Product.search("product", load: false, select: [:user_ids]).first
297
+ assert_equal [1, 2], result.user_ids
298
+ end
299
+
300
+ def test_nested_object
301
+ aisle = {"id" => 1, "name" => "Frozen"}
302
+ store [{name: "Product A", aisle: aisle}]
303
+ assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash
285
304
  end
286
305
 
287
306
  # TODO see if Mongoid is loaded
@@ -10,8 +10,28 @@ File.delete("elasticsearch.log") if File.exists?("elasticsearch.log")
10
10
  Searchkick.client.transport.logger = Logger.new("elasticsearch.log")
11
11
 
12
12
  if defined?(Mongoid)
13
+
14
+ def mongoid2?
15
+ Mongoid::VERSION.starts_with?("2.")
16
+ end
17
+
18
+ if mongoid2?
19
+ # enable comparison of BSON::ObjectIds
20
+ module BSON
21
+ class ObjectId
22
+ def <=>(other)
23
+ self.data <=> other.data
24
+ end
25
+ end
26
+ end
27
+ end
28
+
13
29
  Mongoid.configure do |config|
14
- config.connect_to "searchkick_test"
30
+ if mongoid2?
31
+ config.master = Mongo::Connection.new.db("searchkick_test")
32
+ else
33
+ config.connect_to "searchkick_test"
34
+ end
15
35
  end
16
36
 
17
37
  class Product
@@ -121,10 +141,16 @@ class Product
121
141
  word_middle: [:name],
122
142
  word_end: [:name]
123
143
 
124
- attr_accessor :conversions, :user_ids
144
+ attr_accessor :conversions, :user_ids, :aisle
125
145
 
126
146
  def search_data
127
- serializable_hash.except("id").merge conversions: conversions, user_ids: user_ids, location: [latitude, longitude], multiple_locations: [[latitude, longitude], [0, 0]]
147
+ serializable_hash.except("id").merge(
148
+ conversions: conversions,
149
+ user_ids: user_ids,
150
+ location: [latitude, longitude],
151
+ multiple_locations: [[latitude, longitude], [0, 0]],
152
+ aisle: aisle
153
+ )
128
154
  end
129
155
 
130
156
  def should_index?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-13 00:00:00.000000000 Z
11
+ date: 2014-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -108,6 +108,7 @@ files:
108
108
  - LICENSE.txt
109
109
  - README.md
110
110
  - Rakefile
111
+ - gemfiles/mongoid2.gemfile
111
112
  - gemfiles/mongoid3.gemfile
112
113
  - gemfiles/mongoid4.gemfile
113
114
  - lib/searchkick.rb
@@ -179,3 +180,4 @@ test_files:
179
180
  - test/suggest_test.rb
180
181
  - test/synonyms_test.rb
181
182
  - test/test_helper.rb
183
+ has_rdoc: