2
votes

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

Using the tire gem I want to use a standard analyzer for title and description, but for the body 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 title and description fields and search for the word cook in the body.

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?

1

1 Answers

6
votes

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.