# ElasticSearch - Java layered search BoolQueryBuilder

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-layered-search-boolquerybuilder/328256>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 22, 2023, 2:00pm UTC](https://discuss.elastic.co/t/elasticsearch-java-layered-search-boolquerybuilder/328256 "2023-03-22T14:00:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sachin\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sachin_sharma/32/112079_2.png) [@Sachin\_Sharma](https://discuss.elastic.co/u/Sachin_Sharma)\
**Post date:** [March 22, 2023, 2:00pm UTC](https://discuss.elastic.co/t/elasticsearch-java-layered-search-boolquerybuilder/328256/1 "2023-03-22T14:00:49Z")

</div>

I have data in Elastic. Elastic data is as below.

- Name
- Parties

Parties is array of objects that contains partyCode and displayCode

```auto
{
	"query": {
		"bool": {
			"should": [
				{
					"bool": {
						"must": [
							{
								"wildcard": {
									"name.keyword": {
										"wildcard": "*HELLO*",
										"boost": 1
									}
								}
							},
							{
								"terms": {
									"parties.partyCode": [
										"org",
										"per"
									],
									"boost": 1
								}
							}
						],
						"must_not": [
							{
								"match": {
									"parties.displayCode": {
										"query": "NODSP",
										"operator": "OR",
										"prefix_length": 0,
										"max_expansions": 50,
										"fuzzy_transpositions": true,
										"lenient": false,
										"zero_terms_query": "NONE",
										"auto_generate_synonyms_phrase_query": true,
										"boost": 1
									}
								}
							}
						],
						"adjust_pure_negative": true,
						"boost": 1
					}
				},
				{
					"bool": {
						"must": [
							{
								"wildcard": {
									"name.keyword": {
										"wildcard": "*HELLO*",
										"boost": 1
									}
								}
							}
						],
						"must_not": [
							{
								"exists": {
									"field": "parties",
									"boost": 1
								}
							}
						],
						"adjust_pure_negative": true,
						"boost": 1
					}
				}
			]
		}
	},
	"size": 100,
	"from": 1600,
	"sort": []
}

```

I want to search for (name (contains) and parties.partyCode and parties.displayCode) or (name (contains) and parties is null). As of right now I want to stack the two bool queries in should query. How can I do this in Java using the BoolQueryBuilder?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [March 23, 2023, 9:21am UTC](https://discuss.elastic.co/t/elasticsearch-java-layered-search-boolquerybuilder/328256/2 "2023-03-23T09:21:50Z")

</div>

Hi @Sachin_Sharma,

Since your question is regarding the Java client, I've added the `language-clients` tag to ensure it's visible to the language client community.

Hope that helps!

---

<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:** [April 20, 2023, 9:22am UTC](https://discuss.elastic.co/t/elasticsearch-java-layered-search-boolquerybuilder/328256/3 "2023-04-20T09:22:25Z")

</div>

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