ElasticSearch - Java layered search BoolQueryBuilder

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

  • Name
  • Parties

Parties is array of objects that contains partyCode and displayCode

{
	"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?

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!