2

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?

1 Answer 1

3

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'
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, getting a syntax error: search.rb:25: syntax error, unexpected '\n', expecting =>
Can you update your question with the code you have now?
it works, sorry I must have had a glitch in there somewhere. I am very grateful to you, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.