Search code examples
ruby-on-railsruby-on-rails-3elasticsearchtire

How do I search with multiple queries on the same field in elasticsearch?


I'm using ElasticSearch and tire.rb to index and search my collection of items.

I want to query on the name field in my index.

If I have a document with the name: Alfa Romeo, I would like to find this document by searching for:

  1. "Alfa"
  2. "Alfa Remeo" (notice spelling mistake)

In ElasticSearch and tire, I know how to set up the two queries separately:

Searching with wildcard:

Model.tire.search do
  query do
    boolean do
      must { string "#{myquerystring}*", default_field: 'name' }
    end
  end
end

Searching with fuzzy (Levenshtein-distance):

Model.tire.search do
  query do
    boolean do
      must { text :name, { query: mysquerystring, operator: 'AND', fuzziness: 0.4 } }
    end
  end
end

How to combine (with or)?

What I want to do is to find all documents where wildcard OR fuzzy search matches. I could do two seperate searches and try to combine them, but that does not make much sense. Can I do this in some logical way?


Solution

  • What about using a boolean query with should queries : http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html ?

    Does it answer to your use case ?

    David