# QueryBuilders.nested() in new Java REST Client works not as expected (no "nested" attribute generated), comparing to older (deprecated) HRC

**URL:** <https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099>\
**Category:** Elasticsearch\
**Created:** [April 1, 2023, 10:17am UTC](https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099 "2023-04-01T10:17:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattteo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattteo/32/60803_2.png) [@Mattteo](https://discuss.elastic.co/u/Mattteo)\
**Post date:** [April 1, 2023, 10:17am UTC](https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099/1 "2023-04-01T10:17:51Z")

</div>

The problem: I am trying to upgrade from deprecated HRC (7.13) to new REST Client 8.6 in Java.  
We use nested queries, but although there is a special **NestedQuery.Builder** object in the new java client, its not possible to construct the same object as with an old one client.

Old HRC Java code:

> BoolQueryBuilder innerQueryBuilder = ...;  
> QueryBuilders.nestedQuery("Addresses", innerQueryBuilder, ScoreMode.None);

It produces following JSON request:

```auto
{
  "query": {
    "nested": {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "Addresses.Town": {
                  "query": "Frankfurt",
                  "operator": "AND"
                }
              }
            },
            {
              "match": {
                "Addresses.Street": {
                  "query": "Theodor",
                  "operator": "AND"
                }
              }
            }
          ]
        }
      },
      "path": "Addresses"
    }
  }
}

```

But if try to get the same result with the new one client - it makes no "nested" wrapping at all, although it adds necessary top level attribute name using "."

> Query q = QueryBuilders.nested()  
> .path("Addresses")  
> .query(innerQueryBuilder.build().\_toQuery())  
> .scoreMode(ChildScoreMode.None)  
> .build()  
> .query();

The result is incomplete and according [current documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) not correct:

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "Addresses.Town": {
                            "operator": "and",
                            "query": "Frankfurt"
                        }
                    }
                },
                {
                    "match": {
                        "Addresses.Street": {
                            "operator": "and",
                            "query": "Theodor"
                        }
                    }
                }
            ]
        }
    }
}

```

So am I doing something wrong or I miss something? How may I make the query really "nested" as before?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 1, 2023, 3:28pm UTC](https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099/2 "2023-04-01T15:28:23Z")

</div>

Hi @Mattteo

Try to use NestedQuery. Example:

```
var queryType = MatchQuery.of(ts -> ts.field("Addresses.Town").query("Frankfurt").operator(Operator.And))._toQuery();

var queryStreet = MatchQuery.of(ts -> ts.field("Addresses.Street").query("Theodor").operator(Operator.And))._toQuery();

var innerQueryBuilder = BoolQuery.of(bq -> bq.filter(queryType, queryStreet));

Query q = NestedQuery.of(n -> n.path("Address").query(innerQueryBuilder._toQuery()).scoreMode(ChildScoreMode.None))._toQuery();

var request = SearchRequest.of(s -> s.query(q));

```

---

<div class="post-metadata">

**Author:** ![Mattteo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattteo/32/60803_2.png) [@Mattteo](https://discuss.elastic.co/u/Mattteo)\
**Post date:** [April 1, 2023, 7:17pm UTC](https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099/3 "2023-04-01T19:17:07Z")

</div>

Thanks, your example makes nested query, but please pay attention that you're using **"Addresses.Street"** full qualified attribute name directly. So in your example you should concatenate nested path with an attribute name _manually_, but its getting wrapped under "nested". In my example I do not get this wrapping, but get correct attribute concatenation. How may I get it both? The old one HLRC did not have such a problem at all, just "one-liner".

By the way - the old one client was awaiting QueryBuilder as a parameter, that was a super idea as you may easily create nested and/or boolean queries. The new one operates with complete queries only. And new Builders don't support cloning or something like .toBuilder(). Ho it is supposed to be used then?

---

<div class="post-metadata">

**Author:** ![Mattteo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattteo/32/60803_2.png) [@Mattteo](https://discuss.elastic.co/u/Mattteo)\
**Post date:** [April 3, 2023, 11:31am UTC](https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099/4 "2023-04-03T11:31:51Z")

</div>

For anyone who probably has the same problem:  
it is necessary to use everywhere the method **\_toQuery()** as it was shown above. Otherwise, if you use just _.query()_, it may produce incomplete result as in my first post. For me it was not obvious.

@RabBit_BR  
thanks again for your complete example, it helped to understand where the problem is.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 1, 2023, 11:32am UTC](https://discuss.elastic.co/t/querybuilders-nested-in-new-java-rest-client-works-not-as-expected-no-nested-attribute-generated-comparing-to-older-deprecated-hrc/329099/5 "2023-05-01T11:32:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
