# The doucment is returned even if it does not meet the criteria

**URL:** <https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145>\
**Category:** Elasticsearch\
**Created:** [February 5, 2025, 7:22pm UTC](https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145 "2025-02-05T19:22:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinitp](https://avatars.discourse-cdn.com/v4/letter/v/46a35a/32.png) [@vinitp](https://discuss.elastic.co/u/vinitp)\
**Post date:** [February 5, 2025, 7:22pm UTC](https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145/1 "2025-02-05T19:22:04Z")

</div>

I use the following DSL query

```
"size": 25,
"query": {
	"bool": {
		"must": [
			{
				"term": {
					"hcAccountabilityOfficeCode.keyword": {
						"value": "550"
					}
				}
			},
			{
				"nested": {
					"query": {
						"term": {
							"hearingActionDetails.haaActionTypeCode.keyword": {
								"value": "SU"
							}
						}
					},
					"path": "hearingActionDetails",
					"ignore_unmapped": false,
					"score_mode": "none",
					"boost": 1.0
				}
			},
			{
				"nested": {
					"query": {
						"term": {
							"hearingActionDetails.haaActionCatCode.keyword": {
								"value": "A"
							}
						}
					},
					"path": "hearingActionDetails",
					"ignore_unmapped": false,
					"score_mode": "none",
					"boost": 1.0
				}
			},
			{
				"bool": {
					"must_not": [
						{
							"term": {
								"hcStatusCode.keyword": {
									"value": "STS54"
								}
							}
						}
					],
					"boost": 1.0
				}
			},
			{
				"nested": {
					"query": {
						"exists": {
							"field": "hearingActionDetails.haaReceivedOn",
							"boost": 1.0
						}
					},
					"path": "hearingActionDetails",
					"ignore_unmapped": false,
					"score_mode": "none",
					"boost": 1.0
				}
			},
			{
				"nested": {
					"query": {
						"bool": {
							"must_not": [
								{
									"exists": {
										"field": "hearingActionDetails.haaPartyRespRcvdOn",
										"boost": 1.0
									}
								}
							],
							"boost": 1.0
						}
					},
					"path": "hearingActionDetails",
					"ignore_unmapped": false,
					"score_mode": "none",
					"boost": 1.0
				}
			},
			{
				"nested": {
					"query": {
						"bool": {
							"must_not": [
								{
									"exists": {
										"field": "hearingActionDetails.haaPartyReplyRcvdOn",
										"boost": 1.0
									}
								}
							],
							"boost": 1.0
						}
					},
					"path": "hearingActionDetails",
					"ignore_unmapped": false,
					"score_mode": "none",
					"boost": 1.0
				}
			}
		],
		"boost": 1.0
	}
}

```

The following document is returned even though it does not meet the criteria specified in the query.  
The query clearly defined for "must\_not" for "hearingActionDetails.haaPartyRespRcvdOn" and "hearingActionDetails.haaPartyReplyRcvdOn"

```auto
{
	"hcHearingCaseId": 274,
	"hcCaseNumber": "550-2025-00005",
	"hcStatusCode": "STS11",
	"hcAccountabilityOfficeCode": "550",
	"hcAccountabilityOfficeDescription": "San Francisco District Office",
	"hearingActionDetails": [
		{
			"haaHearingActionId": 374,
			"haaActionCatId": 954,
			"haaActionCatCode": "A",
			"haaActionCatDesc": "Motions",
			"haaActionTypeId": 920,
			"haaActionTypeCode": "SU",
			"haaActionTypeDesc": "Summary Judgement",
			"haaActionStatusId": 971,
			"haaActionStatusCode": "C",
			"haaActionStatusDescription": "Granted in Full for Complainant",
			"haaReqPartyTypeId": 927,
			"haaReqPartyTypeCode": "C",
			"haaReqPartyTypeDesc": "Complainant",
			"haaSurReplyStatusId": null,
			"haaSurReplyStatusCode": null,
			"haaSurReplyStatusDescription": null,
			"haaSurReplySubById": null,
			"haaSurReplySubByCode": null,
			"haaSurReplyReqByDescription": null,
			"haaReceivedOn": "01-03-2025",
			"haaPartyRespDueSetOn": null,
			"haaPartyRespDueOn": "01-21-2025",
			"haaPartyRespRcvdOn": "01-03-2025",
			"haaPartyReplyDueOn": "01-08-2025",
			"haaPartyReplyRcvdOn": "01-03-2025",
			"haaRespRcvdFrmAgcyOn": null,
			"haaRespRcvdFrmCompOn": null,
			"haaSurReplySubOn": null,
			"haaSurReplyRespDueOn": null,
			"haaSurReplyRespRcvdOn": null,
			"haaOrderDueOn": null,
			"haaIssuedOn": null,
			"haaRespRecdFrmAgcyOn": null,
			"haaRespRecdFrmCompOn": null,
			"hacCreatedOn": "01-03-2025",
			"hacMotionSurReplyRecdOn": null,
			"hacSurReplyStatusUpdatedOn": null
		}
	],
	"hcCaseAssigned": "N"
}

```

---

<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:** [February 6, 2025, 12:19am UTC](https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145/2 "2025-02-06T00:19:18Z")

</div>

Hi @vinitp

With [named query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#named-queries) you can find out which clause hás match.

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [February 6, 2025, 7:15am UTC](https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145/3 "2025-02-06T07:15:40Z")

</div>

@RabBit_BR I have tested this with a nested mapping and I have received back no hits. 🤔

Can you please share your index mapping, and the result for the [explain API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html) on your retrieved doc and query?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [February 6, 2025, 8:23am UTC](https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145/4 "2025-02-06T08:23:55Z")

</div>

I do not see why this document would be returned. Have you by any chance altered it so we are not seeing the full document, e.g. by trimming the `hearingActionDetails` list to only contain the nested document you are focused on?

As you have several separate nested clauses each of these can be fulfilled by different objects in the `hearningActionDetails` array, causing the document to be returned. If you are looking for a nested document that fulfills all these nested criteria I believe you need to have a single nested clause with a bool query underneath holding the applicable criteria.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [February 6, 2025, 9:47pm UTC](https://discuss.elastic.co/t/the-doucment-is-returned-even-if-it-does-not-meet-the-criteria/374145/5 "2025-02-06T21:47:45Z")

</div>

See similar thread, where the actual query in that thread is generated by a call to /sql/translate - @vinitp I would be curious if same is true here?

Opening multiple threads on effectively the same topic is, er, a little irritating.

> [@Must\_not does not work in the nested array](https://discuss.elastic.co/t/must-not-does-not-work-in-the-nested-array/374191):
>
> I have an application that generates Elasticsearch SQL dynamically based on the client's search criteria. The application then use the "/sql/translate" API to translate to the DSL query which it executes. The sample of the SQL looks like this: "select \* from hearing\_alias where hcAccountabilityOfficeCode = '550' AND hearingActionDetails.haaActionTypeCode = 'SU' AND hearingActionDetails.haaActionCatCode = 'A' AND hcStatusCode \<\> 'STS54' AND hearingActionDetails.haaPartyRespRcvdOn is null AND…
