简体   繁体   中英

Setting Elasticsearch analyzer for specific field using tire gem

I have a Resource object that has the following fields: , , and .

Using the tire gem I want to use a standard analyzer for and , but for the field I want to use the snowball analyzer. ,但是对于领域,我想使用雪球分析仪。

So In my resource.rb file I have the following code:

mapping do
  indexes :title, type: 'string', :index => :not_analyzed, :store => true
  indexes :description, type: 'string'
  indexes :body, type: 'string', :analyzer => 'snowball'
end

def self.search(params)
  search_query = params[:query]

  tire.search(page: params[:page], per_page: 15) do |s|
    s.query { string params[:query], default_operator: "AND"} if params[:query].present?
    # do i need another line here specifically for description?
    #s.query { string "body:#{params[:query]}", analyzer: 'snowball'} if params[:query].present?
  end
end

I set the analyzer in the mapping to snowball. And I verify that this works because if I run the following command:

curl -XGET localhost:9200/example/_search?q=body:cook

It will return cooking, cooked, etc.

However I want to be able to pass the following query

curl -XGET localhost:9200/example/_search?q=cooking

And I want elasticsearch to search for the word cooking in the and fields and search for the word cook in the . 字段字烹饪和搜索的词厨师

Is this possible? And because I'm new to this whole searching game, is this even a good idea? Or am I a guy just juggling cans of gasoline over a fire pit?

When you are not specifying a field in your query the _all field is searched. The _all field contains copy of all fields and it is using the default analyzer. It's possible to change the default analyzer to snowball as it's explained in How do I set the default analyzer for elastic search with tire? And it's also possible to control which fields are included into the _all field.

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