如何在ElasticSearch中使用多个脚本
以下是示例数据
{
"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
}
}
]
}
}
它看起来像这样。
{
"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"
}
}
}
]
}
}
]
}
}
}
我得到以下错误。我不明白为什么,我只使用其中一个脚本,他们都工作正常,你知道是什么原因吗?
{
"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
}
我想在里面应该使用多个脚本,但是我不知道怎么使用它,你能告诉我吗?
1条答案
按热度按时间i1icjdpr1#
这是文档中
size
域没有有效值时的典型错误。文档没有字段值!请使用doc[].size()==0检查文档是否缺少字段!
如果不能保证所有文档都具有此字段,则需要在使用之前测试该字段是否存在:
此外,这个简单的脚本可以替换为一个术语查询,并且性能更高: