How to check for existence of field in script


(dobe) #1

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


How to fix index_out_of_bounds_exception exception in aggregation in painless script
(Shay Banon) #2

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


(system) #3