# Filtered Query that includes ANDs and ORs using the Java API

**URL:** https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065
**Category:** Elasticsearch
**Created:** [March 20, 2012, 12:47am UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065 "2012-03-20T00:47:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pmac](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@pmac](https://discuss.elastic.co/u/pmac)
#### Post date: [March 20, 2012, 12:47am UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065/1 "2012-03-20T00:47:24Z")

</div>

First, I want to thank you for this list - I'm new to ES and it's been  
extremely helpful.  
I've looked through all the articles I can find on filters and have  
read the ES guide too.  
I still can't figure out how to create a filtered query that includes  
ANDs and ORs using the Java API.  
I am trying to do this;  
QueryString("a text phrase")  
filters;  
and (A and B)  
or(C and not D)

I can filter my QueryString with the AND filter, but don't see how to  
add the OR filter;

```
QueryBuilder qb = QueryBuilders.queryString("a text phrase")
           .defaultOperator(Operator.AND)
           .field("textField-1").field("textField-2");

AndFilterBuilder andFilter = FilterBuilders.andFilter();
andFilter.add(FilterBuilders.termFilter("textField-3", "a quick

```

brown fox"))  
.add(FilterBuilders.termFilter("code-1", "123"));

```
OrFilterBuilder orFilter = FilterBuilders.orFilter();
orFilter.add(FilterBuilders.termFilter("textField-3", "jumps over

```

the lazy dog"));

```
SearchResponse searchResponse = client.prepareSearch("indexName")
       .setTypes("indexType")
       .setSearchType(SearchType.QUERY_THEN_FETCH)
       .setQuery(qb)
       .setFilter(andFilter)
       .setFrom(0).setSize(100).setExplain(true)
       .execute()
       .actionGet();

```

Thanks again,  
Paul

---

<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: [March 20, 2012, 10:52am UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065/2 "2012-03-20T10:52:26Z")

</div>

Whats the relationship between the and and or filters you have? or?

On Tue, Mar 20, 2012 at 2:47 AM, pmac [pmcclure07@gmail.com](mailto:pmcclure07@gmail.com) wrote:

> First, I want to thank you for this list - I'm new to ES and it's been  
> extremely helpful.  
> I've looked through all the articles I can find on filters and have  
> read the ES guide too.  
> I still can't figure out how to create a filtered query that includes  
> ANDs and ORs using the Java API.  
> I am trying to do this;  
> QueryString("a text phrase")  
> filters;  
> and (A and B)  
> or(C and not D)
> 
> I can filter my QueryString with the AND filter, but don't see how to  
> add the OR filter;
> 
> QueryBuilder qb = QueryBuilders.queryString("a text phrase")  
> .defaultOperator(Operator.AND)  
> .field("textField-1").field("textField-2");
> 
> AndFilterBuilder andFilter = FilterBuilders.andFilter();  
> andFilter.add(FilterBuilders.termFilter("textField-3", "a quick  
> brown fox"))  
> .add(FilterBuilders.termFilter("code-1", "123"));
> 
> OrFilterBuilder orFilter = FilterBuilders.orFilter();  
> orFilter.add(FilterBuilders.termFilter("textField-3", "jumps over  
> the lazy dog"));
> 
> SearchResponse searchResponse = client.prepareSearch("indexName")  
> .setTypes("indexType")  
> .setSearchType(SearchType.QUERY\_THEN\_FETCH)  
> .setQuery(qb)  
> .setFilter(andFilter)  
> .setFrom(0).setSize(100).setExplain(true)  
> .execute()  
> .actionGet();
> 
> Thanks again,  
> Paul

---

<div class="post-metadata">

### Author: ![pmac](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@pmac](https://discuss.elastic.co/u/pmac)
#### Post date: [March 20, 2012, 3:32pm UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065/3 "2012-03-20T15:32:52Z")

</div>

Thanks Shay,  
My index has this structure (1-N docs of type BodyPart);

```
BodyPart {
    title-1,
    title-2,
    description,
    code-1,
    code-2
}
...

```

In my query, I need to find a given text phrase in title-1 or title-2  
in a BodyPart (I'm using QueryBuilders.queryString() for this).

Then, I need the ability to say;  
and (description = "something" and code-1 = "123")  
or (description = "something else" and code-2 not "456")

This would allow me to hit on zero or more BodyPart docs.

Thanks,  
Paul

On Mar 20, 6:52 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Whats the relationship between the and and or filters you have? or?
> 
> On Tue, Mar 20, 2012 at 2:47 AM, pmac [pmcclur...@gmail.com](mailto:pmcclur...@gmail.com) wrote:
> 
> > First, I want to thank you for this list - I'm new to ES and it's been  
> > extremely helpful.  
> > I've looked through all the articles I can find on filters and have  
> > read the ES guide too.  
> > I still can't figure out how to create a filtered query that includes  
> > ANDs and ORs using the Java API.  
> > I am trying to do this;  
> > QueryString("a text phrase")  
> > filters;  
> > and (A and B)  
> > or(C and not D)
> 
> > I can filter my QueryString with the AND filter, but don't see how to  
> > add the OR filter;
> 
> > QueryBuilder qb = QueryBuilders.queryString("a text phrase")  
> > .defaultOperator(Operator.AND)  
> > .field("textField-1").field("textField-2");
> 
> > AndFilterBuilder andFilter = FilterBuilders.andFilter();  
> > andFilter.add(FilterBuilders.termFilter("textField-3", "a quick  
> > brown fox"))  
> > .add(FilterBuilders.termFilter("code-1", "123"));
> 
> > OrFilterBuilder orFilter = FilterBuilders.orFilter();  
> > orFilter.add(FilterBuilders.termFilter("textField-3", "jumps over  
> > the lazy dog"));
> 
> > SearchResponse searchResponse = client.prepareSearch("indexName")  
> > .setTypes("indexType")  
> > .setSearchType(SearchType.QUERY\_THEN\_FETCH)  
> > .setQuery(qb)  
> > .setFilter(andFilter)  
> > .setFrom(0).setSize(100).setExplain(true)  
> > .execute()  
> > .actionGet();
> 
> > Thanks again,  
> > Paul

---

<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: [March 20, 2012, 8:50pm UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065/4 "2012-03-20T20:50:04Z")

</div>

What I meant is that you have a "and" part and an "or" part, but what is  
the relatiobship between the two. You can wrap them in "or" / "and" as well  
to create a compound option.

On Tue, Mar 20, 2012 at 5:32 PM, pmac [pmcclure07@gmail.com](mailto:pmcclure07@gmail.com) wrote:

> Thanks Shay,  
> My index has this structure (1-N docs of type BodyPart);
> 
> BodyPart {  
> title-1,  
> title-2,  
> description,  
> code-1,  
> code-2  
> }  
> ...
> 
> In my query, I need to find a given text phrase in title-1 or title-2  
> in a BodyPart (I'm using QueryBuilders.queryString() for this).
> 
> Then, I need the ability to say;  
> and (description = "something" and code-1 = "123")  
> or (description = "something else" and code-2 not "456")
> 
> This would allow me to hit on zero or more BodyPart docs.
> 
> Thanks,  
> Paul
> 
> On Mar 20, 6:52 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > Whats the relationship between the and and or filters you have? or?
> > 
> > On Tue, Mar 20, 2012 at 2:47 AM, pmac [pmcclur...@gmail.com](mailto:pmcclur...@gmail.com) wrote:
> > 
> > > First, I want to thank you for this list - I'm new to ES and it's been  
> > > extremely helpful.  
> > > I've looked through all the articles I can find on filters and have  
> > > read the ES guide too.  
> > > I still can't figure out how to create a filtered query that includes  
> > > ANDs and ORs using the Java API.  
> > > I am trying to do this;  
> > > QueryString("a text phrase")  
> > > filters;  
> > > and (A and B)  
> > > or(C and not D)
> > 
> > > I can filter my QueryString with the AND filter, but don't see how to  
> > > add the OR filter;
> > 
> > > QueryBuilder qb = QueryBuilders.queryString("a text phrase")  
> > > .defaultOperator(Operator.AND)  
> > > .field("textField-1").field("textField-2");
> > 
> > > AndFilterBuilder andFilter = FilterBuilders.andFilter();  
> > > andFilter.add(FilterBuilders.termFilter("textField-3", "a quick  
> > > brown fox"))  
> > > .add(FilterBuilders.termFilter("code-1", "123"));
> > 
> > > OrFilterBuilder orFilter = FilterBuilders.orFilter();  
> > > orFilter.add(FilterBuilders.termFilter("textField-3", "jumps over  
> > > the lazy dog"));
> > 
> > > SearchResponse searchResponse = client.prepareSearch("indexName")  
> > > .setTypes("indexType")  
> > > .setSearchType(SearchType.QUERY\_THEN\_FETCH)  
> > > .setQuery(qb)  
> > > .setFilter(andFilter)  
> > > .setFrom(0).setSize(100).setExplain(true)  
> > > .execute()  
> > > .actionGet();
> > 
> > > Thanks again,  
> > > Paul

---

<div class="post-metadata">

### Author: ![pmac](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@pmac](https://discuss.elastic.co/u/pmac)
#### Post date: [March 21, 2012, 12:31am UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065/5 "2012-03-21T00:31:07Z")

</div>

I see,  
I wrapped my AND filters in my OR filters and that worked...

Thanks very much Shay

On Mar 20, 4:50 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> What I meant is that you have a "and" part and an "or" part, but what is  
> the relatiobship between the two. You can wrap them in "or" / "and" as well  
> to create a compound option.
> 
> On Tue, Mar 20, 2012 at 5:32 PM, pmac [pmcclur...@gmail.com](mailto:pmcclur...@gmail.com) wrote:
> 
> > Thanks Shay,  
> > My index has this structure (1-N docs of type BodyPart);
> 
> > BodyPart {  
> > title-1,  
> > title-2,  
> > description,  
> > code-1,  
> > code-2  
> > }  
> > ...
> 
> > In my query, I need to find a given text phrase in title-1 or title-2  
> > in a BodyPart (I'm using QueryBuilders.queryString() for this).
> 
> > Then, I need the ability to say;  
> > and (description = "something" and code-1 = "123")  
> > or (description = "something else" and code-2 not "456")
> 
> > This would allow me to hit on zero or more BodyPart docs.
> 
> > Thanks,  
> > Paul
> 
> > On Mar 20, 6:52 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> > 
> > > Whats the relationship between the and and or filters you have? or?
> 
> > > On Tue, Mar 20, 2012 at 2:47 AM, pmac [pmcclur...@gmail.com](mailto:pmcclur...@gmail.com) wrote:
> > > 
> > > > First, I want to thank you for this list - I'm new to ES and it's been  
> > > > extremely helpful.  
> > > > I've looked through all the articles I can find on filters and have  
> > > > read the ES guide too.  
> > > > I still can't figure out how to create a filtered query that includes  
> > > > ANDs and ORs using the Java API.  
> > > > I am trying to do this;  
> > > > QueryString("a text phrase")  
> > > > filters;  
> > > > and (A and B)  
> > > > or(C and not D)
> 
> > > > I can filter my QueryString with the AND filter, but don't see how to  
> > > > add the OR filter;
> 
> > > > QueryBuilder qb = QueryBuilders.queryString("a text phrase")  
> > > > .defaultOperator(Operator.AND)  
> > > > .field("textField-1").field("textField-2");
> 
> > > > AndFilterBuilder andFilter = FilterBuilders.andFilter();  
> > > > andFilter.add(FilterBuilders.termFilter("textField-3", "a quick  
> > > > brown fox"))  
> > > > .add(FilterBuilders.termFilter("code-1", "123"));
> 
> > > > OrFilterBuilder orFilter = FilterBuilders.orFilter();  
> > > > orFilter.add(FilterBuilders.termFilter("textField-3", "jumps over  
> > > > the lazy dog"));
> 
> > > > SearchResponse searchResponse = client.prepareSearch("indexName")  
> > > > .setTypes("indexType")  
> > > > .setSearchType(SearchType.QUERY\_THEN\_FETCH)  
> > > > .setQuery(qb)  
> > > > .setFilter(andFilter)  
> > > > .setFrom(0).setSize(100).setExplain(true)  
> > > > .execute()  
> > > > .actionGet();
> 
> > > > Thanks again,  
> > > > Paul

---

<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, 3:35am UTC](https://discuss.elastic.co/t/filtered-query-that-includes-ands-and-ors-using-the-java-api/7065/6 "2017-07-06T03:35:20Z")

</div>


