How to do multiple filter query in Elasticsearch using Java?
Asked Answered
B

2

7

I have build a web app on top of elasticsearch. I would like to do a multiple filter using Java.

Elasticsearch Query:

{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "T": "TEXT"},
          "match": {
            "new_content": "TEXT"
          }
        },

      ],
      "filter": {
        "term": {
          "collection1": "xyz"
        },
        "term": {
          "collection2": "abc"
        }

I want to filter the query such that it should filter on the same field collection with two different values(for eg,"xyz" and "abc")

Right now, I have coded a Java program for the single filter.

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.simpleQueryStringQuery(query).field("newContent").field("T"))
                .filter(QueryBuilders.termQuery(Collection1, "abc"));

How should I filter a query on a same field for multiple values?

Edit:

Actually in my web app, I have extracted the value of the collection as:

String[] Collection=request.getParameterValues("site");

Collection is the field in my elasticsearch documents. Suppose Collection[0] is germany_collection(value:true), Collection[1] is usa_collection(value:true). I want to write a query which filters my documents such that I will get results when germany_collection:true and usa_collection:true.

This is my original program:

public StringBuffer getJson(String query, String start, String [] Collection{
Client client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.simpleQueryStringQuery(query).field("newContent").field("T"))
                .filter(QueryBuilders.termQuery(Collection[0], "true").filter(QueryBuilders.termQuery(Collection[1], "true");

But when I did applied this filter, I am getting this error The method filter(TermsQueryBuilder) is undefined for the type TermsQueryBuilder. It says add cast to method reciever.

Blowpipe answered 27/6, 2016 at 20:19 Comment(0)
I
11

You can use the terms query instead of multiple term queries:

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.simpleQueryStringQuery(query).field("newContent").field("T"))
                .filter(QueryBuilders.termsQuery(Collection, "abc", "xyz"));
                                          ^                    ^      ^
                                          |                    |      |
                                    use termsQuery      with multiple values

UPDATE

I'm surprised your code compiles at all because you're missing some closing parenthesis, just rewrite your query like this and it should work:

    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
            .must(QueryBuilders.simpleQueryStringQuery(query).field("newContent").field("T"))
            .filter(QueryBuilders.termQuery(Collection[0], "true"))
            .filter(QueryBuilders.termQuery(Collection[1], "true"));
Ilex answered 27/6, 2016 at 21:7 Comment(2)
Sorry. I messed up in asking a proper question. Can you please check my edit part?Blowpipe
It worked. Thanks. There was an error because of the missing closing parenthesis. Thanks againBlowpipe
T
0

Term query will take only one value for the filed, Terms query will take multiple values for the filed

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder QueryBuilders = QueryBuilders.boolQuery();

sourceBuilder.query(QueryBuilders.filter(QueryBuilders.termsQuery("age", "44", "45", "30")));

strong text searchRequest.source(sourceBuilder);

Threephase answered 11/3, 2020 at 14:7 Comment(2)
How to go if one cannot hard code the values ? Say if having a collection with the values.Squirearchy
Probably like QueryBuilders.termsQuery("name", Arrays.asList(values))Squirearchy

© 2022 - 2025 — McMap. All rights reserved.