如何在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
}我想使用多个脚本里面应该,但我不知道如何使用它,你能告诉我吗?
发布于 2022-10-19 05:38:32
对于没有有效值的size字段的文档来说,这是典型的错误。
文档没有字段的值!使用doc[].size()==0检查文档是否缺少字段!
如果不能保证所有文档都有此字段,则需要在使用该字段之前测试该字段的存在性:
"inline": "doc['size'].size() > 0 ? doc['size'].value == 11 : false",此外,这个简单的脚本可以替换为一个术语查询,而且性能要好得多:
{
"term": {
"size": 11
}
}https://stackoverflow.com/questions/74119676
复制相似问题