# How to find all docs where field\_a === val1 and field\_b === val2?

**URL:** https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653
**Category:** Elasticsearch
**Created:** [January 14, 2015, 10:59pm UTC](https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653 "2015-01-14T22:59:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jerrac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jerrac/32/52980_2.png) [@jerrac](https://discuss.elastic.co/u/jerrac)
#### Post date: [January 14, 2015, 10:59pm UTC](https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653/1 "2015-01-14T22:59:01Z")

</div>

So far I can find all docs where field\_a == val1. But I can't figure out  
how to add the second condition.

I've been trying this with the elasticsearch php library.

```
    $postParams = [];

```

> ```
> $postParams['index'] = 'myindex';
> $postParams['type'] = 'mytype';
> $postParams['body'] = '{
> "query" : {
> "match" : {
> "status" : {"query" => "unread", "operator" : "and" },
> "priority" : {"query" => 5, "operator" : "and" }
> }
> }
> }';
> $results = $esclient->search($postParams);
> 
> ```

I've also tried:

> $postParams['body'] = '{  
> "query" : {  
> "match" : {  
> "status" : {"query" =\> "unread", "operator" : "and" }  
> },  
> "match" : {  
> "priority" : {"query" =\> 5, "operator" : "and" }  
> }  
> }  
> }';

And a bunch of other ways that I didn't document.

So, what am I missing?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3c6ad98b-dbfe-42b0-bddf-4a47e0aec368%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3c6ad98b-dbfe-42b0-bddf-4a47e0aec368%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [January 14, 2015, 11:31pm UTC](https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653/2 "2015-01-14T23:31:28Z")

</div>

David,

This is what I use. I hope it helps.

{  
"_bool_" : {  
"_must_" : [ {  
"match" : {  
"field\_a" : {  
"query" : "val1",  
"type" : "boolean"  
}  
}  
}, {  
"match" : {  
"field\_b" : {  
"query" : "val2",  
"type" : "boolean"  
}  
}  
} ]  
}  
}

Brian

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/64aa8c31-2002-4432-bd7f-5c9bba3184da%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/64aa8c31-2002-4432-bd7f-5c9bba3184da%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [January 14, 2015, 11:34pm UTC](https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653/3 "2015-01-14T23:34:05Z")

</div>

By the way, David, the full query follows:

{  
"from" : 0,  
"size" : 20,  
"timeout" : 60000,  
"_query_" : {  
"_bool_" : {  
"_must_" : [ {  
"match" : {  
"field\_a" : {  
"query" : "val1",  
"type" : "boolean"  
}  
}  
}, {  
"match" : {  
"field\_b" : {  
"query" : "val2",  
"type" : "boolean"  
}  
}  
} ]  
}  
},  
"version" : true,  
"explain" : false,  
"fields" : ["\_ttl", "\_source"]  
}

Also note that since the \_ttl field is being requested (always), then the  
\_source must also be asked for explicitly. If you don't ask for any fields,  
\_source is returned by default. But if you ask for one or more fields  
explicitly, then you must also ask for \_source or it won't be returned.

Brian

On Wednesday, January 14, 2015 at 6:31:29 PM UTC-5, Brian wrote:

> David,
> 
> This is what I use. I hope it helps.
> 
> {  
> "_bool_" : {  
> "_must_" : [ {  
> "match" : {  
> "field\_a" : {  
> "query" : "val1",  
> "type" : "boolean"  
> }  
> }  
> }, {  
> "match" : {  
> "field\_b" : {  
> "query" : "val2",  
> "type" : "boolean"  
> }  
> }  
> } ]  
> }  
> }
> 
> Brian

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ee28beb0-4ee6-463d-8891-a2d158e00934%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ee28beb0-4ee6-463d-8891-a2d158e00934%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![jerrac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jerrac/32/52980_2.png) [@jerrac](https://discuss.elastic.co/u/jerrac)
#### Post date: [January 15, 2015, 5:38pm UTC](https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653/4 "2015-01-15T17:38:42Z")

</div>

Thanks! I was thinking a bool query was something specific to fields with  
boolean values. Which is why I didn't understand the bool query example in  
the docs. Your posts helped me get what I wanted. 🙂

On Wednesday, January 14, 2015 at 3:34:05 PM UTC-8, Brian wrote:

> By the way, David, the full query follows:
> 
> {  
> "from" : 0,  
> "size" : 20,  
> "timeout" : 60000,  
> "_query_" : {  
> "_bool_" : {  
> "_must_" : [ {  
> "match" : {  
> "field\_a" : {  
> "query" : "val1",  
> "type" : "boolean"  
> }  
> }  
> }, {  
> "match" : {  
> "field\_b" : {  
> "query" : "val2",  
> "type" : "boolean"  
> }  
> }  
> } ]  
> }  
> },  
> "version" : true,  
> "explain" : false,  
> "fields" : ["\_ttl", "\_source"]  
> }
> 
> Also note that since the \_ttl field is being requested (always), then the  
> \_source must also be asked for explicitly. If you don't ask for any fields,  
> \_source is returned by default. But if you ask for one or more fields  
> explicitly, then you must also ask for \_source or it won't be returned.
> 
> Brian
> 
> On Wednesday, January 14, 2015 at 6:31:29 PM UTC-5, Brian wrote:
> 
> > David,
> > 
> > This is what I use. I hope it helps.
> > 
> > {  
> > "_bool_" : {  
> > "_must_" : [ {  
> > "match" : {  
> > "field\_a" : {  
> > "query" : "val1",  
> > "type" : "boolean"  
> > }  
> > }  
> > }, {  
> > "match" : {  
> > "field\_b" : {  
> > "query" : "val2",  
> > "type" : "boolean"  
> > }  
> > }  
> > } ]  
> > }  
> > }
> > 
> > Brian

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/feb306b6-aa38-4eaf-a9fc-ad23be10ea4a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/feb306b6-aa38-4eaf-a9fc-ad23be10ea4a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 12:38am UTC](https://discuss.elastic.co/t/how-to-find-all-docs-where-field-a-val1-and-field-b-val2/21653/5 "2017-07-06T00:38:38Z")

</div>


