简体   繁体   中英

Tire does not find partial word (search on 2 fields)

What I want to do:

  • I have a model 'Item' with 2 fields I want elasticsearch to search on: title and description.
  • I want the search to find partial words, ex: bicycl should match against bicycle, bicycles, etc...

Current situation:

  • The search only shows perfect matches

Here is what I have right now in my Item model:

 include Tire::Model::Search
 include Tire::Model::Callbacks

 class << self
   def search_index
     Tire.index(Item.index_name)
   end
 end

 settings :analysis => {
                    :filter => {
                      :my_ngram  => {
                        "type"     => "nGram",
                        "max_gram" => 10,
                        "min_gram" => 3 }
                    },
                    :analyzer => {
                      :my_analyzer => {
                         "type"         => "custom",
                         "tokenizer"    => "standard",
                         "filter"       => ["my_ngram"]
                       }
                    }
                  } do
   mapping do
     indexes :title,       boost: 10, analyzer: 'my_analyzer'
     indexes :description, boost: 5,  analyzer: 'my_analyzer'
   end
end

 def self.search(query_string)
   tire.search(load: true) do
     if query_string.present?
       query do
         string query_string, default_operator: "AND"
       end
     end
   end
 end

When you do...

string query_string, default_operator: "AND"

... you're actually searching the magic _all field.

I'm pretty sure that you need to specifically search for the field analyzed with the ngram filter for this to work.

should { string "title:#{query_string}", default_operator: "OR" }
should { string "description:#{query_string}", default_operator: "OR" }

for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM