如何编写Elasticsearch多个必须脚本查询? [英] How to write Elasticsearch multiple must scripts query?

查看:163
本文介绍了如何编写Elasticsearch多个必须脚本查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用查询来比较多个字段.我有字段1到4.我想搜索字段1大于字段2且下面的查询工作正常的数据;

I want to use a query to compare multiple fields. I have field 1 to 4. I want to search data which field 1 is greater than field 2 and below query is work perfectly;

{
    "size": 0,
    "_source": [
        "field1",
        "field2",
        "field3",
        "field4"
    ],
    "sort": [],
    "query": {
        "bool": {
            "filter": [],
            "must": {
                "script": {
                    "script": {
                        "inline": "doc['field1'].value > doc['field2'].value;",
                        "lang": "painless"
                    }
                }
            }
        }
    }
}

现在,我想搜索字段1大于字段2以及字段3大于字段4的数据.弹性搜索:如何编写多语句脚本?和

Now, I want to search data which field 1 is greater than field 2 and also which field 3 is greater than field 4. according Elastic Search: How to write multi statement scripts? and This link I just need to separate each statement with a semicolon. So it should be like this:

{
    "size": 0,
    "_source": [
        "field1",
        "field2",
        "field3",
        "field4"
    ],
    "sort": [],
    "query": {
        "bool": {
            "filter": [],
            "must": {
                "script": {
                    "script": {
                        "inline": "doc['field1'].value > doc['field2'].value; doc['field3'].value > doc['field4'].value;",
                        "lang": "painless"
                    }
                }
            }
        }
    }
}

但是该查询无法正常工作,并返回如下编译错误:

But that query doesn't work and return compile error like this:

{"root_cause":[{"type":"script_exception","reason":编译 错误," script_stack:[" doc ['field1'].value> doc ['...," ^ ---- HERE]," script:" doc ['field1'].value> doc ['field2'].value; doc ['field1'].value> doc ['field2'].value; ," lang:"无痛}]],"类型:" search_phase_execution_exception,"原因:"全部 分片 失败",阶段":查询",分组":true,"failed_shards":[{"shard":0,"index":"financials","node":"8SXaM2HcStelpLHvTDSMCQ",原因":{ "type":"query_shard_exception",原因":失败 创建查询:{\ n \"bool \":{\ n \必须\":[\ n {\ n \脚本\": {\ n \"script \":{\ n \"source \":\"doc ['field1'].value> doc ['field2'].value; doc ['field1'].value> doc ['field2'].value; \,\ n \"lang \":\无痛\" \ n},\ n \"boost \":1.0 \ n} \ n} \ n],\ n \"adjust_pure_negative \":true,\ n \"boost \":1.0 \ n } \ n}," index_uuid:" hz12cHg1SkGwq712n6BUIA," index:"财务," caused_by:{" type:" script_exception," reason:"编译 错误," script_stack:[" doc ['field1'].value> doc ['...," ^ ---- HERE]," script:" doc ['field1'].value> doc ['field2'].value; doc ['field1'].value> doc ['field2'].value; ," lang:"无痛," caused_by:{" type:" illegal_argument_exception," reason:"不 声明.}}}}]}

{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["doc['field1'].value > doc[' ...","^---- HERE"],"script":"doc['field1'].value > doc['field2'].value; doc['field1'].value > doc['field2'].value; ","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"financials","node":"8SXaM2HcStelpLHvTDSMCQ","reason":{"type":"query_shard_exception","reason":"failed to create query: {\n \"bool\" : {\n \"must\" : [\n {\n \"script\" : {\n \"script\" : {\n \"source\" : \"doc['field1'].value > doc['field2'].value; doc['field1'].value > doc['field2'].value; \",\n \"lang\" : \"painless\"\n },\n \"boost\" : 1.0\n }\n }\n ],\n \"adjust_pure_negative\" : true,\n \"boost\" : 1.0\n }\n}","index_uuid":"hz12cHg1SkGwq712n6BUIA","index":"financials","caused_by":{"type":"script_exception","reason":"compile error","script_stack":["doc['field1'].value > doc[' ...","^---- HERE"],"script":"doc['field1'].value > doc['field2'].value; doc['field1'].value > doc['field2'].value; ","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"Not a statement."}}}}]}

推荐答案

您需要像这样组合两个条件:

You need to combine your two conditions like this:

doc['field1'].value > doc['field2'].value && doc['field3'].value > doc['field4'].value
                                           ^
                                           |
                               replace the semicolon by &&

这篇关于如何编写Elasticsearch多个必须脚本查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆