Commit 6b57aa61244c1fcb34e0651bfff63d99a5818a1c
1 parent
82fd2292
Exists in
master
and in
21 other branches
Added partial option
Showing
3 changed files
with
18 additions
and
4 deletions
Show diff stats
README.md
@@ -159,6 +159,12 @@ product = Product.find(1) | @@ -159,6 +159,12 @@ product = Product.find(1) | ||
159 | product.update_index | 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 | ## Elasticsearch Gotchas | 168 | ## Elasticsearch Gotchas |
163 | 169 | ||
164 | ### Inconsistent Scores | 170 | ### Inconsistent Scores |
@@ -181,6 +187,7 @@ bundle | @@ -181,6 +187,7 @@ bundle | ||
181 | 187 | ||
182 | ## TODO | 188 | ## TODO |
183 | 189 | ||
190 | +- Autocomplete | ||
184 | - Focus on results format (load: true?) | 191 | - Focus on results format (load: true?) |
185 | - Test helpers - everyone should test their own search | 192 | - Test helpers - everyone should test their own search |
186 | - Built-in synonyms from WordNet | 193 | - Built-in synonyms from WordNet |
lib/searchkick/search.rb
@@ -7,22 +7,23 @@ module Searchkick | @@ -7,22 +7,23 @@ module Searchkick | ||
7 | 7 | ||
8 | def search(term, options = {}) | 8 | def search(term, options = {}) |
9 | fields = options[:fields] || ["_all"] | 9 | fields = options[:fields] || ["_all"] |
10 | + operator = options[:partial] ? "or" : "and" | ||
10 | tire.search do | 11 | tire.search do |
11 | query do | 12 | query do |
12 | boolean do | 13 | boolean do |
13 | must do | 14 | must do |
14 | dis_max do | 15 | dis_max do |
15 | query do | 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 | end | 18 | end |
18 | query do | 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 | end | 21 | end |
21 | query do | 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 | end | 24 | end |
24 | query do | 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 | end | 27 | end |
27 | end | 28 | end |
28 | end | 29 | end |
test/searchkick_test.rb
@@ -257,6 +257,12 @@ class TestSearchkick < Minitest::Unit::TestCase | @@ -257,6 +257,12 @@ class TestSearchkick < Minitest::Unit::TestCase | ||
257 | assert_equal 1, Product.search("Product", facets: {store_id: {where: {in_stock: true, color: "blue"}}}).facets["store_id"]["terms"].size | 257 | assert_equal 1, Product.search("Product", facets: {store_id: {where: {in_stock: true, color: "blue"}}}).facets["store_id"]["terms"].size |
258 | end | 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 | protected | 266 | protected |
261 | 267 | ||
262 | def store(documents) | 268 | def store(documents) |