# Bulding nested query with new Java API

**URL:** <https://discuss.elastic.co/t/bulding-nested-query-with-new-java-api/323949>\
**Category:** Elasticsearch\
**Created:** [January 25, 2023, 3:58pm UTC](https://discuss.elastic.co/t/bulding-nested-query-with-new-java-api/323949 "2023-01-25T15:58:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmermeister](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/schmermeister/32/116391_2.png) [@schmermeister](https://discuss.elastic.co/u/schmermeister)\
**Post date:** [January 25, 2023, 3:58pm UTC](https://discuss.elastic.co/t/bulding-nested-query-with-new-java-api/323949/1 "2023-01-25T15:58:50Z")

</div>

I try to achieve a query like this:

```auto
 {
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "metaData.cluster": "mobile"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "city.keyword": "London"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

into the new (8.1.1) Java API. I tried so far something like

```auto
BoolQuery bool_query = BoolQuery
				.of(b -> b.must(t -> t.match(r -> r.field("metaData.cluster").query("mobile")))
						.should(s -> s.match(t -> t.field("city.keyword").query("London"))));

```

But this results in a query whre the "should" is at the same level as the "must". The "should"-query must be underneath the "must" uery.

Any ideas?

---

<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:** [January 25, 2023, 4:29pm UTC](https://discuss.elastic.co/t/bulding-nested-query-with-new-java-api/323949/2 "2023-01-25T16:29:21Z")

</div>

Hi @schmermeister

Try this code:

```auto
BoolQuery bool_query = BoolQuery
        .of(b -> b
            .must(t -> t.match(r -> r.field("metaData.cluster").query("mobile")))
            .must(t -> t.bool(BoolQuery.of(bq -> bq.should(s -> s.match(m -> m.field("city.keyword").query("London")))))));

```

---

<div class="post-metadata">

**Author:** ![schmermeister](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/schmermeister/32/116391_2.png) [@schmermeister](https://discuss.elastic.co/u/schmermeister)\
**Post date:** [January 25, 2023, 4:55pm UTC](https://discuss.elastic.co/t/bulding-nested-query-with-new-java-api/323949/3 "2023-01-25T16:55:36Z")

</div>

Wow, phnatastic, works like a charm. Thank you very much.

---

<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:** [February 22, 2023, 4:56pm UTC](https://discuss.elastic.co/t/bulding-nested-query-with-new-java-api/323949/4 "2023-02-22T16:56:15Z")

</div>

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