5

I am trying to achieve this elasticsearch query in java querybuilder for elasticsearch. But i am not able to get the equivalent results. Can anyone help with this.

 GET /XX/XX/_search
    {
       "query": {
          "bool": {
             "must": [
                {
                   "nested": {
                      "path": "XX",
                      "filter": {
                         "term": {
                            "A": "7:140453136:T"
                         }
                      }
                   }
                },
                {
                   "nested": {
                      "path": "XX",
                      "filter": {
                         "term": {
                            "B": "RF"
                         }
                      }
                   }
                },
                {
                   "nested": {
                      "path": "XX",
                      "filter": {
                         "term": {
                            "C": "RFFF"
                         }
                      }
                   }
                }
             ]
          }
       }
    }

The code which i tried:

   QueryBuilders qbWithArguments = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("A", "RF"))
                    .must(QueryBuilders.termQuery("B", "EF"))
                    .must(QueryBuilders.termQuery("C", "RF"));

1 Answer 1

11

You need to add nested query too. Use below code:

QueryBuilders.boolQuery().must(nestedQuery("XX", FilterBuilders.termFilter("A","RF")))
                             .must(nestedQuery("XX", FilterBuilders.termFilter("B","EF")))
                             .must(nestedQuery("XX", FilterBuilders.termFilter("C","RF")))

For higher versions you can use :

QueryBuilders.boolQuery().must(nestedQuery("XX", QueryBuilders.boolQuery()
           .should(QueryBuilders.termQuery("A","RF"))
           .should(QueryBuilders.termQuery("B","EF"))
           .should(QueryBuilders.termQuery("C","RF"))
           .minimumShouldMatch("1")))

Notes: In 6.7, even if the doc stayed that ScoreMode is optional, you may need to provide this param when using the java QueryBuilders.

Sign up to request clarification or add additional context in comments.

7 Comments

nestedQuery() doesnt accept FilterBuilders as second argument. It expects QueryBuilder.
There is an overloaded method in QueryBuilders class which accepts filterbuilder as second argument. public static NestedQueryBuilder nestedQuery(String path, FilterBuilder filter) { return new NestedQueryBuilder(path, filter); }. Refer to rajish.github.io/api/elasticsearch/0.20.0.Beta1-SNAPSHOT/org/…
Thanks for the reference. But i am using ES 2.1.1 which is the latest and i dont see that overloaded method in the latest API doc.
FYI: org.elasticsearch.index.queries.FilterBuilders has been removed as part of the merge of queries and filters. These filters are now available in QueryBuilders with the same name. All methods that used to accept a FilterBuilder now accept a QueryBuilder instead.
Can you use QueryBuilders then? I have edited my answer according to that.
|

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.