Commit 6b57aa61244c1fcb34e0651bfff63d99a5818a1c

Authored by Andrew Kane
1 parent 82fd2292

Added partial option

README.md
... ... @@ -159,6 +159,12 @@ product = Product.find(1)
159 159 product.update_index
160 160 ```
161 161  
  162 +Partial matches (needs better name)
  163 +
  164 +```ruby
  165 +Item.search "fresh honey", partial: true # matches organic honey
  166 +```
  167 +
162 168 ## Elasticsearch Gotchas
163 169  
164 170 ### Inconsistent Scores
... ... @@ -181,6 +187,7 @@ bundle
181 187  
182 188 ## TODO
183 189  
  190 +- Autocomplete
184 191 - Focus on results format (load: true?)
185 192 - Test helpers - everyone should test their own search
186 193 - Built-in synonyms from WordNet
... ...
lib/searchkick/search.rb
... ... @@ -7,22 +7,23 @@ module Searchkick
7 7  
8 8 def search(term, options = {})
9 9 fields = options[:fields] || ["_all"]
  10 + operator = options[:partial] ? "or" : "and"
10 11 tire.search do
11 12 query do
12 13 boolean do
13 14 must do
14 15 dis_max do
15 16 query do
16   - match fields, term, boost: 10, operator: "and", analyzer: "searchkick_search"
  17 + match fields, term, boost: 10, operator: operator, analyzer: "searchkick_search"
17 18 end
18 19 query do
19   - match fields, term, boost: 10, operator: "and", analyzer: "searchkick_search2"
  20 + match fields, term, boost: 10, operator: operator, analyzer: "searchkick_search2"
20 21 end
21 22 query do
22   - match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: "and", analyzer: "searchkick_search"
  23 + match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: operator, analyzer: "searchkick_search"
23 24 end
24 25 query do
25   - match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: "and", analyzer: "searchkick_search2"
  26 + match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: operator, analyzer: "searchkick_search2"
26 27 end
27 28 end
28 29 end
... ...
test/searchkick_test.rb
... ... @@ -257,6 +257,12 @@ class TestSearchkick < Minitest::Unit::TestCase
257 257 assert_equal 1, Product.search("Product", facets: {store_id: {where: {in_stock: true, color: "blue"}}}).facets["store_id"]["terms"].size
258 258 end
259 259  
  260 + def test_partial
  261 + store_names ["Honey"]
  262 + assert_search "fresh honey", []
  263 + assert_search "fresh honey", ["Honey"], partial: true
  264 + end
  265 +
260 266 protected
261 267  
262 268 def store(documents)
... ...