2

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?

schmudu
  • 2,111
  • 1
  • 21
  • 30

1 Answers1

6

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.

Community
  • 1
  • 1
imotov
  • 28,277
  • 3
  • 90
  • 82
  • Thanks imotov, I saw that post. If I use that post as a reference I'm assuming that if I set snowball as the default analyzer it will also apply to the title and description fields. I think what I want is the second option you mentioned, namely control which fields are included in the _all field, but if I use this option can I also define to include other fields via the snowball analyzer? Also are there any samples of online that you're aware of that utilize two analyzers in the same query? Thanks again. – schmudu May 30 '12 at 12:48
  • 1
    I see, if you want to use different analyzers for different fields, the _all field is not going to work for you. The _all field is basically a copy of several fields and it can use only one analyzer. If you want different analyzers, you should use separate queries for each field and then combine these queries using [Bool Query](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html) or [Dis Max Query](http://www.elasticsearch.org/guide/reference/query-dsl/dis-max-query.html). – imotov May 30 '12 at 13:00