0

I need to apply an "OR" filter, which would be equivalent to category in ('a' or 'b' or 'c' or 'd' or 'e') an equivalent ES query is provided below

"query_string": {
                      "query": "category: (\"a\", \"b\", \"c\", \"d\", \"e\")"
                   }

I have this below mentioned JAVA ES API code and I am struck in adding OR filter in Java. Can somebody help please?

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
     searchSourceBuilder.query(QueryBuilders.boolQuery()
                .must(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
                        FilterBuilders.termFilter("category","a")))
                .must(QueryBuilders.rangeQuery("@timestamp").from(fromHr).to(toHr)))
                .fields(fieldList);

1 Answer 1

1

Based on your example, I assume you are using ES 1.x and assume the RangeQuery should be a query. If I understand your question correctly you want do something like this:

QueryBuilders.filteredQuery(
 QueryBuilders.rangeQuery("@timestamp").from(fromHr).to(toHr),
 FilterBuilders.boolFilter()
  .should(FilterBuilders.termFilter("category","a"))
  .should(FilterBuilders.termFilter("category","b")) 
  .should(FilterBuilders.termFilter("category","c")) 
  .should(FilterBuilders.termFilter("category","d"))   
  .should(FilterBuilders.termFilter("category","e"))
)

However, you can also use the terms filter:

QueryBuilders.filteredQuery(
  QueryBuilders.rangeQuery("@timestamp").from(fromHr).to(toHr),
  FilterBuilders.termsFilter("category", "a", "b", "c", "d", "e", "f" ))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Micheal !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.