# Boolean query vs filters and more

**URL:** <https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225>\
**Category:** Elasticsearch\
**Created:** [August 16, 2010, 8:44pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225 "2010-08-16T20:44:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)\
**Post date:** [August 16, 2010, 8:44pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/1 "2010-08-16T20:44:01Z")

</div>

Hi,

I've just started using ElasticSearch and so far it's been fairly easy  
to get going with. However, I have a couple of questions that came up  
during the course of development:

1. I need to find docs where a string field contains a keyword, but  
restricted by an integer field matching a given value. Is it better  
to use the "bool" query feature (with sub-queries for string and  
integer portions), or do the string query with a filter?

2. Is it possible to specify multiple fields to search for a given  
keyword? So far I've been using \_all, but sometimes I need to change  
which fields I'm matching in dynamically and \_all is predetermined by  
the mapping.

Thank you,

-Andrei

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [August 16, 2010, 9:00pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/2 "2010-08-16T21:00:04Z")

</div>

Hi,

On Mon, Aug 16, 2010 at 10:44 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> Hi,
> 
> I've just started using Elasticsearch and so far it's been fairly easy  
> to get going with. However, I have a couple of questions that came up  
> during the course of development:
> 
> 1. I need to find docs where a string field contains a keyword, but  
> restricted by an integer field matching a given value. Is it better  
> to use the "bool" query feature (with sub-queries for string and  
> integer portions), or do the string query with a filter?

Filters perform better because they do not perform scoring. So I think in  
your case the filtering is way to go.

> 1. Is it possible to specify multiple fields to search for a given  
> keyword? So far I've been using \_all, but sometimes I need to change  
> which fields I'm matching in dynamically and \_all is predetermined by  
> the mapping.

I think you can use  
bool[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/bool\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/bool_query/)  
query  
and combine several  
query\_string[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/query\_string\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/query_string_query/)queries  
with default\_field specified accordingly (or you can combine  
field[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/field\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/)queries).

> Thank you,
> 
> -Andrei

Regards,  
Lukas

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 17, 2010, 6:54am UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/3 "2010-08-17T06:54:42Z")

</div>

On Tue, Aug 17, 2010 at 12:00 AM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> Hi,
> 
> On Mon, Aug 16, 2010 at 10:44 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:
> 
> > Hi,
> > 
> > I've just started using Elasticsearch and so far it's been fairly easy  
> > to get going with. However, I have a couple of questions that came up  
> > during the course of development:
> > 
> > 1. I need to find docs where a string field contains a keyword, but  
> > restricted by an integer field matching a given value. Is it better  
> > to use the "bool" query feature (with sub-queries for string and  
> > integer portions), or do the string query with a filter?
> 
> Filters perform better because they do not perform scoring. So I think in  
> your case the filtering is way to go.

Yep, filters are the best way to go here. Much faster, and easily cacheable.

> > 1. Is it possible to specify multiple fields to search for a given  
> > keyword? So far I've been using \_all, but sometimes I need to change  
> > which fields I'm matching in dynamically and \_all is predetermined by  
> > the mapping.
> 
> I think you can use bool[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/bool\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/bool_query/) query  
> and combine several query\_string[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/query\_string\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/query_string_query/)queries with default\_field specified accordingly (or you can combine  
> field[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/field\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/)queries).

Actually, if you use query\_string, then it allows you to define several  
fields for it to execute on. Check out the bottom part of the docs here:  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/query\_string\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/query_string_query/)  
.

> > Thank you,
> > 
> > -Andrei
> 
> Regards,  
> Lukas

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)\
**Post date:** [August 17, 2010, 5:56pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/4 "2010-08-17T17:56:07Z")

</div>

Great, that lets me eliminate the \_all field then.

Also, does ES support indexing non-English text?

-Andrei

On Aug 16, 11:54 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Actually, if you use query\_string, then it allows you to define several  
> fields for it to execute on. Check out the bottom part of the docs here:[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/qu](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/qu)...

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 17, 2010, 6:25pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/5 "2010-08-17T18:25:46Z")

</div>

It depends on the \_all field. For the price of storing more data in the  
index, you can get much better results in terms of query performance  
(comparing \_all to X files query).

-shay.banon

On Tue, Aug 17, 2010 at 8:56 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> Great, that lets me eliminate the \_all field then.
> 
> Also, does ES support indexing non-English text?
> 
> -Andrei
> 
> On Aug 16, 11:54 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Actually, if you use query\_string, then it allows you to define several  
> > fields for it to execute on. Check out the bottom part of the docs here:  
> > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/qu](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/qu)...

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)\
**Post date:** [August 17, 2010, 7:40pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/6 "2010-08-17T19:40:58Z")

</div>

I currently only search against 3 fields, so that's what my \_all would  
contain. The total length of 3 fields is likely to be 300 to 1000  
characters.

-Andrei

On Aug 17, 11:25 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> It depends on the \_all field. For the price of storing more data in the  
> index, you can get much better results in terms of query performance  
> (comparing \_all to X files query).
> 
> -shay.banon

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 17, 2010, 7:45pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/7 "2010-08-17T19:45:01Z")

</div>

Then that might make sense, really depends on the latency of you require  
from your searches. \_all field causes larger index and slower indexing time,  
but usually results in faster queries when trying to match on several  
fields. In your case, it sounds like \_all field is not needed, but I would  
run a simple test to see the difference in expected queries.

-shay.banon

On Tue, Aug 17, 2010 at 10:40 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> I currently only search against 3 fields, so that's what my \_all would  
> contain. The total length of 3 fields is likely to be 300 to 1000  
> characters.
> 
> -Andrei
> 
> On Aug 17, 11:25 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > It depends on the \_all field. For the price of storing more data in the  
> > index, you can get much better results in terms of query performance  
> > (comparing \_all to X files query).
> > 
> > -shay.banon

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)\
**Post date:** [August 17, 2010, 9:19pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/8 "2010-08-17T21:19:15Z")

</div>

Will do. Is there a good way to time ES queries aside from just timing  
the curl request?

-Andrei

On Aug 17, 12:45 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Then that might make sense, really depends on the latency of you require  
> from your searches. \_all field causes larger index and slower indexing time,  
> but usually results in faster queries when trying to match on several  
> fields. In your case, it sounds like \_all field is not needed, but I would  
> run a simple test to see the difference in expected queries.
> 
> -shay.banon

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 17, 2010, 10:11pm UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/9 "2010-08-17T22:11:42Z")

</div>

Thats pretty much it, though I think you should time it based on how you  
plan to actually invoke it in production (from the lang you plan to use, and  
so on).

-shay.banon

On Wed, Aug 18, 2010 at 12:19 AM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> Will do. Is there a good way to time ES queries aside from just timing  
> the curl request?
> 
> -Andrei
> 
> On Aug 17, 12:45 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Then that might make sense, really depends on the latency of you require  
> > from your searches. \_all field causes larger index and slower indexing  
> > time,  
> > but usually results in faster queries when trying to match on several  
> > fields. In your case, it sounds like \_all field is not needed, but I  
> > would  
> > run a simple test to see the difference in expected queries.
> > 
> > -shay.banon

---

<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, 4:20am UTC](https://discuss.elastic.co/t/boolean-query-vs-filters-and-more/3225/10 "2017-07-06T04:20:39Z")

</div>


