Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join 2 match queries into a query for elasticsearch?

I'd like to query for data that user_id is '1' and name is 'John'. It's easy to write a commonly-used SQL:

select * from t where user_id = '1' and name = 'John';

But it's not easy for me to make a query for elasticsearch.

First, I made a query for user_id:

{
  "query" : {
    "match" : {
      "user_id" : "1"
    }
  }
}

And the results were what I expected.

Then, I made a query for name:

{
  "query" : {
    "match" : {
      "name" : "John"
    }
  }
}

It worked well, too.

But I couldn't make a query joining 2 conditions with and operation. How can I join those 2 match queries into one using and operation?

like image 919
philipjkim Avatar asked Nov 13 '12 15:11

philipjkim


1 Answers

What you need is a bool query in which you put all your single queries:

(I could not test the query, it might be wrong, but the bool-query is the answer to your problem)

{
    "bool" : {
        "must" : [{
            "match" : {
                "user_id" : "1"
            },
            "match" : {
                "name" : "John"
            }
        }]
    }
}
like image 129
Thorsten Avatar answered Nov 15 '22 08:11

Thorsten