# Elasticsearch script execution on missing field

**URL:** <https://discuss.elastic.co/t/elasticsearch-script-execution-on-missing-field/19840>\
**Category:** Elasticsearch\
**Created:** [September 17, 2014, 10:53am UTC](https://discuss.elastic.co/t/elasticsearch-script-execution-on-missing-field/19840 "2014-09-17T10:53:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj\_2](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@manoj\_2](https://discuss.elastic.co/u/manoj_2)\
**Post date:** [September 17, 2014, 10:53am UTC](https://discuss.elastic.co/t/elasticsearch-script-execution-on-missing-field/19840/1 "2014-09-17T10:53:08Z")

</div>

I am currently using ES version 0.19. For a feature requirement, I wanted  
to execute script on missing field through terms facet. The curl which I  
tried is something like below

`
{
    "query": {
        "term": {
            "content": "deep"
        }
    },
    "filter": {
        "and": {
            "filters": [
                {
                    "type": {
                        "value": "twitter"
                    }
                }
            ]
        }
    },
    "facets": {
        "loca": {
            "terms": {
                "field": "countryid",
                "script": "doc['countryid']==null?1:doc['countryid'].value"
            }
        }
    }
}
`

I assume that missing fields can be accessed by the condition  
doc['countryid']==null. But it looks like this is not the way to identify  
the missing field in script ☹

For which am always receiving response as missing

`{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 6,
"successful" : 6,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : []
},
"facets" : {
"loca" : {
"_type" : "terms",
"missing" : 1,
"total" : 0,
"other" : 0,
"terms" : []
}
}
}`

Could anybody help me to get this correct.

Thanks in advance, Manoj

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/aaccc328-e7c1-4268-a8b9-585d16e7cecb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/aaccc328-e7c1-4268-a8b9-585d16e7cecb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [September 17, 2014, 12:24pm UTC](https://discuss.elastic.co/t/elasticsearch-script-execution-on-missing-field/19840/2 "2014-09-17T12:24:51Z")

</div>

I think the correct way to see if there is a missing field is the following

doc['countryid'].empty == true

Check also:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

btw why such an old version of ES?

Thomas

On Wednesday, 17 September 2014 13:53:08 UTC+3, Manoj wrote:

> I am currently using ES version 0.19. For a feature requirement, I wanted  
> to execute script on missing field through terms facet. The curl which I  
> tried is something like below
> 
> `
> {
> "query": {
> "term": {
> "content": "deep"
> }
> },
> "filter": {
> "and": {
> "filters": [
> {
> "type": {
> "value": "twitter"
> }
> }
> ]
> }
> },
> "facets": {
> "loca": {
> "terms": {
> "field": "countryid",
> "script": "doc['countryid']==null?1:doc['countryid'].value"
> }
> }
> }
> }
> `
> 
> I assume that missing fields can be accessed by the condition  
> doc['countryid']==null. But it looks like this is not the way to identify  
> the missing field in script ☹
> 
> For which am always receiving response as missing
> 
> `{
> "took" : 1,
> "timed_out" : false,
> "_shards" : {
> "total" : 6,
> "successful" : 6,
> "failed" : 0
> },
> "hits" : {
> "total" : 0,
> "max_score" : null,
> "hits" : 
> },
> "facets" : {
> "loca" : {
> "_type" : "terms",
> "missing" : 1,
> "total" : 0,
> "other" : 0,
> "terms" : 
> }
> }
> }`
> 
> Could anybody help me to get this correct.
> 
> Thanks in advance, Manoj

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b5a1acee-05c1-412d-b10a-d4235cd0b628%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b5a1acee-05c1-412d-b10a-d4235cd0b628%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:01am UTC](https://discuss.elastic.co/t/elasticsearch-script-execution-on-missing-field/19840/3 "2017-07-06T01:01:37Z")

</div>


