How to use multiple scripts in ElasticSearch should
Here is the sample data
{
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"hits": [
{
"_source": {
"type": 2,
"size": 11,
"name": "haha",
"length": 18
}
},
{
"_source": {
"type": 2,
"size": 13,
"name": "haha",
"length": 17
}
},
{
"_source": {
"type": 2,
"size": 13,
"name": "hehe",
"length": 17
}
}
]
}
}
It looks like this.
{
"query": {
"bool": {
"must": [
{
"term": {
"type": 2
}
},
{
"bool": {
"should": [
{
"script": {
"script": {
"inline": "doc['size'].value == 11",
"lang": "painless"
}
}
},
{
"script": {
"script": {
"inline": "doc['type'].value + doc['size'].value == 15",
"lang": "painless"
}
}
}
]
}
}
]
}
}
}
I am getting the following error. I don't understand why, I only use one of the scripts and they all work fine, do you know what is the reason?
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
"doc['size'].value == 11",
" ^---- HERE"
],
"script": "doc['size'].value == 11",
"lang": "painless",
"position": {
"offset": 11,
"start": 0,
"end": 23
}
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "my-test",
"node": "O9NpQOHXSNqELJwzBf5r0w",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
"doc['size'].value == 11",
" ^---- HERE"
],
"script": "doc['size'].value == 11",
"lang": "painless",
"position": {
"offset": 11,
"start": 0,
"end": 23
},
"caused_by": {
"type": "illegal_state_exception",
"reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
}
}
}
]
},
"status": 400
}
I want to use multiple scripts inside should, but I don't know how to use it, can you tell me?
sizefield with a valid value.