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

查看:38
本文介绍了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.

推荐答案

尽管这会很慢而且 反对访问_source的指导搜索查询,该查询在 v6.4 之前就可以工作了.

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 之后,由于 norelellow" 的无意副作用,在脚本查询上下文中访问 _source 是不可能的.

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

话虽如此,您可以劫持"一个 function_score 查询,其无痛上下文仍然可以访问_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
    }
  }
}


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


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天全站免登陆