首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ElasticSearch中使用多个脚本?

如何在ElasticSearch中使用多个脚本?
EN

Stack Overflow用户
提问于 2022-10-19 03:53:32
回答 1查看 29关注 0票数 0

如何在ElasticSearch中使用多个脚本应该

以下是示例数据

代码语言:javascript
复制
{
    "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
                }
            }
        ]
    }
}

它看起来像这样。

代码语言:javascript
复制
{
    "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"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

我得到了以下错误。我不明白为什么,我只使用其中一个脚本,它们都工作正常,你知道原因是什么吗?

代码语言:javascript
复制
{
    "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
}

我想使用多个脚本里面应该,但我不知道如何使用它,你能告诉我吗?

EN

回答 1

Stack Overflow用户

发布于 2022-10-19 05:38:32

对于没有有效值的size字段的文档来说,这是典型的错误。

文档没有字段的值!使用doc[].size()==0检查文档是否缺少字段!

如果不能保证所有文档都有此字段,则需要在使用该字段之前测试该字段的存在性:

代码语言:javascript
复制
 "inline": "doc['size'].size() > 0 ? doc['size'].value == 11 : false",

此外,这个简单的脚本可以替换为一个术语查询,而且性能要好得多:

代码语言:javascript
复制
{
    "term": {
         "size": 11
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74119676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档