searchkick 0.7.4 → 0.7.5

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: 4b8625d5f69db773beb2bda77ee3b78fdd54398e
4
- data.tar.gz: d796061050099ca9a13ec01e61511b4e66e2a760
3
+ metadata.gz: f2b3dc0e591516c6ddd76101c1053211ae7f9fe9
4
+ data.tar.gz: f56695cbbfcc4e92b6d751add7c00ec6bbdb82ed
5
5
  SHA512:
6
- metadata.gz: 8e9774402df7d1e076cbfac13f577fe1ec51464405d1e157c2f9078ddc8d757ab64ce7d4a0d96427897f62e34d58753a7045c6e1731a18c9c3f2ca009010c8d6
7
- data.tar.gz: ecf0c24cc9908419d018d31118ff3f744534f7d426936e4daa8ddc90ee30759e65e452087c697bfa60d87aa330e2c39692f902e2c260c67c159b0e374df86643
6
+ metadata.gz: 1a4ca76c448e90b4d838aa1e0076b01355866b837a046618222aaa7e65b9142b3370c8f93fc622be57e248a5ed8fd4941155fa915dda118c1781893266c832c6
7
+ data.tar.gz: 2d7f62a4329cf8005f57ae60968f3e2aea626cff87e5073a8fa141250ec135424ba8bb2ccff46ddf8033bdf07d05ecad3dec59fceeb5d67d044911e7ffa9f561
@@ -1,3 +1,9 @@
1
+ ## 0.7.5
2
+
3
+ - Do not throw errors when index becomes out of sync with database
4
+ - Added custom exception types
5
+ - Fixed `offset` and `offset_value`
6
+
1
7
  ## 0.7.4
2
8
 
3
9
  - Fixed reindex with inheritance
data/README.md CHANGED
@@ -353,7 +353,7 @@ Typically, you want to use a JavaScript library like [typeahead.js](http://twitt
353
353
 
354
354
  #### Here’s how to make it work with Rails
355
355
 
356
- First, add a controller action.
356
+ First, add a route and controller action.
357
357
 
358
358
  ```ruby
359
359
  # app/controllers/cities_controller.rb
@@ -13,6 +13,9 @@ require "searchkick/tasks"
13
13
  require "searchkick/logging" if defined?(Rails)
14
14
 
15
15
  module Searchkick
16
+ class MissingIndexError < StandardError; end
17
+ class UnsupportedVersionError < StandardError; end
18
+ class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
16
19
 
17
20
  def self.client
18
21
  @client ||= Elasticsearch::Client.new(url: ENV["ELASTICSEARCH_URL"])
@@ -26,7 +26,7 @@ module Searchkick
26
26
  client.index(
27
27
  index: name,
28
28
  type: document_type(record),
29
- id: record.id,
29
+ id: search_id(record),
30
30
  body: search_data(record)
31
31
  )
32
32
  end
@@ -35,7 +35,7 @@ module Searchkick
35
35
  client.delete(
36
36
  index: name,
37
37
  type: document_type(record),
38
- id: record.id
38
+ id: search_id(record)
39
39
  )
40
40
  end
41
41
 
@@ -44,7 +44,7 @@ module Searchkick
44
44
  client.bulk(
45
45
  index: name,
46
46
  type: type,
47
- body: batch.map{|r| data = search_data(r); {index: {_id: data["_id"] || data["id"] || r.id, data: data}} }
47
+ body: batch.map{|r| data = search_data(r); {index: {_id: search_id(r), data: data}} }
48
48
  )
49
49
  end
50
50
  end
@@ -71,16 +71,15 @@ module Searchkick
71
71
  klass_document_type(record.class)
72
72
  end
73
73
 
74
+ def search_id(record)
75
+ record.id.is_a?(Numeric) ? record.id : record.id.to_s
76
+ end
77
+
74
78
  def search_data(record)
75
79
  source = record.search_data
76
80
 
77
81
  # stringify fields
78
- source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
79
-
80
- # Mongoid 4 hack
81
- if defined?(BSON::ObjectId) and source["_id"].is_a?(BSON::ObjectId)
82
- source["_id"] = source["_id"].to_s
83
- end
82
+ source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}.except("id", "_id")
84
83
 
85
84
  options = record.class.searchkick_options
86
85
 
@@ -328,7 +328,7 @@ module Searchkick
328
328
  rescue => e # TODO rescue type
329
329
  status_code = e.message[1..3].to_i
330
330
  if status_code == 404
331
- raise "Index missing - run #{searchkick_klass.name}.reindex"
331
+ raise MissingIndexError, "Index missing - run #{searchkick_klass.name}.reindex"
332
332
  elsif status_code == 500 and (
333
333
  e.message.include?("IllegalArgumentException[minimumSimilarity >= 1]") or
334
334
  e.message.include?("No query registered for [multi_match]") or
@@ -336,7 +336,13 @@ module Searchkick
336
336
  e.message.include?("No query registered for [function_score]]")
337
337
  )
338
338
 
339
- raise "This version of Searchkick requires Elasticsearch 0.90.4 or greater"
339
+ raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 0.90.4 or greater"
340
+ elsif status_code == 400
341
+ if e.message.include?("[multi_match] analyzer [searchkick_search] not found")
342
+ raise InvalidQueryError, "Bad mapping - run #{searchkick_klass.name}.reindex"
343
+ else
344
+ raise InvalidQueryError, e.message
345
+ end
340
346
  else
341
347
  raise e
342
348
  end
@@ -127,11 +127,6 @@ module Searchkick
127
127
  tokenizer: "standard",
128
128
  filter: ["lowercase", "asciifolding", "searchkick_suggest_shingle"]
129
129
  },
130
- searchkick_suggest_index: {
131
- type: "custom",
132
- tokenizer: "standard",
133
- filter: ["lowercase", "asciifolding", "searchkick_suggest_shingle"]
134
- },
135
130
  searchkick_text_start_index: {
136
131
  type: "custom",
137
132
  tokenizer: "keyword",
@@ -24,13 +24,18 @@ module Searchkick
24
24
  if options[:includes]
25
25
  records = records.includes(options[:includes])
26
26
  end
27
- results[type] = records.find(grouped_hits.map{|hit| hit["_id"] })
27
+ results[type] =
28
+ if records.respond_to?(:primary_key)
29
+ records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a
30
+ else
31
+ records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
32
+ end
28
33
  end
29
34
 
30
35
  # sort
31
36
  hits.map do |hit|
32
37
  results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s }
33
- end
38
+ end.compact
34
39
  else
35
40
  hits.map do |hit|
36
41
  result = hit.except("_source").merge(hit["_source"])
@@ -88,9 +93,10 @@ module Searchkick
88
93
  def total_pages
89
94
  (total_count / per_page.to_f).ceil
90
95
  end
96
+ alias_method :num_pages, :total_pages
91
97
 
92
98
  def offset_value
93
- current_page * per_page
99
+ (current_page - 1) * per_page
94
100
  end
95
101
  alias_method :offset, :offset_value
96
102
 
@@ -1,3 +1,3 @@
1
1
  module Searchkick
2
- VERSION = "0.7.4"
2
+ VERSION = "0.7.5"
3
3
  end
@@ -35,6 +35,35 @@ class TestIndex < Minitest::Unit::TestCase
35
35
  assert_equal ["Dollar Tree"], Store.search(query: {match: {name: "Dollar Tree"}}).map(&:name)
36
36
  end
37
37
 
38
+ def test_record_not_found
39
+ store_names ["Product A", "Product B"]
40
+ Product.where(name: "Product A").delete_all
41
+ assert_search "product", ["Product B"]
42
+ end
43
+
44
+ def test_bad_mapping
45
+ Product.searchkick_index.delete
46
+ store_names ["Product A"]
47
+ assert_raises(Searchkick::InvalidQueryError){ Product.search "test" }
48
+ ensure
49
+ Product.reindex
50
+ end
51
+
52
+ def test_missing_index
53
+ assert_raises(Searchkick::MissingIndexError){ Product.search "test", index_name: "not_found" }
54
+ end
55
+
56
+ def test_unsupported_version
57
+ raises_exception = lambda { |s| raise Elasticsearch::Transport::Transport::Error.new("[500] No query registered for [multi_match]") }
58
+ Searchkick.client.stub :search, raises_exception do
59
+ assert_raises(Searchkick::UnsupportedVersionError){ Product.search("test") }
60
+ end
61
+ end
62
+
63
+ def test_invalid_query
64
+ assert_raises(Searchkick::InvalidQueryError){ Product.search(query: {}) }
65
+ end
66
+
38
67
  if defined?(ActiveRecord)
39
68
 
40
69
  def test_transaction
@@ -30,8 +30,8 @@ class TestSql < Minitest::Unit::TestCase
30
30
  assert_equal 5, products.total_count
31
31
  assert_equal 5, products.total_entries
32
32
  assert_equal 2, products.limit_value
33
- assert_equal 4, products.offset_value
34
- assert_equal 4, products.offset
33
+ assert_equal 2, products.offset_value
34
+ assert_equal 2, products.offset
35
35
  assert !products.first_page?
36
36
  assert !products.last_page?
37
37
  assert !products.empty?
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.7.4
4
+ version: 0.7.5
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-05-07 00:00:00.000000000 Z
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel