How to attach 2 matching queries to a query for elasticsearch?

I would like to request the data that user_id is '1' and name is 'John' . It is easy to write commonly used SQL:

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

But it is not easy for me to make a request for elasticsearch.

First I made a request for user_id :

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

And the results were what I expected.

Then I made a request for name :

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

It worked well.

But I could not make a request connecting 2 conditions and work. How can I attach these 2 matching queries to one use and operation?

+6
source share
1 answer

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

(I could not verify the request, this may be wrong, but the bool request is the answer to your problem)

 { "bool" : { "must" : [{ "match" : { "user_id" : "1" }, "match" : { "name" : "John" } }] } } 
+11
source

All Articles