Commit 9f218051fe35d99d2ff301e0831ad7294d2483f0

Authored by Andrew Kane
1 parent 97d73154

Added support for Elasticsearch 1.2

CHANGELOG.md
  1 +## 0.8.0
  2 +
  3 +- Added support for Elasticsearch 1.2
  4 +
1 5 ## 0.7.9
2 6  
3 7 - Added `tokens` method
... ...
README.md
... ... @@ -45,12 +45,6 @@ Add this line to your application’s Gemfile:
45 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 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 50 Add searchkick to models you want to search.
... ...
lib/searchkick.rb
... ... @@ -25,6 +25,10 @@ module Searchkick
25 25 @client = client
26 26 end
27 27  
  28 + def self.server_version
  29 + @server_version ||= client.info["version"]["number"]
  30 + end
  31 +
28 32 @callbacks = true
29 33  
30 34 def self.enable_callbacks
... ...
lib/searchkick/query.rb
... ... @@ -15,6 +15,8 @@ module Searchkick
15 15 @term = term
16 16 @options = options
17 17  
  18 + below12 = Gem::Version.new(Searchkick.server_version) < Gem::Version.new("1.2")
  19 +
18 20 boost_fields = {}
19 21 fields =
20 22 if options[:fields]
... ... @@ -127,6 +129,13 @@ module Searchkick
127 129  
128 130 if conversions_field and options[:conversions] != false
129 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 139 payload = {
131 140 bool: {
132 141 must: payload,
... ... @@ -141,11 +150,8 @@ module Searchkick
141 150 match: {
142 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 171 end
166 172  
167 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 181 custom_filters << {
169 182 filter: {
170 183 exists: {
171 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 188 end
179 189  
180 190 boost_where = options[:boost_where] || {}
... ... @@ -370,7 +380,7 @@ module Searchkick
370 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 384 elsif status_code == 400
375 385 if e.message.include?("[multi_match] analyzer [searchkick_search] not found")
376 386 raise InvalidQueryError, "Bad mapping - run #{searchkick_klass.name}.reindex"
... ...
searchkick.gemspec
... ... @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19 19 spec.require_paths = ["lib"]
20 20  
21 21 spec.add_dependency "activemodel"
22   - spec.add_dependency "elasticsearch", "~> 0.4.11"
  22 + spec.add_dependency "elasticsearch", ">= 1"
23 23 spec.add_dependency "hashie"
24 24  
25 25 spec.add_development_dependency "bundler", "~> 1.3"
... ...