Elasticsearch搜索查询:为什么params._source.nested_field.size()在脚本中不起作用? [英] Elasticsearch search query: why params._source.nested_field.size() is not working in script?

查看:168
本文介绍了Elasticsearch搜索查询:为什么params._source.nested_field.size()在脚本中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此有很多疑问和答案,但仍未获得满意的答案.Elasticsearch版本:6.5

There are lot of questions and answers about this but still didn't get a satisfied answers. Elasticsearch version: 6.5

索引映射

"_doc": {
    "properties": {
      "ssid": {
        "type": "long"
      },
      "nested_field": {
        "type": "nested"
      }
    }
  }
}

搜索查询:

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "params._source.nested_field.size() > 1"
        }
      }
    }
  }
}

也在下面的查询中尝试过但没有运气

Also tried below query but no luck

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "nested_field",
            "query": {
              "bool": {
                "filter": {
                  "script": {
                    "script": "params._source.nested_field.size() > 1"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

错误

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "params._source.nested_field.size() > 1",
          "              ^---- HERE"
        ],
        "script": "params._source.nested_field.size() > 1",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "testing_index",
        "node": "XXXXXXXXXXXXXXXXXXXXXXX",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "params._source.nested_field.size() > 1",
            "              ^---- HERE"
          ],
          "script": "params._source.nested_field.size() > 1",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "status": 500
}

params._source.nested_field在scripted_field中使用时返回嵌套数组,但在验证查询中不起作用.有关使用无痛脚本的嵌套查询的文档并不完整.

params._source.nested_field return nested array while using in scripted_field but not working in validation query. Documentation are not complete about nested query with painless script.

推荐答案

尽管事实会很慢,并且

Despite the fact that this would've been slow and against the guidance of not accessing _source in search queries, the query would have worked before v6.4.

在v6.4之后,由于重构的意外副作用,则无法在脚本查询上下文中访问 _source .

After v6.4, thanks to an unintentional side effect of refactoring, accessing _source in the script query context is not possible.

话虽如此,您可以劫持"一个 function_score 查询,其无痛上下文 still 可以访问 _source :

With that being said, you can "hijack" a function_score query whose Painless context still has access to the _source:

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "params._source.containsKey('nested_field') && params._source['nested_field'] != null && params._source.nested_field.size() > 1 ? 1 : 0"
            }
          }
        }
      ],
      "min_score": 1
    }
  }
}


您可以使用管道计算字段大小,然后将其保存在文档的顶层.


You could use a pipeline to calculate the field size and then save it on the doc's top level.

或者,您可以使用我相关的答案中概述的 copy_to .

Alternatively, you could use copy_to as outlined in my related answer.

这篇关于Elasticsearch搜索查询:为什么params._source.nested_field.size()在脚本中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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