Commit 9f218051fe35d99d2ff301e0831ad7294d2483f0
1 parent
97d73154
Exists in
master
and in
21 other branches
Added support for Elasticsearch 1.2
Showing
5 changed files
with
28 additions
and
16 deletions
Show diff stats
CHANGELOG.md
README.md
@@ -45,12 +45,6 @@ Add this line to your application’s Gemfile: | @@ -45,12 +45,6 @@ Add this line to your application’s Gemfile: | ||
45 | gem 'searchkick' | 45 | gem 'searchkick' |
46 | ``` | 46 | ``` |
47 | 47 | ||
48 | -For Elasticsearch 1.2, use: | ||
49 | - | ||
50 | -```ruby | ||
51 | -gem 'searchkick', github: 'ankane/searchkick', branch: 'elasticsearch-1.2' | ||
52 | -``` | ||
53 | - | ||
54 | For Elasticsearch 0.90, use version `0.6.3` and [this readme](https://github.com/ankane/searchkick/blob/v0.6.3/README.md). | 48 | For Elasticsearch 0.90, use version `0.6.3` and [this readme](https://github.com/ankane/searchkick/blob/v0.6.3/README.md). |
55 | 49 | ||
56 | Add searchkick to models you want to search. | 50 | Add searchkick to models you want to search. |
lib/searchkick.rb
@@ -25,6 +25,10 @@ module Searchkick | @@ -25,6 +25,10 @@ module Searchkick | ||
25 | @client = client | 25 | @client = client |
26 | end | 26 | end |
27 | 27 | ||
28 | + def self.server_version | ||
29 | + @server_version ||= client.info["version"]["number"] | ||
30 | + end | ||
31 | + | ||
28 | @callbacks = true | 32 | @callbacks = true |
29 | 33 | ||
30 | def self.enable_callbacks | 34 | def self.enable_callbacks |
lib/searchkick/query.rb
@@ -15,6 +15,8 @@ module Searchkick | @@ -15,6 +15,8 @@ module Searchkick | ||
15 | @term = term | 15 | @term = term |
16 | @options = options | 16 | @options = options |
17 | 17 | ||
18 | + below12 = Gem::Version.new(Searchkick.server_version) < Gem::Version.new("1.2") | ||
19 | + | ||
18 | boost_fields = {} | 20 | boost_fields = {} |
19 | fields = | 21 | fields = |
20 | if options[:fields] | 22 | if options[:fields] |
@@ -127,6 +129,13 @@ module Searchkick | @@ -127,6 +129,13 @@ module Searchkick | ||
127 | 129 | ||
128 | if conversions_field and options[:conversions] != false | 130 | if conversions_field and options[:conversions] != false |
129 | # wrap payload in a bool query | 131 | # wrap payload in a bool query |
132 | + script_score = | ||
133 | + if below12 | ||
134 | + {script_score: {script: "doc['count'].value"}} | ||
135 | + else | ||
136 | + {field_value_factor: {field: "count"}} | ||
137 | + end | ||
138 | + | ||
130 | payload = { | 139 | payload = { |
131 | bool: { | 140 | bool: { |
132 | must: payload, | 141 | must: payload, |
@@ -141,11 +150,8 @@ module Searchkick | @@ -141,11 +150,8 @@ module Searchkick | ||
141 | match: { | 150 | match: { |
142 | query: term | 151 | query: term |
143 | } | 152 | } |
144 | - }, | ||
145 | - script_score: { | ||
146 | - script: "doc['count'].value" | ||
147 | } | 153 | } |
148 | - } | 154 | + }.merge(script_score) |
149 | } | 155 | } |
150 | } | 156 | } |
151 | } | 157 | } |
@@ -165,16 +171,20 @@ module Searchkick | @@ -165,16 +171,20 @@ module Searchkick | ||
165 | end | 171 | end |
166 | 172 | ||
167 | boost_by.each do |field, value| | 173 | boost_by.each do |field, value| |
174 | + script_score = | ||
175 | + if below12 | ||
176 | + {script_score: {script: "#{value[:factor].to_f} * log(doc['#{field}'].value + 2.718281828)"}} | ||
177 | + else | ||
178 | + {field_value_factor: {field: field, factor: value[:factor].to_f, modifier: "ln2p"}} | ||
179 | + end | ||
180 | + | ||
168 | custom_filters << { | 181 | custom_filters << { |
169 | filter: { | 182 | filter: { |
170 | exists: { | 183 | exists: { |
171 | field: field | 184 | field: field |
172 | } | 185 | } |
173 | - }, | ||
174 | - script_score: { | ||
175 | - script: "#{value[:factor].to_f} * log(doc['#{field}'].value + 2.718281828)" | ||
176 | } | 186 | } |
177 | - } | 187 | + }.merge(script_score) |
178 | end | 188 | end |
179 | 189 | ||
180 | boost_where = options[:boost_where] || {} | 190 | boost_where = options[:boost_where] || {} |
@@ -370,7 +380,7 @@ module Searchkick | @@ -370,7 +380,7 @@ module Searchkick | ||
370 | e.message.include?("No query registered for [function_score]]") | 380 | e.message.include?("No query registered for [function_score]]") |
371 | ) | 381 | ) |
372 | 382 | ||
373 | - raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 0.90.4 or greater" | 383 | + raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 1.0 or greater" |
374 | elsif status_code == 400 | 384 | elsif status_code == 400 |
375 | if e.message.include?("[multi_match] analyzer [searchkick_search] not found") | 385 | if e.message.include?("[multi_match] analyzer [searchkick_search] not found") |
376 | raise InvalidQueryError, "Bad mapping - run #{searchkick_klass.name}.reindex" | 386 | raise InvalidQueryError, "Bad mapping - run #{searchkick_klass.name}.reindex" |
searchkick.gemspec
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec| | @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| | ||
19 | spec.require_paths = ["lib"] | 19 | spec.require_paths = ["lib"] |
20 | 20 | ||
21 | spec.add_dependency "activemodel" | 21 | spec.add_dependency "activemodel" |
22 | - spec.add_dependency "elasticsearch", "~> 0.4.11" | 22 | + spec.add_dependency "elasticsearch", ">= 1" |
23 | spec.add_dependency "hashie" | 23 | spec.add_dependency "hashie" |
24 | 24 | ||
25 | spec.add_development_dependency "bundler", "~> 1.3" | 25 | spec.add_development_dependency "bundler", "~> 1.3" |