# Is it possible to filter on multiple fields?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-filter-on-multiple-fields/23098>\
**Category:** Elasticsearch\
**Created:** [April 5, 2015, 11:40am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-on-multiple-fields/23098 "2015-04-05T11:40:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Akhtar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_akhtar/32/785_2.png) [@Ali\_Akhtar](https://discuss.elastic.co/u/Ali_Akhtar)\
**Post date:** [April 5, 2015, 11:40am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-on-multiple-fields/23098/1 "2015-04-05T11:40:50Z")

</div>

Hi there,

I'm able to filter on one field at a time, using:

```
            client.prepareSearch(index)
            .setTypes(type)
            .setQuery( new FilteredQueryBuilder(null, FilterBuilders
                                                    .termFilter(field, 

```

value)));

However, I need to be able to filter on multiple fields.

For example, I may have an index for books, and I need to find the books  
written by Author X, in Category Y, and in the year Z.

In this case, I'd need to filter on multiple fields (author, category,  
year).

Is it possible to combine these filters in one query?

--  
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/0df8ad1c-f29c-48b2-a30c-da3abbdc9126%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0df8ad1c-f29c-48b2-a30c-da3abbdc9126%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [April 6, 2015, 3:36am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-on-multiple-fields/23098/2 "2015-04-06T03:36:53Z")

</div>

Hello Ali ,

You need to use bool query  
[http://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-bool-query.html](http://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-bool-query.html)  
for that.  
Filtered query is not the best match for that.

You can see some code samples here  
[http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/query-dsl-queries.html#bool](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/query-dsl-queries.html#bool)  
.

Thanks  
Vineeth Mohan,  
Elasticsearch consultant,  
[qbox.io](http://qbox.io) ( Elasticsearch service provider [http://qbox.io/](http://qbox.io/))

On Sun, Apr 5, 2015 at 5:10 PM, Ali Akhtar [ali.rac200@gmail.com](mailto:ali.rac200@gmail.com) wrote:

> Hi there,
> 
> I'm able to filter on one field at a time, using:
> 
> ```
> client.prepareSearch(index)
> .setTypes(type)
> .setQuery( new FilteredQueryBuilder(null, FilterBuilders
> .termFilter(field,
> 
> ```
> 
> value)));
> 
> However, I need to be able to filter on multiple fields.
> 
> For example, I may have an index for books, and I need to find the books  
> written by Author X, in Category Y, and in the year Z.
> 
> In this case, I'd need to filter on multiple fields (author, category,  
> year).
> 
> Is it possible to combine these filters in one query?
> 
> --  
> 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/0df8ad1c-f29c-48b2-a30c-da3abbdc9126%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0df8ad1c-f29c-48b2-a30c-da3abbdc9126%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/0df8ad1c-f29c-48b2-a30c-da3abbdc9126%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0df8ad1c-f29c-48b2-a30c-da3abbdc9126%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAGdPd5m2TZThDHw2%2BkgC7cHi-YJvQrRN8FpeVSPQaU%3Dg2Li8Yg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5m2TZThDHw2%2BkgC7cHi-YJvQrRN8FpeVSPQaU%3Dg2Li8Yg%40mail.gmail.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:21am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-on-multiple-fields/23098/3 "2017-07-06T00:21:37Z")

</div>


