user1903663
user1903663

Reputation: 1725

using ruby elasticsearch gem, how can I query with multiple paramaters

I basically want to do a search which is:

select all from index_name where tag = 'big data' and city = 'tokyo'

The code is:

require "elasticsearch"
client = Elasticsearch::Client.new log: true
client.search index: 'candidates', body:
{
  query: {

    match: {
          tags: search_term
           }
        }
}

which works fine. When, however, I change to something like:

match: {
          tags: search_term, city: 'Tokyo'
           }

that is to say just adding another parameter I get an error.

I have tried adding

filter: {
            city: 'Tokyo'
       }

also with no success.

Thanks guys, what's the best way forward here?

Upvotes: 2

Views: 1053

Answers (1)

Val
Val

Reputation: 217274

You need to check the Elasticsearch query DSL. For a simple "A and B" query you need to simply use a bool / must query. Replace your body with this:

client.search index: 'candidates', body:
{
  query: {
    bool: {
      must: [
        {
          match: {
            tags: 'big data'
          }
        },
        {
          match: {
            city: 'tokyo'
          }
        }
      ]
    }
  }
}

Upvotes: 3

Related Questions