【问题标题】:How to write Elasticsearch multiple must scripts query?如何编写 Elasticsearch 多个必须脚本查询?
【发布时间】:2018-12-10 16:24:59
【问题描述】:

我想使用查询来比较多个字段。我有字段 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","re​​ason":"编译 错误","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","re​​ason":"all 碎片 失败","阶段":"查询","分组":true,"failed_shards":[{"shard":0,"index":"financials","node":"8SXaM2HcStelpLHvTDSMCQ","re​​ason":{ “类型”:“query_shard_exception”,“原因”:“失败 创建查询: {\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","re​​ason":"编译 错误","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","re​​ason":"不是 声明。"}}}}]}

【问题讨论】:

    标签: json elasticsearch


    【解决方案1】:

    你需要像这样结合你的两个条件:

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

    【讨论】:

    • 哇,非常感谢,你救了我的命。实际上这是我之前的想法,但是在过滤器查询中我不能使用 && 所以我认为那是行不通的。我花了几个小时在谷歌上搜索解决方案,我感到压力很大。显然,我之前认为工作正常。
    • 酷,很高兴它有帮助!
    • @Val:知道如何解决this 吗?
    【解决方案2】:

    为了使用多个条件,'must'、'should'和'must_not'可以作为数组使用,每个条件都成为它的元素。根据Elasticsearch documentation

    "query": {
      "bool" : {
        "must" : {
          "term" : { "user" : "kimchy" }
        },
        "filter": {
          "term" : { "tag" : "tech" }
        },
        "must_not" : {
          "range" : {
            "age" : { "gte" : 10, "lte" : 20 }
          }
        },
        "should" : [
          { "term" : { "tag" : "wow" } },
          { "term" : { "tag" : "elasticsearch" } },
          { "term" : { "tag" : "and so on" } }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
      }
    }
    

    【讨论】:

    • OP需要比较两个字段,正常情况下无法做到这一点,需要使用脚本或在索引时将比较结果存储在新字段中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2015-04-16
    相关资源
    最近更新 更多