# Elastic Search nested data type query

**URL:** <https://discuss.elastic.co/t/elastic-search-nested-data-type-query/237773>\
**Category:** Elasticsearch\
**Created:** [June 19, 2020, 9:23am UTC](https://discuss.elastic.co/t/elastic-search-nested-data-type-query/237773 "2020-06-19T09:23:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [June 19, 2020, 9:23am UTC](https://discuss.elastic.co/t/elastic-search-nested-data-type-query/237773/1 "2020-06-19T09:23:17Z")

</div>

Hi,  
I have created the index in elastic search with the following mapping:

```
PUT test
{
	"mappings": {
		"documents": {
			"properties": {
				"title": {
					"type": "text",
					"fields": {
						"raw": {
							"type": "keyword"
						}
					}
				},
				"publish_details": {
					"type": "nested",
					"properties": {
						"environment": {
							"type": "keyword"
						},
						"locale": {
							"type": "keyword"
						},
						"time": {
							"type": "date"
						},
						"version": {
							"type": "integer"
						}
					}
				}
			}
		}
	}
}

```

and added documents into it. here is the list of documents:

```
    [{
	"publish_details": [{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:46:15.433Z",
			"locale": "hi-in",
			"version": 1
		},
		{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:46:15.433Z",
			"locale": "en-us",
			"version": 1
		}, {
			"environment": "blt9f5867a332cbb2576",
			"time": "2020-06-19T05:46:15.433Z",
			"locale": "en-us",
			"version": 1
		}
	],
	"title": "Entry 1"
}, {
	"publish_details": [{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:46:53.508Z",
			"locale": "mr-in",
			"version": 1
		},
		{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:46:53.508Z",
			"locale": "en-us",
			"version": 1
		}
	],
	"title": "Entry 2"
}, {
	"publish_details": [{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:48:24.742Z",
			"locale": "mr-in",
			"version": 1
		},
		{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:48:24.742Z",
			"locale": "hi-in",
			"version": 1
		}
	],
	"title": "Entry 3"
}, {
	"publish_details": [{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:48:57.936Z",
			"locale": "mr-in",
			"version": 1
		},
		{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:48:57.936Z",
			"locale": "hi-in",
			"version": 1
		},
		{
			"environment": "blt9f5867a332cbb250",
			"time": "2020-06-19T05:48:57.936Z",
			"locale": "en-us",
			"version": 1
		}
	],
	"title": "Entry 4"
}, {
	"publish_details": [],
	"title": "Entry 5"
}]

```

and I want the following result

```
[{
		"publish_details": [{
				"environment": "blt9f5867a332cbb250",
				"time": "2020-06-19T05:46:15.433Z",
				"locale": "hi-in",
				"version": 1
			},
			{
				"environment": "blt9f5867a332cbb250",
				"time": "2020-06-19T05:46:15.433Z",
				"locale": "en-us",
				"version": 1
			}, {
				"environment": "blt9f5867a332cbb2576",
				"time": "2020-06-19T05:46:15.433Z",
				"locale": "en-us",
				"version": 1
			}
		],
		"title": "Entry 1"
	}, {
		"publish_details": [{
				"environment": "blt9f5867a332cbb250",
				"time": "2020-06-19T05:46:53.508Z",
				"locale": "mr-in",
				"version": 1
			},
			{
				"environment": "blt9f5867a332cbb250",
				"time": "2020-06-19T05:46:53.508Z",
				"locale": "en-us",
				"version": 1
			}
		],
		"title": "Entry 2"
	}, {
		"publish_details": [{
				"environment": "blt9f5867a332cbb250",
				"time": "2020-06-19T05:48:24.742Z",
				"locale": "mr-in",
				"version": 1
			},
			{
				"environment": "blt9f5867a332cbb250",
				"time": "2020-06-19T05:48:24.742Z",
				"locale": "hi-in",
				"version": 1
			}
		],
		"title": "Entry 3"
	},
	{
		"publish_details": [],
		"title": "Entry 5"
	}
]

```

So How I can achieve this. want to filter nested data type documents, so What query I used here to get a proper result (for the nested data type). Kindly help me with this.

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [June 19, 2020, 5:07pm UTC](https://discuss.elastic.co/t/elastic-search-nested-data-type-query/237773/2 "2020-06-19T17:07:12Z")

</div>

@surajdalvi  
Are you trying to filter document with title "Entry 4"? If yes try this

```auto
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "title.raw": "Entry 4"
              }
            }
          ]
        }
      }
    }
  }
}

```

---

<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:** [July 17, 2020, 5:07pm UTC](https://discuss.elastic.co/t/elastic-search-nested-data-type-query/237773/3 "2020-07-17T17:07:23Z")

</div>

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