# How to check for existence of field in script

**URL:** https://discuss.elastic.co/t/how-to-check-for-existence-of-field-in-script/3815
**Category:** Elasticsearch
**Created:** [January 21, 2011, 5:54am UTC](https://discuss.elastic.co/t/how-to-check-for-existence-of-field-in-script/3815 "2011-01-21T05:54:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dobe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dobe/32/2152_2.png) [@dobe](https://discuss.elastic.co/u/dobe)
#### Post date: [January 21, 2011, 5:54am UTC](https://discuss.elastic.co/t/how-to-check-for-existence-of-field-in-script/3815/1 "2011-01-21T05:54:22Z")

</div>

hi all,  
as already discussed on irc, i don't know how to check for the  
existence of a field in a script filter. version 0.14.1 and 0.14.2  
tried

note that this example is not the full implementation and i know that  
this can be done without script it is just for demonstration.  
so when i issue:

curl -XPUT $H/ft/Policy/1 -d '  
{  
"contract": "blah",  
constraints:[  
{  
"regions": "reg1",  
"applications": "app1"  
},  
{  
"regions": "any",  
"applications": "app2"  
}  
]  
}  
'

curl -XGET $H/ft/Policy/\_search?pretty=1 -d '  
{  
"query": {  
"filtered" : {  
"query" : {match\_all:{}},  
"filter" : {  
"script" : {  
"script" : "\_source.contains\_key('x')"  
,  
"params" : {"a" : "app1", "r": "at"}  
}  
}  
}  
}  
}  
'

i get:

{  
"took" : 87,  
"\_shards" : {  
"total" : 5,  
"successful" : 4,  
"failed" : 1,  
"failures" : [ {  
"index" : "ft",  
"shard" : 2,  
"reason" : "RemoteTransportException[[Harmonica][inet[/  
192.168.34.135:9300]][search/phase/query]]; nested:  
QueryPhaseExecutionException[[ft][2]:  
query[filtered(ConstantScore(_:_))-

> ScriptFilter(\_source.contains\_key(x))],from[0],size[10]: Query Failed  
> [Failed to execute main query]]; nested:  
> PropertyAccessException[[Error: could not access: x; in class: null]  
> \n[Near : {... Unknown ....}]\n ^\n[Line: 1, Column: 0]];  
> "  
> } ]  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }  
> }

is there another way? imho "containsKey" sounds like it should return  
a boolean  
thx in advance, bernd

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [January 23, 2011, 4:46pm UTC](https://discuss.elastic.co/t/how-to-check-for-existence-of-field-in-script/3815/2 "2011-01-23T16:46:54Z")

</div>

Does check for null works (the source is parsed into map of maps)? Try and use javascript, should make things simpler (and possibly faster). Note, accessing the \_source as part of a search request will mean loading the actual source json and parsing it, which can be expensive. You might consider using the doc['field\_name'] notation to access it by loading all values to memory. Then doc['field\_name'].empty will return true if the field has no data.  
On Friday, January 21, 2011 at 7:54 AM, dobe wrote:

> hi all,  
> as already discussed on irc, i don't know how to check for the  
> existence of a field in a script filter. version 0.14.1 and 0.14.2  
> tried
> 
> note that this example is not the full implementation and i know that  
> this can be done without script it is just for demonstration.  
> so when i issue:
> 
> curl -XPUT $H/ft/Policy/1 -d '  
> {  
> "contract": "blah",  
> constraints:[  
> {  
> "regions": "reg1",  
> "applications": "app1"  
> },  
> {  
> "regions": "any",  
> "applications": "app2"  
> }  
> ]  
> }  
> '
> 
> curl -XGET $H/ft/Policy/\_search?pretty=1 -d '  
> {  
> "query": {  
> "filtered" : {  
> "query" : {match\_all:{}},  
> "filter" : {  
> "script" : {  
> "script" : "\_source.contains\_key('x')"  
> ,  
> "params" : {"a" : "app1", "r": "at"}  
> }  
> }  
> }  
> }  
> }  
> '
> 
> i get:
> 
> {  
> "took" : 87,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 4,  
> "failed" : 1,  
> "failures" : [ {  
> "index" : "ft",  
> "shard" : 2,  
> "reason" : "RemoteTransportException[[Harmonica][inet[/  
> 192.168.34.135:9300]][search/phase/query]]; nested:  
> QueryPhaseExecutionException[[ft][2]:  
> query[filtered(ConstantScore(_:_))-
> 
> > ScriptFilter(\_source.contains\_key(x))],from[0],size[10]: Query Failed
> 
> [Failed to execute main query]]; nested:  
> PropertyAccessException[[Error: could not access: x; in class: null]  
> \n[Near : {... Unknown ....}]\n ^\n[Line: 1, Column: 0]];  
> "  
> } ]  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }  
> }
> 
> is there another way? imho "containsKey" sounds like it should return  
> a boolean  
> thx in advance, bernd

---

<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, 4:13am UTC](https://discuss.elastic.co/t/how-to-check-for-existence-of-field-in-script/3815/3 "2017-07-06T04:13:22Z")

</div>


