searchkick 0.8.2 → 0.8.3

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: 647df0215d082ec2eed37cabd3f1949aafc6c058
4
- data.tar.gz: b3be78d876de64f7323fe31fbbe7829f7885ebc4
3
+ metadata.gz: 8b8b951d29bd106ae56b6ca7d2738574430f76ae
4
+ data.tar.gz: a56ba366f77bfb6f0378e8f5711704bec31ef2cf
5
5
  SHA512:
6
- metadata.gz: 9326c663870b3dcf60384515ec29873a9b7c3ce6325922e3250ee0c7f75ebd933859d1a481275d035f2a8e00ac2b5c55ee0d342dfd55e698b575ca947f781f93
7
- data.tar.gz: e6aaaa85a4ee9ccea8dba7bbd3077aef5164be1a5381ea60e1716881035050505575e1190494f9e53c1bef92b89852050b587a3fe3d84b4b35019eb9c8364ba8
6
+ metadata.gz: 7f532ecc2e7bb89ad4a705400c89151d7b5ab114efc7c8d4fa9771578de22a3e3ed5361863b6cea9cf56ea9ab6cf4b3379899168af619242844035197c1b294a
7
+ data.tar.gz: 37c8d654358714fc2a5389190940fdf5f9714b60ded559364ec28ea6a639ed69570f80495e004e2d17c3354e8aada69b7f5e9cf062176f595d4e626024921b1c
@@ -1,10 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
3
+ - 2.1
5
4
  services:
6
5
  - elasticsearch
7
6
  - mongodb
7
+ before_install:
8
+ - wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.3.2.deb && sudo dpkg -i elasticsearch-1.3.2.deb && sudo service elasticsearch restart
8
9
  script: bundle exec rake test
9
10
  before_script:
10
11
  - psql -c 'create database searchkick_test;' -U postgres
@@ -14,6 +15,9 @@ notifications:
14
15
  on_failure: change
15
16
  gemfile:
16
17
  - Gemfile
18
+ - gemfiles/activerecord40.gemfile
19
+ - gemfiles/activerecord32.gemfile
20
+ - gemfiles/activerecord31.gemfile
17
21
  - gemfiles/mongoid2.gemfile
18
22
  - gemfiles/mongoid3.gemfile
19
23
  - gemfiles/mongoid4.gemfile
@@ -1,3 +1,9 @@
1
+ ## 0.8.3
2
+
3
+ - Added support for ActiveJob
4
+ - Added `timeout` setting
5
+ - Fixed import with no records
6
+
1
7
  ## 0.8.2
2
8
 
3
9
  - Added `async` to `callbacks` option
data/Gemfile CHANGED
@@ -4,6 +4,5 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem "sqlite3"
7
- gem "activerecord", ">= 3.1"
8
- # gem "activerecord", "~> 3.2.0"
9
- # gem "activerecord", "~> 3.1.0"
7
+ gem "activerecord", "~> 4.1.0"
8
+ gem "activejob_backport"
data/README.md CHANGED
@@ -343,7 +343,7 @@ There are three strategies for keeping the index synced with your database.
343
343
  end
344
344
  ```
345
345
 
346
- Supports [Delayed Job](https://github.com/collectiveidea/delayed_job) only at the moment
346
+ And [install Active Job](https://github.com/ankane/activejob_backport) for Rails 4.1 and below
347
347
 
348
348
  3. Manual
349
349
 
@@ -539,6 +539,14 @@ Product.search "*", facets: {store_id: {stats: true}}
539
539
 
540
540
  ### Highlight
541
541
 
542
+ Specify which fields to index with highlighting.
543
+
544
+ ```ruby
545
+ class Product < ActiveRecord::Base
546
+ searchkick highlight: [:name]
547
+ end
548
+ ```
549
+
542
550
  Highlight the search query in the results.
543
551
 
544
552
  ```ruby
@@ -801,6 +809,12 @@ Product.enable_search_callbacks # or use Searchkick.enable_callbacks for all mod
801
809
  Product.reindex
802
810
  ```
803
811
 
812
+ Change timeout
813
+
814
+ ```ruby
815
+ Searchkick.timeout = 5 # defaults to 10
816
+ ```
817
+
804
818
  Change the search method name in `config/initializers/searchkick.rb`
805
819
 
806
820
  ```ruby
@@ -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 "sqlite3"
7
+ gem "activerecord", "~> 3.1.0"
@@ -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 "sqlite3"
7
+ gem "activerecord", "~> 3.2.0"
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in searchkick.gemspec
4
+ gemspec path: "../"
5
+
6
+ gem "sqlite3"
7
+ gem "activerecord", "~> 4.0.0"
8
+ gem "activejob_backport"
@@ -4,3 +4,4 @@ source 'https://rubygems.org'
4
4
  gemspec path: "../"
5
5
 
6
6
  gem "mongoid", "~> 4.0.0"
7
+ gem "activejob_backport"
@@ -12,6 +12,14 @@ require "searchkick/model"
12
12
  require "searchkick/tasks"
13
13
  require "searchkick/logging" if defined?(Rails)
14
14
 
15
+ # background jobs
16
+ begin
17
+ require "active_job"
18
+ rescue LoadError
19
+ # do nothing
20
+ end
21
+ require "searchkick/reindex_v2_job" if defined?(ActiveJob)
22
+
15
23
  module Searchkick
16
24
  class MissingIndexError < StandardError; end
17
25
  class UnsupportedVersionError < StandardError; end
@@ -21,13 +29,19 @@ module Searchkick
21
29
  attr_accessor :callbacks
22
30
  attr_accessor :search_method_name
23
31
  attr_accessor :wordnet_path
32
+ attr_accessor :timeout
24
33
  end
25
34
  self.callbacks = true
26
35
  self.search_method_name = :search
27
36
  self.wordnet_path = "/var/lib/wn_s.pl"
37
+ self.timeout = 10
28
38
 
29
39
  def self.client
30
- @client ||= Elasticsearch::Client.new(url: ENV["ELASTICSEARCH_URL"])
40
+ @client ||=
41
+ Elasticsearch::Client.new(
42
+ url: ENV["ELASTICSEARCH_URL"],
43
+ transport_options: {request: {timeout: timeout}}
44
+ )
31
45
  end
32
46
 
33
47
  def self.client=(client)
@@ -38,12 +38,14 @@ module Searchkick
38
38
  alias_method_chain :remove, :instrumentation
39
39
 
40
40
  def import_with_instrumentation(records)
41
- event = {
42
- name: "#{records.first.searchkick_klass.name} Import",
43
- count: records.size
44
- }
45
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
46
- import_without_instrumentation(records)
41
+ if records.any?
42
+ event = {
43
+ name: "#{records.first.searchkick_klass.name} Import",
44
+ count: records.size
45
+ }
46
+ ActiveSupport::Notifications.instrument("request.searchkick", event) do
47
+ import_without_instrumentation(records)
48
+ end
47
49
  end
48
50
  end
49
51
  alias_method_chain :import, :instrumentation
@@ -32,9 +32,11 @@ module Searchkick
32
32
  extend Searchkick::Reindex
33
33
  include Searchkick::Similar
34
34
 
35
- if callbacks == :async
36
- def reindex_async
37
- Delayed::Job.enqueue Searchkick::ReindexJob.new(self.class.name, id)
35
+ def reindex_async
36
+ if defined?(Searchkick::ReindexV2Job)
37
+ Searchkick::ReindexV2Job.perform_later(self.class.name, id.to_s)
38
+ else
39
+ Delayed::Job.enqueue Searchkick::ReindexJob.new(self.class.name, id.to_s)
38
40
  end
39
41
  end
40
42
 
@@ -17,7 +17,7 @@ module Searchkick
17
17
  # check if alias exists
18
18
  if Searchkick.client.indices.exists_alias(name: alias_name)
19
19
  # import before swap
20
- searchkick_import(index) unless skip_import
20
+ searchkick_import(index: index) unless skip_import
21
21
 
22
22
  # get existing indices to remove
23
23
  old_indices = Searchkick.client.indices.get_alias(name: alias_name).keys
@@ -29,7 +29,7 @@ module Searchkick
29
29
  Searchkick.client.indices.update_aliases body: {actions: [{add: {index: new_name, alias: alias_name}}]}
30
30
 
31
31
  # import after swap
32
- searchkick_import(index) unless skip_import
32
+ searchkick_import(index: index) unless skip_import
33
33
  end
34
34
 
35
35
  index.refresh
@@ -52,9 +52,8 @@ module Searchkick
52
52
  @descendents << klass unless @descendents.include?(klass)
53
53
  end
54
54
 
55
- private
56
-
57
- def searchkick_import(index)
55
+ def searchkick_import(options = {})
56
+ index = options[:index] || searchkick_index
58
57
  batch_size = searchkick_options[:batch_size] || 1000
59
58
 
60
59
  # use scope for import
@@ -261,7 +260,7 @@ module Searchkick
261
260
  end
262
261
 
263
262
  mapping_options = Hash[
264
- [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end]
263
+ [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight]
265
264
  .map{|type| [type, (options[type] || []).map(&:to_s)] }
266
265
  ]
267
266
 
@@ -276,12 +275,16 @@ module Searchkick
276
275
  }
277
276
  }
278
277
 
279
- mapping_options.each do |type, fields|
278
+ mapping_options.except(:highlight).each do |type, fields|
280
279
  if fields.include?(field)
281
280
  field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
282
281
  end
283
282
  end
284
283
 
284
+ if mapping_options[:highlight].include?(field)
285
+ field_mapping[:fields]["analyzed"][:term_vector] = "with_positions_offsets"
286
+ end
287
+
285
288
  mapping[field] = field_mapping
286
289
  end
287
290
 
@@ -0,0 +1,24 @@
1
+ module Searchkick
2
+ class ReindexV2Job < ActiveJob::Base
3
+ queue_as :searchkick
4
+
5
+ def perform(klass, id)
6
+ model = klass.constantize
7
+ record = model.find(id) rescue nil # TODO fix lazy coding
8
+ index = model.searchkick_index
9
+ if !record or !record.should_index?
10
+ # hacky
11
+ record ||= model.new
12
+ record.id = id
13
+ begin
14
+ index.remove record
15
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
16
+ # do nothing
17
+ end
18
+ else
19
+ index.store record
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -88,6 +88,10 @@ module Searchkick
88
88
  klass.model_name
89
89
  end
90
90
 
91
+ def entry_name
92
+ model_name.human.downcase
93
+ end
94
+
91
95
  def total_count
92
96
  response["hits"]["total"]
93
97
  end
@@ -1,3 +1,3 @@
1
1
  module Searchkick
2
- VERSION = "0.8.2"
2
+ VERSION = "0.8.3"
3
3
  end
@@ -19,6 +19,11 @@ class TestHighlight < Minitest::Test
19
19
  assert_equal "<em>Cinema</em> Orange", highlight[:color]
20
20
  end
21
21
 
22
+ def test_multiple_words
23
+ store_names ["Hello World Hello"]
24
+ assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name]
25
+ end
26
+
22
27
  def test_json
23
28
  store_names ["Two Door Cinema Club"]
24
29
  json = {
@@ -53,6 +53,8 @@ class TestIndex < Minitest::Test
53
53
  store_names ["Product A", "Product B"]
54
54
  Product.where(name: "Product A").delete_all
55
55
  assert_search "product", ["Product B"]
56
+ ensure
57
+ Product.reindex
56
58
  end
57
59
 
58
60
  def test_bad_mapping
@@ -15,7 +15,7 @@ class TestReindexJob < Minitest::Test
15
15
  product = Product.create!(name: "Boom")
16
16
  Product.searchkick_index.refresh
17
17
  assert_search "*", []
18
- Searchkick::ReindexJob.new("Product", product.id).perform
18
+ Searchkick::ReindexJob.new("Product", product.id.to_s).perform
19
19
  Product.searchkick_index.refresh
20
20
  assert_search "*", ["Boom"]
21
21
  end
@@ -25,7 +25,7 @@ class TestReindexJob < Minitest::Test
25
25
  Product.reindex
26
26
  assert_search "*", ["Boom"]
27
27
  product.destroy
28
- Searchkick::ReindexJob.new("Product", product.id).perform
28
+ Searchkick::ReindexJob.new("Product", product.id.to_s).perform
29
29
  Product.searchkick_index.refresh
30
30
  assert_search "*", []
31
31
  end
@@ -0,0 +1,34 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestReindexV2Job < Minitest::Test
4
+
5
+ def setup
6
+ skip if !defined?(ActiveJob)
7
+ super
8
+ Searchkick.disable_callbacks
9
+ end
10
+
11
+ def teardown
12
+ Searchkick.enable_callbacks
13
+ end
14
+
15
+ def test_create
16
+ product = Product.create!(name: "Boom")
17
+ Product.searchkick_index.refresh
18
+ assert_search "*", []
19
+ Searchkick::ReindexV2Job.perform_later("Product", product.id.to_s)
20
+ Product.searchkick_index.refresh
21
+ assert_search "*", ["Boom"]
22
+ end
23
+
24
+ def test_destroy
25
+ product = Product.create!(name: "Boom")
26
+ Product.reindex
27
+ assert_search "*", ["Boom"]
28
+ product.destroy
29
+ Searchkick::ReindexV2Job.perform_later("Product", product.id.to_s)
30
+ Product.searchkick_index.refresh
31
+ assert_search "*", []
32
+ end
33
+
34
+ end
@@ -22,6 +22,7 @@ class TestSql < Minitest::Test
22
22
  store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"]
23
23
  products = Product.search("product", order: {name: :asc}, page: 2, per_page: 2, padding: 1)
24
24
  assert_equal ["Product D", "Product E"], products.map(&:name)
25
+ assert_equal "product", products.entry_name
25
26
  assert_equal 2, products.current_page
26
27
  assert_equal 1, products.padding
27
28
  assert_equal 2, products.per_page
@@ -23,7 +23,8 @@ class TestSuggest < Minitest::Test
23
23
 
24
24
  def test_multiple_fields
25
25
  store [
26
- {name: "Shark", color: "Sharp"}
26
+ {name: "Shark", color: "Sharp"},
27
+ {name: "Shark", color: "Sharp"},
27
28
  ]
28
29
  assert_suggest_all "shar", ["shark", "sharp"]
29
30
  end
@@ -11,6 +11,10 @@ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)
11
11
  File.delete("elasticsearch.log") if File.exists?("elasticsearch.log")
12
12
  Searchkick.client.transport.logger = Logger.new("elasticsearch.log")
13
13
 
14
+ I18n.config.enforce_available_locales = true
15
+
16
+ ActiveJob::Base.logger = nil if defined?(ActiveJob)
17
+
14
18
  if defined?(Mongoid)
15
19
 
16
20
  def mongoid2?
@@ -141,7 +145,8 @@ class Product
141
145
  text_end: [:name],
142
146
  word_start: [:name],
143
147
  word_middle: [:name],
144
- word_end: [:name]
148
+ word_end: [:name],
149
+ highlight: [:name]
145
150
 
146
151
  attr_accessor :conversions, :user_ids, :aisle
147
152
 
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.2
4
+ version: 0.8.3
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-08-18 00:00:00.000000000 Z
11
+ date: 2014-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -108,6 +108,9 @@ files:
108
108
  - LICENSE.txt
109
109
  - README.md
110
110
  - Rakefile
111
+ - gemfiles/activerecord31.gemfile
112
+ - gemfiles/activerecord32.gemfile
113
+ - gemfiles/activerecord40.gemfile
111
114
  - gemfiles/mongoid2.gemfile
112
115
  - gemfiles/mongoid3.gemfile
113
116
  - gemfiles/mongoid4.gemfile
@@ -118,6 +121,7 @@ files:
118
121
  - lib/searchkick/query.rb
119
122
  - lib/searchkick/reindex.rb
120
123
  - lib/searchkick/reindex_job.rb
124
+ - lib/searchkick/reindex_v2_job.rb
121
125
  - lib/searchkick/results.rb
122
126
  - lib/searchkick/similar.rb
123
127
  - lib/searchkick/tasks.rb
@@ -133,6 +137,7 @@ files:
133
137
  - test/model_test.rb
134
138
  - test/query_test.rb
135
139
  - test/reindex_job_test.rb
140
+ - test/reindex_v2_job_test.rb
136
141
  - test/should_index_test.rb
137
142
  - test/similar_test.rb
138
143
  - test/sql_test.rb
@@ -176,6 +181,7 @@ test_files:
176
181
  - test/model_test.rb
177
182
  - test/query_test.rb
178
183
  - test/reindex_job_test.rb
184
+ - test/reindex_v2_job_test.rb
179
185
  - test/should_index_test.rb
180
186
  - test/similar_test.rb
181
187
  - test/sql_test.rb