diff --git a/README.md b/README.md index b701003..0d0032d 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,12 @@ product = Product.find(1) product.update_index ``` +Partial matches (needs better name) + +```ruby +Item.search "fresh honey", partial: true # matches organic honey +``` + ## Elasticsearch Gotchas ### Inconsistent Scores @@ -181,6 +187,7 @@ bundle ## TODO +- Autocomplete - Focus on results format (load: true?) - Test helpers - everyone should test their own search - Built-in synonyms from WordNet diff --git a/lib/searchkick/search.rb b/lib/searchkick/search.rb index d59d43a..aff6851 100644 --- a/lib/searchkick/search.rb +++ b/lib/searchkick/search.rb @@ -7,22 +7,23 @@ module Searchkick def search(term, options = {}) fields = options[:fields] || ["_all"] + operator = options[:partial] ? "or" : "and" tire.search do query do boolean do must do dis_max do query do - match fields, term, boost: 10, operator: "and", analyzer: "searchkick_search" + match fields, term, boost: 10, operator: operator, analyzer: "searchkick_search" end query do - match fields, term, boost: 10, operator: "and", analyzer: "searchkick_search2" + match fields, term, boost: 10, operator: operator, analyzer: "searchkick_search2" end query do - match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: "and", analyzer: "searchkick_search" + match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: operator, analyzer: "searchkick_search" end query do - match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: "and", analyzer: "searchkick_search2" + match fields, term, use_dis_max: false, fuzziness: 0.7, max_expansions: 1, prefix_length: 1, operator: operator, analyzer: "searchkick_search2" end end end diff --git a/test/searchkick_test.rb b/test/searchkick_test.rb index dc26b4b..f1c89c5 100644 --- a/test/searchkick_test.rb +++ b/test/searchkick_test.rb @@ -257,6 +257,12 @@ class TestSearchkick < Minitest::Unit::TestCase assert_equal 1, Product.search("Product", facets: {store_id: {where: {in_stock: true, color: "blue"}}}).facets["store_id"]["terms"].size end + def test_partial + store_names ["Honey"] + assert_search "fresh honey", [] + assert_search "fresh honey", ["Honey"], partial: true + end + protected def store(documents) -- libgit2 0.21.0