# Beginners Query Questions

**URL:** <https://discuss.elastic.co/t/beginners-query-questions/3874>\
**Category:** Elasticsearch\
**Created:** [February 3, 2011, 12:39pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874 "2011-02-03T12:39:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [February 3, 2011, 12:39pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/1 "2011-02-03T12:39:35Z")

</div>

Hi,

I'm just looking at the different sorts of queries I can use with the  
Java API and I have a couple of questions to clear up.

1. What is a termQuery used for, how does it differ from a fieldQuery

- initially I though termQueries were for exact quoted "this exact  
expression" type queries, but now I'm not so sure.

1. How are filters used in a way that is different from a boolean AND  
search?

I'm very new to ElasticSearch so any detailled answers to enlighten a  
newbie would be much appreciated.

thanks,

David

---

<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:** [February 3, 2011, 1:08pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/2 "2011-02-03T13:08:56Z")

</div>

Hi,

On Thu, Feb 3, 2011 at 1:39 PM, dalesrob [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:

> Hi,
> 
> I'm just looking at the different sorts of queries I can use with the  
> Java API and I have a couple of questions to clear up.
> 
> 1. What is a termQuery used for, how does it differ from a fieldQuery
> 
> - initially I though termQueries were for exact quoted "this exact  
> expression" type queries, but now I'm not so sure.

Term queries does not perform any analysis (  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/term\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/term_query/)).  
Thus they are for exact matches.  
However, field query (  
[Query DSL | Elasticsearch Guide [8.11] | Elastic](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/))  
uses analysis that is setup for this field (or default).

> 1. How are filters used in a way that is different from a boolean AND  
> search?

Filters do no scoring. So if you can use filter instead of query then you  
probably should (much faster and can be cached).

> I'm very new to Elasticsearch so any detailled answers to enlighten a  
> newbie would be much appreciated.
> 
> thanks,
> 
> David

Regards,  
Lukas

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 3, 2011, 1:54pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/3 "2011-02-03T13:54:00Z")

</div>

Just looking at this answer, do boolean query and filtered guarantee same  
result?

On Thu, Feb 3, 2011 at 2:08 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> Hi,
> 
> On Thu, Feb 3, 2011 at 1:39 PM, dalesrob [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:
> 
> > Hi,
> > 
> > I'm just looking at the different sorts of queries I can use with the  
> > Java API and I have a couple of questions to clear up.
> > 
> > 1. What is a termQuery used for, how does it differ from a fieldQuery
> > 
> > - initially I though termQueries were for exact quoted "this exact  
> > expression" type queries, but now I'm not so sure.
> 
> Term queries does not perform any analysis (  
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/term\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/term_query/)).  
> Thus they are for exact matches.  
> However, field query (  
> [Query DSL | Elasticsearch Guide [8.11] | Elastic](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/))  
> uses analysis that is setup for this field (or default).
> 
> > 1. How are filters used in a way that is different from a boolean AND  
> > search?
> 
> Filters do no scoring. So if you can use filter instead of query then you  
> probably should (much faster and can be cached).
> 
> > I'm very new to Elasticsearch so any detailled answers to enlighten a  
> > newbie would be much appreciated.
> > 
> > thanks,
> > 
> > David
> 
> Regards,  
> Lukas

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 3, 2011, 1:59pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/4 "2011-02-03T13:59:27Z")

</div>

I ask because with the same set of data, this query:

{

```
"from" : 0, size: 100,

"query" : {

    "bool" : {

        "must" : {

           "fuzzy" : {

   "_all" : {

        "value" : "lacie"

    }

}

        },

        "must" : {

           "fuzzy" : {

   "_all" : {

        "value" : "disco"

    }

}

        }

    }

}

```

}

Returns:

"max\_score": 1.0678896,

```
"total": 1830

```

Whereas if I use this query:

{

```
"from" : 0, size: 100,

"query" : {

    "fuzzy" : {

   "_all" : {

        "value" : "lacie"

    }

},

```

"filtered" : {

"query" : {

```
           "fuzzy" : {

   "_all" : {

        "value" : "disco"

    }

 }

        }

    }

```

}

Then I get many more results:

"max\_score": 3.9756753,

```
"total": 9825

```

Is this the expected behaviour?

Thanks.

On Thu, Feb 3, 2011 at 2:54 PM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> Just looking at this answer, do boolean query and filtered guarantee same  
> result?
> 
> On Thu, Feb 3, 2011 at 2:08 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:
> 
> > Hi,
> > 
> > On Thu, Feb 3, 2011 at 1:39 PM, dalesrob [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:
> > 
> > > Hi,
> > > 
> > > I'm just looking at the different sorts of queries I can use with the  
> > > Java API and I have a couple of questions to clear up.
> > > 
> > > 1. What is a termQuery used for, how does it differ from a fieldQuery
> > > 
> > > - initially I though termQueries were for exact quoted "this exact  
> > > expression" type queries, but now I'm not so sure.
> > 
> > Term queries does not perform any analysis (  
> > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/term\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/term_query/)).  
> > Thus they are for exact matches.  
> > However, field query (  
> > [Query DSL | Elasticsearch Guide [8.11] | Elastic](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/))  
> > uses analysis that is setup for this field (or default).
> > 
> > > 1. How are filters used in a way that is different from a boolean AND  
> > > search?
> > 
> > Filters do no scoring. So if you can use filter instead of query then you  
> > probably should (much faster and can be cached).
> > 
> > > I'm very new to Elasticsearch so any detailled answers to enlighten a  
> > > newbie would be much appreciated.
> > > 
> > > thanks,
> > > 
> > > David
> > 
> > Regards,  
> > Lukas

---

<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:** [February 3, 2011, 2:17pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/5 "2011-02-03T14:17:19Z")

</div>

I think your second query is not correct.

[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/filtered\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/filtered_query/)How  
about this:

"query" : {  
"filtered" : {  
"query" : { ... your fuzzy query #1 ...},  
"filter" : {  
"query" : { ... your fuzzy query #2 ...}  
}  
}  
}

In other words combination of  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/filtered\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/filtered_query/)  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/query\_filter/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/query_filter/)

However, I did not try this myself...

Regards,  
Lukas

On Thu, Feb 3, 2011 at 2:59 PM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> I ask because with the same set of data, this query:
> 
> {
> 
> ```
> "from" : 0, size: 100,
> 
> "query" : {
> 
> "bool" : {
> 
> "must" : {
> 
> "fuzzy" : {
> 
> "_all" : {
> 
> "value" : "lacie"
> 
> }
> 
> }
> 
> },
> 
> "must" : {
> 
> "fuzzy" : {
> 
> "_all" : {
> 
> "value" : "disco"
> 
> }
> 
> }
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> Returns:
> 
> "max\_score": 1.0678896,
> 
> ```
> "total": 1830
> 
> ```
> 
> Whereas if I use this query:
> 
> {
> 
> ```
> "from" : 0, size: 100,
> 
> "query" : {
> 
> "fuzzy" : {
> 
> "_all" : {
> 
> "value" : "lacie"
> 
> }
> 
> },
> 
> ```
> 
> "filtered" : {
> 
> "query" : {
> 
> ```
> "fuzzy" : {
> 
> "_all" : {
> 
> "value" : "disco"
> 
> }
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> Then I get many more results:
> 
> "max\_score": 3.9756753,
> 
> ```
> "total": 9825
> 
> ```
> 
> Is this the expected behaviour?
> 
> Thanks.
> 
> On Thu, Feb 3, 2011 at 2:54 PM, Enrique Medina Montenegro \<  
> [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:
> 
> > Just looking at this answer, do boolean query and filtered guarantee same  
> > result?
> > 
> > On Thu, Feb 3, 2011 at 2:08 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com)wrote:
> > 
> > > Hi,
> > > 
> > > On Thu, Feb 3, 2011 at 1:39 PM, dalesrob [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm just looking at the different sorts of queries I can use with the  
> > > > Java API and I have a couple of questions to clear up.
> > > > 
> > > > 1. What is a termQuery used for, how does it differ from a fieldQuery
> > > > 
> > > > - initially I though termQueries were for exact quoted "this exact  
> > > > expression" type queries, but now I'm not so sure.
> > > 
> > > Term queries does not perform any analysis (  
> > > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/term\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/term_query/)).  
> > > Thus they are for exact matches.  
> > > However, field query (  
> > > [Query DSL | Elasticsearch Guide [8.11] | Elastic](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/))  
> > > uses analysis that is setup for this field (or default).
> > > 
> > > > 1. How are filters used in a way that is different from a boolean AND  
> > > > search?
> > > 
> > > Filters do no scoring. So if you can use filter instead of query then you  
> > > probably should (much faster and can be cached).
> > > 
> > > > I'm very new to Elasticsearch so any detailled answers to enlighten a  
> > > > newbie would be much appreciated.
> > > > 
> > > > thanks,
> > > > 
> > > > David
> > > 
> > > Regards,  
> > > Lukas

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 3, 2011, 2:31pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/6 "2011-02-03T14:31:44Z")

</div>

Yes, you're right, it was not properly configured. So now the filter  
approach is:

{

```
"from" : 0, size: 100,

"query" : {

```

"filtered" : {

"query" : {

```
     "fuzzy" : {

    "_all" : {

         "value" : "lacie"

     }

```

}

},

"filter" : {

"query" : {

```
            "fuzzy" : {

    "_all" : {

          "value" : "disco"

      }

  }

         }

```

}

```
    }

}

```

}

which gives a result of 1826, just 4 less than the other one.

Should it still be the same exact result or that deviation is fine?

And BTW, what was my previous query doing then?

(Original purpose was to find "disco lacie" as the search string).

Thanks.

On Thu, Feb 3, 2011 at 3:17 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> I think your second query is not correct.
> 
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/filtered\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/filtered_query/)How  
> about this:
> 
> "query" : {  
> "filtered" : {  
> "query" : { ... your fuzzy query #1 ...},  
> "filter" : {  
> "query" : { ... your fuzzy query #2 ...}  
> }  
> }  
> }
> 
> In other words combination of
> 
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/filtered\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/filtered_query/)
> 
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/query\_filter/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/query_filter/)
> 
> However, I did not try this myself...
> 
> Regards,  
> Lukas
> 
> On Thu, Feb 3, 2011 at 2:59 PM, Enrique Medina Montenegro \<  
> [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:
> 
> > I ask because with the same set of data, this query:
> > 
> > {
> > 
> > ```
> > "from" : 0, size: 100,
> > 
> > "query" : {
> > 
> > "bool" : {
> > 
> > "must" : {
> > 
> > "fuzzy" : {
> > 
> > "_all" : {
> > 
> > "value" : "lacie"
> > 
> > }
> > 
> > }
> > 
> > },
> > 
> > "must" : {
> > 
> > "fuzzy" : {
> > 
> > "_all" : {
> > 
> > "value" : "disco"
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > ```
> > 
> > }
> > 
> > Returns:
> > 
> > "max\_score": 1.0678896,
> > 
> > ```
> > "total": 1830
> > 
> > ```
> > 
> > Whereas if I use this query:
> > 
> > {
> > 
> > ```
> > "from" : 0, size: 100,
> > 
> > "query" : {
> > 
> > "fuzzy" : {
> > 
> > "_all" : {
> > 
> > "value" : "lacie"
> > 
> > }
> > 
> > },
> > 
> > ```
> > 
> > "filtered" : {
> > 
> > "query" : {
> > 
> > ```
> > "fuzzy" : {
> > 
> > "_all" : {
> > 
> > "value" : "disco"
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > ```
> > 
> > }
> > 
> > Then I get many more results:
> > 
> > "max\_score": 3.9756753,
> > 
> > ```
> > "total": 9825
> > 
> > ```
> > 
> > Is this the expected behaviour?
> > 
> > Thanks.
> > 
> > On Thu, Feb 3, 2011 at 2:54 PM, Enrique Medina Montenegro \<  
> > [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:
> > 
> > > Just looking at this answer, do boolean query and filtered guarantee same  
> > > result?
> > > 
> > > On Thu, Feb 3, 2011 at 2:08 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com)wrote:
> > > 
> > > > Hi,
> > > > 
> > > > On Thu, Feb 3, 2011 at 1:39 PM, dalesrob [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I'm just looking at the different sorts of queries I can use with the  
> > > > > Java API and I have a couple of questions to clear up.
> > > > > 
> > > > > 1. What is a termQuery used for, how does it differ from a fieldQuery
> > > > > 
> > > > > - initially I though termQueries were for exact quoted "this exact  
> > > > > expression" type queries, but now I'm not so sure.
> > > > 
> > > > Term queries does not perform any analysis (  
> > > > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/term\_query/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/term_query/)).  
> > > > Thus they are for exact matches.  
> > > > However, field query (  
> > > > [Query DSL | Elasticsearch Guide [8.11] | Elastic](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/field_query/))  
> > > > uses analysis that is setup for this field (or default).
> > > > 
> > > > > 1. How are filters used in a way that is different from a boolean AND  
> > > > > search?
> > > > 
> > > > Filters do no scoring. So if you can use filter instead of query then  
> > > > you probably should (much faster and can be cached).
> > > > 
> > > > > I'm very new to Elasticsearch so any detailled answers to enlighten a  
> > > > > newbie would be much appreciated.
> > > > > 
> > > > > thanks,
> > > > > 
> > > > > David
> > > > 
> > > > Regards,  
> > > > Lukas

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [February 3, 2011, 5:29pm UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/7 "2011-02-03T17:29:51Z")

</div>

Hi Lukas,

Thanks for the reply, does the same apply to the Lucence syntax "Query  
String" Queries usings booleans? Would it be better to use (cached)  
filters?

thanks,

David.

On Feb 3, 1:08 pm, Lukáš Vlček [lukas.vl...@gmail.com](mailto:lukas.vl...@gmail.com) wrote:

> Hi,
> 
> On Thu, Feb 3, 2011 at 1:39 PM, dalesrob [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > Hi,
> 
> > I'm just looking at the different sorts of queries I can use with the  
> > Java API and I have a couple of questions to clear up.
> 
> > 1. What is a termQuery used for, how does it differ from a fieldQuery
> > 
> > - initially I though termQueries were for exact quoted "this exact  
> > expression" type queries, but now I'm not so sure.
> 
> Term queries does not perform any analysis ([http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/te](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/te)...).  
> Thus they are for exact matches.  
> However, field query ([http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/fi](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/fi)...)  
> uses analysis that is setup for this field (or default).
> 
> > 1. How are filters used in a way that is different from a boolean AND  
> > search?
> 
> Filters do no scoring. So if you can use filter instead of query then you  
> probably should (much faster and can be cached).
> 
> > I'm very new to Elasticsearch so any detailled answers to enlighten a  
> > newbie would be much appreciated.
> 
> > thanks,
> 
> > David
> 
> Regards,  
> Lukas

---

<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:12am UTC](https://discuss.elastic.co/t/beginners-query-questions/3874/8 "2017-07-06T04:12:43Z")

</div>


