Filter by with missing doc/fields in nested docs

Hi guys,

Is it possible to filter documents on missing docs/fields in nested documents?

Example
I have documents which looks like this (lastSessions is nested):

[{
	"id": "41853",
	"lastSessions": [{
		"guid": "0278A47B-4B16-4487-A797-5666BF2BA522",
		"periodId": 344,
		"state": "Started"
	}]
}, {
	"id": "41854",
	"lastSessions": [{
			"guid": "0278A47B-4B16-4487-A797-5666BF2BA522",
			"periodId": 344,
			"state": "Started"
		},
		{
			"guid": "0278A47B-4B16-4487-A797-5666BF2BA522",
			"periodId": 343,
			"state": "Abandoned"
		}
	]
}]

If I want to find all docs which has "last session state" set to "started" for period 344, I would do something like:

{
	"query": {
		"bool": {
			"must": [{
				"nested": {
					"path": "lastSessions",
					"query": {
						"bool": {
							"must": [{
									"term": {
										"periodId": {
											"value": 344
										}
									}
								},
								{
									"terms": {
										"lastSessions.state": [
											"Started"
										]
									}
								}
							]
						}
					}
				}
			}]
		}
	}
}

but... what if I want to find all docs which doesn't have any session for period 343 (i.e the document 41853) :slight_smile:? Is it possible?

Regards,
Anton