I am trying to use script fields in my query. I enabled sandbox scripting, and trying to use an expression to calculate a new field.
The thing is I get the following error:
{
"type": "expression_script_compilation_exception",
"reason": "Only the member variable [value] or member methods may be accessed on a field when not accessing the field directly"
}
It seems that only "value" is accessible. what am I missing here?
When running the following query:
{
"query": {
"match_all": {}
},
"script_fields" : {
"field1" : {
"script" : {
"lang": "expression",
"inline": "doc['about.hobbies'].empty"
}
}
}
}
Mapping:
{
"my_index": {
"mappings": {
"type": {
"properties": {
"about": {
"properties": {
"hobbies": {
"type": "string",
"analyzer": "lowercase"
}
}
}
}
}
}
}
Little explanation: I have a field which can contain list of string values.
"hobbies": ["a","b",c"]
and it can be also empty. I want to have a script field of type boolean that will have the value true when the list is not empty, and false when the list is empty.
Update: Reading some more, I encounter this documentation on lucene expressions scripts
There are a few limitations relative to other script languages:
- Only numeric fields may be accessed
- Stored fields are not available
- If a field is sparse (only some documents contain a value), documents missing the field will have a value of 0
My field is of type String, it might be the problem? If it is, is there any other way to use script fields based on string fields? maybe using groovy?
I think the problem is that the field is a nested object, if I read the docs correctly then doc['field'] only supports simple term fields.
Note, however, that the doc[...] notation only allows for simple valued fields (can’t return a json object from it) and makes sense only on non-analyzed or single term based fields.
However using _source has worked for me
"script_fields" : {
"field1" : {
"script" : "_source.about.hobbies.size() > 0"
}
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With