如何编写Elasticsearch多个必须脚本查询?

时间:2018-07-02 12:26:33

标签: json elasticsearch

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

{
    "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的数据。根据Elastic Search: How to write multi statement scripts?This link,我只需要用一个逗号分隔每个语句分号。所以应该像这样:

{
    "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"
                    }
                }
            }
        }
    }
}

但是该查询不起作用,并返回如下编译错误:

  

{“ 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”,“ reason”:“失败   创建查询:{\ 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“:”不   声明。“}}}}]}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:-1)

为了使用多个条件,可以将'must','should'和'must_not'用作数组,并且每个条件都将成为条件元素。根据{{​​3}}

        paddingright = 50pt
        paddingtop = 50pt
        paddingright = 50pt
        paddingleft = 50pt
        padding = 50pt