# Query for missing fields in nested documents

**URL:** <https://discuss.elastic.co/t/query-for-missing-fields-in-nested-documents/11834>\
**Category:** Elasticsearch\
**Created:** [May 6, 2013, 1:08pm UTC](https://discuss.elastic.co/t/query-for-missing-fields-in-nested-documents/11834 "2013-05-06T13:08:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)\
**Post date:** [May 6, 2013, 1:08pm UTC](https://discuss.elastic.co/t/query-for-missing-fields-in-nested-documents/11834/1 "2013-05-06T13:08:03Z")

</div>

I have a user document which contains many tags  
Here is the mapping:  
{  
"user" : {  
"properties" : {  
"tags" : {  
"type" : "nested",  
"properties" : {  
"id" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"store" : "yes"  
},  
"current" : {  
"type" : "boolean"  
},  
"type" : {  
"type" : "string"  
},  
"value" : {  
"type" : "multi\_field",  
"fields" : {  
"value" : {  
"type" : "string",  
"analyzer" : "name\_analyzer"  
},  
"value\_untouched" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"include\_in\_all" : false  
}  
}  
}  
}  
}  
}  
}  
}  
Here are the sample user documents  
User 1:

{  
"created\_at": 1317484762000,  
"updated\_at": 1367040856000,  
"tags": [  
{  
"type": "college",  
"value": "Dhirubhai Ambani Institute of Information and  
Communication Technology",  
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361829"  
},  
{  
"type": "company",  
"value": "alma connect",  
"id": "58ad4afcc8415216ea451339aaecf311ed40e132"  
},  
{  
"type": "company",  
"value": "Google",  
"id": "93bc8199c5fe7adfd181d59e7182c73fec74eab5",  
"current": true  
},  
{  
"type": "discipline",  
"value": "B.Tech.",  
"id": "a7706af7f1477cbb1ac0ceb0e8531de8da4ef1eb",  
"institute\_id": "4fb424a5addf32296f00013a"  
},  
]  
}

User 2:

{  
"created\_at": 1318513355000,  
"updated\_at": 1364888695000,  
"tags": [  
{  
"type": "college",  
"value": "Dhirubhai Ambani Institute of Information and  
Communication Technology",  
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361829"  
},  
{  
"type": "college",  
"value": "Bharatiya Vidya Bhavan's Public School, Jubilee hills,  
Hyderabad",  
"id": "d20730345465a974dc61f2132eb72b04e2f5330c"  
},  
{  
"type": "company",  
"value": "Alma Connect",  
"id": "93bc8199c5fe7adfd181d59e7182c73fec74eab5"  
},  
{  
"type": "sector",  
"value": "Website and Software Development",  
"id": "dc387d78fc99ab43e6ae2b83562c85cf3503a8a4"  
}  
]  
}

User 3

{  
"created\_at": 1318513355001,  
"updated\_at": 1364888695010,  
"tags": [  
{  
"type": "college",  
"value": "Dhirubhai Ambani Institute of Information and  
Communication Technology",  
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361821"  
},  
{  
"type": "sector",  
"value": "Website and Software Development",  
"id": "dc387d78fc99ab43e6ae2b83562c85cf3503a8a1"  
}  
]  
}

Using the above ES documents for search, I want to construct a query where  
I need to fetch users who have company tags in nested tag documents or the  
users who do not have any company tags. What will be my search query?

For example in above case, if search for google tag, then the returned  
documents should be 'user 1' and 'user 3' (as user 1 has company tag google  
and user 3 has no company tag). User 2 is not returned as it has a company  
tag other than google too.

--  
Thanks,  
Aash

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![acabrol](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@acabrol](https://discuss.elastic.co/u/acabrol)\
**Post date:** [October 15, 2016, 9:34am UTC](https://discuss.elastic.co/t/query-for-missing-fields-in-nested-documents/11834/2 "2016-10-15T09:34:44Z")

</div>

I have the same issue.

I need to remove empty topics field and i can't find any documentation or solution to query them.  
**Scheme:**

> "doc": {  
> "\_parent": {  
> "type": "source"  
> },  
> "\_timestamp": {  
> "enabled": true  
> },  
> "\_all": {  
> "enabled": true  
> },  
> "properties": {  
> "topics":{  
> "type": "nested",  
> "properties":{  
> "label":{"type":"string","index":"not\_analyzed"},  
> "score":{"type":"float"}  
> }  
> }  
> }  
> }

**Data sample:**

> {  
> "\_index" : "watch",  
> "\_type" : "doc",  
> "\_id" : "539f6cdafd667041e356c8740707b87f",  
> "\_score" : 0.5058483,  
> "\_routing" : "cbf1d10571c4da9d101c1b4fab3d3d93",  
> "\_timestamp" : 1476395901292,  
> "\_parent" : "cbf1d10571c4da9d101c1b4fab3d3d93",  
> "\_source" : {  
> "topics" :   
> }  
> }

**Query:**

> {  
> "size":1,  
> "query":{  
> "nested": {  
> "path": "topics",  
> "query": {  
> "missing": {  
> "field":"topics"  
> }  
> }  
> }  
> }  
> }

**Result:**

> {  
> "took": 6,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 0,  
> "max\_score": null,  
> "hits":   
> }  
> }

Thank you for your help.

---

<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 5, 2017, 10:12pm UTC](https://discuss.elastic.co/t/query-for-missing-fields-in-nested-documents/11834/3 "2017-07-05T22:12:03Z")

</div>


