# Help with Elastic search multi clause query

**URL:** <https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323>\
**Category:** Elasticsearch\
**Created:** [March 26, 2013, 6:27pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323 "2013-03-26T18:27:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shgeorge](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@shgeorge](https://discuss.elastic.co/u/shgeorge)\
**Post date:** [March 26, 2013, 6:27pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/1 "2013-03-26T18:27:07Z")

</div>

Hi all  
I need some help with Java DSL for Elastic search.

I have the below query:

{

"bool" : {

```
"must" : {

  "term" : {

    "pl" : "728x90"

  }

},

"must_not" : {

  "term" : {

    "bi" : "qa"

  }

}

```

}

}

The DSL corresponding is

QueryBuilder biBuilder = QueryBuilders.termQuery(

"bi", val);

QueryBuilder plBuilder = QueryBuilders.termQuery(

"pl", plval);

QueryBuilders.boolQuery()

.must(plBuilder)

.mustNot(biBuilder);

But in the results , I am getting records that has "bi" = "qa". I am  
supposed to get results which match "pl=728x90" AND that doesnt have  
"bi=qa". Please let me know if I am doing something wrong.

Thanks

G

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [March 27, 2013, 6:31pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/2 "2013-03-27T18:31:30Z")

</div>

Can you provide a few examples documents that are being matched but should  
not be? The mapping would also be helpful.

Term queries are not analyzed, so they will only match exactly  
(case sensitive) the token in the index. Also, if you do not need to have  
your items ranked/score, it would be more efficient to have that query as a  
filter instead.

--  
Ivan

On Tue, Mar 26, 2013 at 11:27 AM, george [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com) wrote:

> Hi all  
> I need some help with Java DSL for Elastic search.
> 
> I have the below query:
> 
> {
> 
> "bool" : {
> 
> ```
> "must" : {
> 
> "term" : {
> 
> "pl" : "728x90"
> 
> }
> 
> },
> 
> "must_not" : {
> 
> "term" : {
> 
> "bi" : "qa"
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> }
> 
> The DSL corresponding is
> 
> QueryBuilder biBuilder = QueryBuilders.termQuery(
> 
> "bi", val);
> 
> QueryBuilder plBuilder = QueryBuilders.termQuery(
> 
> "pl", plval);
> 
> QueryBuilders.boolQuery()
> 
> .must(plBuilder)
> 
> .mustNot(biBuilder);
> 
> But in the results , I am getting records that has "bi" = "qa". I am  
> supposed to get results which match "pl=728x90" AND that doesnt have  
> "bi=qa". Please let me know if I am doing something wrong.
> 
> Thanks
> 
> G
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![shgeorge](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@shgeorge](https://discuss.elastic.co/u/shgeorge)\
**Post date:** [April 3, 2013, 6:50pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/3 "2013-04-03T18:50:16Z")

</div>

Hi Ivan  
Sorry for the delay. The issue turned out that we were using "." (eg :  
xyz.1) in the searchable fields . Looks like ES treats "." as special  
character and during tokenization it is replaced with space.  
I am thinking of using "not\_analyzed" for strings that needn't be  
tokenized. Do you recommend using cache with filters? In that case I assume  
we'll loose all the updates unless the expiry is too short .

Thanks  
George

On Wed, Mar 27, 2013 at 11:31 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Can you provide a few examples documents that are being matched but should  
> not be? The mapping would also be helpful.
> 
> Term queries are not analyzed, so they will only match exactly  
> (case sensitive) the token in the index. Also, if you do not need to have  
> your items ranked/score, it would be more efficient to have that query as a  
> filter instead.
> 
> --  
> Ivan
> 
> On Tue, Mar 26, 2013 at 11:27 AM, george [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com) wrote:
> 
> > Hi all  
> > I need some help with Java DSL for Elastic search.
> > 
> > I have the below query:
> > 
> > {
> > 
> > "bool" : {
> > 
> > ```
> > "must" : {
> > 
> > "term" : {
> > 
> > "pl" : "728x90"
> > 
> > }
> > 
> > },
> > 
> > "must_not" : {
> > 
> > "term" : {
> > 
> > "bi" : "qa"
> > 
> > }
> > 
> > }
> > 
> > ```
> > 
> > }
> > 
> > }
> > 
> > The DSL corresponding is
> > 
> > QueryBuilder biBuilder = QueryBuilders.termQuery(
> > 
> > "bi", val);
> > 
> > QueryBuilder plBuilder = QueryBuilders.termQuery(
> > 
> > "pl", plval);
> > 
> > QueryBuilders.boolQuery()
> > 
> > .must(plBuilder)
> > 
> > .mustNot(biBuilder);
> > 
> > But in the results , I am getting records that has "bi" = "qa". I am  
> > supposed to get results which match "pl=728x90" AND that doesnt have  
> > "bi=qa". Please let me know if I am doing something wrong.
> > 
> > Thanks
> > 
> > G
> > 
> > --  
> > 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).
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Sheeba Ann George

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [April 4, 2013, 2:39pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/4 "2013-04-04T14:39:52Z")

</div>

If a string does not need to be tokenized, then not analyzing almost always  
makes sense.

The caches should be invalidated when a document is updated. You also have  
the option of turning off caching on a filter-by-filter basis.

--  
Ivan

On Wed, Apr 3, 2013 at 11:50 AM, Sheeba George [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com)wrote:

> Hi Ivan  
> Sorry for the delay. The issue turned out that we were using "." (eg :  
> xyz.1) in the searchable fields . Looks like ES treats "." as special  
> character and during tokenization it is replaced with space.  
> I am thinking of using "not\_analyzed" for strings that needn't be  
> tokenized. Do you recommend using cache with filters? In that case I assume  
> we'll loose all the updates unless the expiry is too short .
> 
> Thanks  
> George
> 
> On Wed, Mar 27, 2013 at 11:31 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > Can you provide a few examples documents that are being matched but  
> > should not be? The mapping would also be helpful.
> > 
> > Term queries are not analyzed, so they will only match exactly  
> > (case sensitive) the token in the index. Also, if you do not need to have  
> > your items ranked/score, it would be more efficient to have that query as a  
> > filter instead.
> > 
> > --  
> > Ivan
> > 
> > On Tue, Mar 26, 2013 at 11:27 AM, george [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com) wrote:
> > 
> > > Hi all  
> > > I need some help with Java DSL for Elastic search.
> > > 
> > > I have the below query:
> > > 
> > > {
> > > 
> > > "bool" : {
> > > 
> > > ```
> > > "must" : {
> > > 
> > > "term" : {
> > > 
> > > "pl" : "728x90"
> > > 
> > > }
> > > 
> > > },
> > > 
> > > "must_not" : {
> > > 
> > > "term" : {
> > > 
> > > "bi" : "qa"
> > > 
> > > }
> > > 
> > > }
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > }
> > > 
> > > The DSL corresponding is
> > > 
> > > QueryBuilder biBuilder = QueryBuilders.termQuery(
> > > 
> > > "bi", val);
> > > 
> > > QueryBuilder plBuilder = QueryBuilders.termQuery(
> > > 
> > > "pl", plval);
> > > 
> > > QueryBuilders.boolQuery()
> > > 
> > > .must(plBuilder)
> > > 
> > > .mustNot(biBuilder);
> > > 
> > > But in the results , I am getting records that has "bi" = "qa". I am  
> > > supposed to get results which match "pl=728x90" AND that doesnt have  
> > > "bi=qa". Please let me know if I am doing something wrong.
> > > 
> > > Thanks
> > > 
> > > G
> > > 
> > > --  
> > > 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).
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Sheeba Ann George
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![shgeorge](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@shgeorge](https://discuss.elastic.co/u/shgeorge)\
**Post date:** [April 4, 2013, 4:17pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/5 "2013-04-04T16:17:48Z")

</div>

Thanks Ivan. When we set cachable as true, are the results of the filter  
also cached or just the filter query is cached. Can you please confirm.  
I am using Java DSL for filter query and had to wrap with a constant score  
(we dont need scoring) query to get the results. Does the below query  
look fine?

{

"constant\_score" : {

```
"filter" : {

  "bool" : {

    "must" : {

      "term" : {

        "p" : "120"

      }

    },

    "must" : {

      "terms" : {

        "c" : ["gc"],

        "execution" : "bool"

      }

    },

    "must" : {

      "term" : {

        "b" : "bbb"

      }

    }

  }

}

```

}

}

On Thu, Apr 4, 2013 at 7:39 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> If a string does not need to be tokenized, then not analyzing almost  
> always makes sense.
> 
> The caches should be invalidated when a document is updated. You also have  
> the option of turning off caching on a filter-by-filter basis.
> 
> --  
> Ivan
> 
> On Wed, Apr 3, 2013 at 11:50 AM, Sheeba George [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com)wrote:
> 
> > Hi Ivan  
> > Sorry for the delay. The issue turned out that we were using "." (eg :  
> > xyz.1) in the searchable fields . Looks like ES treats "." as special  
> > character and during tokenization it is replaced with space.  
> > I am thinking of using "not\_analyzed" for strings that needn't be  
> > tokenized. Do you recommend using cache with filters? In that case I assume  
> > we'll loose all the updates unless the expiry is too short .
> > 
> > Thanks  
> > George
> > 
> > On Wed, Mar 27, 2013 at 11:31 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > 
> > > Can you provide a few examples documents that are being matched but  
> > > should not be? The mapping would also be helpful.
> > > 
> > > Term queries are not analyzed, so they will only match exactly  
> > > (case sensitive) the token in the index. Also, if you do not need to have  
> > > your items ranked/score, it would be more efficient to have that query as a  
> > > filter instead.
> > > 
> > > --  
> > > Ivan
> > > 
> > > On Tue, Mar 26, 2013 at 11:27 AM, george [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com)wrote:
> > > 
> > > > Hi all  
> > > > I need some help with Java DSL for Elastic search.
> > > > 
> > > > I have the below query:
> > > > 
> > > > {
> > > > 
> > > > "bool" : {
> > > > 
> > > > ```
> > > > "must" : {
> > > > 
> > > > "term" : {
> > > > 
> > > > "pl" : "728x90"
> > > > 
> > > > }
> > > > 
> > > > },
> > > > 
> > > > "must_not" : {
> > > > 
> > > > "term" : {
> > > > 
> > > > "bi" : "qa"
> > > > 
> > > > }
> > > > 
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > }
> > > > 
> > > > }
> > > > 
> > > > The DSL corresponding is
> > > > 
> > > > QueryBuilder biBuilder = QueryBuilders.termQuery(
> > > > 
> > > > "bi", val);
> > > > 
> > > > QueryBuilder plBuilder = QueryBuilders.termQuery(
> > > > 
> > > > "pl", plval);
> > > > 
> > > > QueryBuilders.boolQuery()
> > > > 
> > > > .must(plBuilder)
> > > > 
> > > > .mustNot(biBuilder);
> > > > 
> > > > But in the results , I am getting records that has "bi" = "qa". I am  
> > > > supposed to get results which match "pl=728x90" AND that doesnt have  
> > > > "bi=qa". Please let me know if I am doing something wrong.
> > > > 
> > > > Thanks
> > > > 
> > > > G
> > > > 
> > > > --  
> > > > 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).
> > > > 
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US)  
> > > .  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > Sheeba Ann George
> > 
> > --  
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Sheeba Ann George

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [April 5, 2013, 7:20pm UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/6 "2013-04-05T19:20:03Z")

</div>

Only filters are cached in Lucene since they are just bitsets. I have not  
used a filtered query, so I don't know if it cached, but if the API has a  
cache method, then it supports it.

--  
Ivan

On Thu, Apr 4, 2013 at 9:17 AM, Sheeba George [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com)wrote:

> Thanks Ivan. When we set cachable as true, are the results of the filter  
> also cached or just the filter query is cached. Can you please confirm.  
> I am using Java DSL for filter query and had to wrap with a constant score  
> (we dont need scoring) query to get the results. Does the below query  
> look fine?
> 
> {
> 
> "constant\_score" : {
> 
> ```
> "filter" : {
> 
> "bool" : {
> 
> "must" : {
> 
> "term" : {
> 
> "p" : "120"
> 
> }
> 
> },
> 
> "must" : {
> 
> "terms" : {
> 
> "c" : ["gc"],
> 
> "execution" : "bool"
> 
> }
> 
> },
> 
> "must" : {
> 
> "term" : {
> 
> "b" : "bbb"
> 
> }
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> }
> 
> On Thu, Apr 4, 2013 at 7:39 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > If a string does not need to be tokenized, then not analyzing almost  
> > always makes sense.
> > 
> > The caches should be invalidated when a document is updated. You also  
> > have the option of turning off caching on a filter-by-filter basis.
> > 
> > --  
> > Ivan
> > 
> > On Wed, Apr 3, 2013 at 11:50 AM, Sheeba George [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com)wrote:
> > 
> > > Hi Ivan  
> > > Sorry for the delay. The issue turned out that we were using "." (eg :  
> > > xyz.1) in the searchable fields . Looks like ES treats "." as special  
> > > character and during tokenization it is replaced with space.  
> > > I am thinking of using "not\_analyzed" for strings that needn't be  
> > > tokenized. Do you recommend using cache with filters? In that case I assume  
> > > we'll loose all the updates unless the expiry is too short .
> > > 
> > > Thanks  
> > > George
> > > 
> > > On Wed, Mar 27, 2013 at 11:31 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > > 
> > > > Can you provide a few examples documents that are being matched but  
> > > > should not be? The mapping would also be helpful.
> > > > 
> > > > Term queries are not analyzed, so they will only match exactly  
> > > > (case sensitive) the token in the index. Also, if you do not need to have  
> > > > your items ranked/score, it would be more efficient to have that query as a  
> > > > filter instead.
> > > > 
> > > > --  
> > > > Ivan
> > > > 
> > > > On Tue, Mar 26, 2013 at 11:27 AM, george [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com)wrote:
> > > > 
> > > > > Hi all  
> > > > > I need some help with Java DSL for Elastic search.
> > > > > 
> > > > > I have the below query:
> > > > > 
> > > > > {
> > > > > 
> > > > > "bool" : {
> > > > > 
> > > > > ```
> > > > > "must" : {
> > > > > 
> > > > > "term" : {
> > > > > 
> > > > > "pl" : "728x90"
> > > > > 
> > > > > }
> > > > > 
> > > > > },
> > > > > 
> > > > > "must_not" : {
> > > > > 
> > > > > "term" : {
> > > > > 
> > > > > "bi" : "qa"
> > > > > 
> > > > > }
> > > > > 
> > > > > }
> > > > > 
> > > > > ```
> > > > > 
> > > > > }
> > > > > 
> > > > > }
> > > > > 
> > > > > The DSL corresponding is
> > > > > 
> > > > > QueryBuilder biBuilder = QueryBuilders.termQuery(
> > > > > 
> > > > > "bi", val);
> > > > > 
> > > > > QueryBuilder plBuilder = QueryBuilders.termQuery(
> > > > > 
> > > > > "pl", plval);
> > > > > 
> > > > > QueryBuilders.boolQuery()
> > > > > 
> > > > > .must(plBuilder)
> > > > > 
> > > > > .mustNot(biBuilder);
> > > > > 
> > > > > But in the results , I am getting records that has "bi" = "qa". I am  
> > > > > supposed to get results which match "pl=728x90" AND that doesnt have  
> > > > > "bi=qa". Please let me know if I am doing something wrong.
> > > > > 
> > > > > Thanks
> > > > > 
> > > > > G
> > > > > 
> > > > > --  
> > > > > 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).
> > > > > 
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > 
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > Sheeba Ann George
> > > 
> > > --  
> > > 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).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Sheeba Ann George
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![shgeorge](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@shgeorge](https://discuss.elastic.co/u/shgeorge)\
**Post date:** [April 6, 2013, 4:00am UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/7 "2013-04-06T04:00:23Z")

</div>

Thanks a lot, Ivan

Sent from my iPhone

On Apr 5, 2013, at 12:20 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Only filters are cached in Lucene since they are just bitsets. I have not used a filtered query, so I don't know if it cached, but if the API has a cache method, then it supports it.
> 
> --  
> Ivan
> 
> On Thu, Apr 4, 2013 at 9:17 AM, Sheeba George [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com) wrote:
> 
> > Thanks Ivan. When we set cachable as true, are the results of the filter also cached or just the filter query is cached. Can you please confirm.  
> > I am using Java DSL for filter query and had to wrap with a constant score (we dont need scoring) query to get the results. Does the below query look fine?  
> > {
> > 
> > "constant\_score" : {
> > 
> > ```
> > "filter" : {
> > 
> > "bool" : {
> > 
> > "must" : {
> > 
> > "term" : {
> > 
> > "p" : "120"
> > 
> > }
> > 
> > },
> > 
> > "must" : {
> > 
> > "terms" : {
> > 
> > "c" : ["gc"],
> > 
> > "execution" : "bool"
> > 
> > }
> > 
> > },
> > 
> > "must" : {
> > 
> > "term" : {
> > 
> > "b" : "bbb"
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > ```
> > 
> > }
> > 
> > }
> > 
> > On Thu, Apr 4, 2013 at 7:39 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > 
> > > If a string does not need to be tokenized, then not analyzing almost always makes sense.
> > > 
> > > The caches should be invalidated when a document is updated. You also have the option of turning off caching on a filter-by-filter basis.
> > > 
> > > --  
> > > Ivan
> > > 
> > > On Wed, Apr 3, 2013 at 11:50 AM, Sheeba George [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com) wrote:
> > > 
> > > > Hi Ivan  
> > > > Sorry for the delay. The issue turned out that we were using "." (eg : xyz.1) in the searchable fields . Looks like ES treats "." as special character and during tokenization it is replaced with space.  
> > > > I am thinking of using "not\_analyzed" for strings that needn't be tokenized. Do you recommend using cache with filters? In that case I assume we'll loose all the updates unless the expiry is too short .
> > > > 
> > > > Thanks  
> > > > George
> > > > 
> > > > On Wed, Mar 27, 2013 at 11:31 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > > > 
> > > > > Can you provide a few examples documents that are being matched but should not be? The mapping would also be helpful.
> > > > > 
> > > > > Term queries are not analyzed, so they will only match exactly (case sensitive) the token in the index. Also, if you do not need to have your items ranked/score, it would be more efficient to have that query as a filter instead.
> > > > > 
> > > > > --  
> > > > > Ivan
> > > > > 
> > > > > On Tue, Mar 26, 2013 at 11:27 AM, george [sheeba.george@gmail.com](mailto:sheeba.george@gmail.com) wrote:
> > > > > 
> > > > > > Hi all  
> > > > > > I need some help with Java DSL for Elastic search.
> > > > > > 
> > > > > > I have the below query:  
> > > > > > {
> > > > > > 
> > > > > > "bool" : {
> > > > > > 
> > > > > > ```
> > > > > > "must" : {
> > > > > > 
> > > > > > "term" : {
> > > > > > 
> > > > > > "pl" : "728x90"
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > },
> > > > > > 
> > > > > > "must_not" : {
> > > > > > 
> > > > > > "term" : {
> > > > > > 
> > > > > > "bi" : "qa"
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > The DSL corresponding is
> > > > > > 
> > > > > > QueryBuilder biBuilder = QueryBuilders.termQuery(
> > > > > > 
> > > > > > "bi", val);
> > > > > > 
> > > > > > QueryBuilder plBuilder = QueryBuilders.termQuery(
> > > > > > 
> > > > > > "pl", plval);
> > > > > > 
> > > > > > QueryBuilders.boolQuery()
> > > > > > 
> > > > > > .must(plBuilder)
> > > > > > 
> > > > > > .mustNot(biBuilder);
> > > > > > 
> > > > > > But in the results , I am getting records that has "bi" = "qa". I am supposed to get results which match "pl=728x90" AND that doesnt have "bi=qa". Please let me know if I am doing something wrong.
> > > > > > 
> > > > > > Thanks
> > > > > > 
> > > > > > G
> > > > > > 
> > > > > > --  
> > > > > > 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).
> > > > > > 
> > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > 
> > > > > --  
> > > > > You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> > > > > To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US).  
> > > > > To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > > 
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > 
> > > > ## -- Sheeba Ann George
> > > > 
> > > > 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).  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US).  
> > > To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > ## -- Sheeba Ann George
> > 
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/34DmI31cMlw/unsubscribe?hl=en-US).  
> To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:42am UTC](https://discuss.elastic.co/t/help-with-elastic-search-multi-clause-query/11323/8 "2017-07-06T02:42:30Z")

</div>


