Filtered Query that includes ANDs and ORs using the Java API


(pmac) #1

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


(Shay Banon) #2

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 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


(pmac) #3

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 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 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


(Shay Banon) #4

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 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 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 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


(pmac) #5

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 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 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 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 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


(system) #6