Nested elasticsearch query for java QueryBuilders
Asked Answered
A

1

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"));
Ablation answered 29/1, 2016 at 0:43 Comment(0)
K
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.

Kalpak answered 29/1, 2016 at 2:54 Comment(7)
nestedQuery() doesnt accept FilterBuilders as second argument. It expects QueryBuilder.Ablation
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/…Kalpak
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.Ablation
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.Ablation
Can you use QueryBuilders then? I have edited my answer according to that.Kalpak
Thanks for the query! It works only if i provide all the three fields for search. But in my use case i want to see results even if i provide only one/two/three inputs. I mean to say if i provide one term for search the rest two will empty. but still i want to see results for the provided field. Should i replace must with should for this and add minimumShouldMatch to 1?Ablation
Yup that works as expected. Thanks a Ton. I am a new bee to ES :-) . Can you please mark this as the answer.Ablation

© 2022 - 2024 — McMap. All rights reserved.