How to do multiple filter query in Elasticsearch using Java?

Rose picture Rose · Jun 27, 2016 · Viewed 11.7k times · Source

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.

Answer

Val picture Val · Jun 27, 2016

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"));