【问题标题】:Elasticsearch search query: why params._source.nested_field.size() is not working in script?Elasticsearch 搜索查询:为什么 params._source.nested_field.size() 在脚本中不起作用?
【发布时间】:2019-01-03 12:26:48
【问题描述】:

对此有很多问题和答案,但仍然没有得到满意的答案。 弹性搜索版本:6.5

索引映射

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

搜索查询:

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

也试过下面的查询,但没有运气

{
  "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 中使用时返回嵌套数组,但在验证查询中不起作用。关于使用无痛脚本的嵌套查询的文档不完整。

【问题讨论】:

  • 在过滤上下文中,Painless 无权访问_source 文档:elastic.co/guide/en/elasticsearch/painless/6.5/…,主要是出于性能原因。
  • 验证嵌套值的替代方法是什么?
  • 你最终解决了这个问题吗?我也有同样的问题...
  • @Rob:不,我们最近升级到了最新版本 7.2,我还没有尝试过相同的脚本。会让您知道这是否适用于最新版本。我真的很惊讶世界上没有人对这些 elaticsearch 的问题感兴趣。
  • @Avi 现在我有一个类似的问题 - discuss.elastic.co/t/… 你的问题解决了吗?

标签: elasticsearch scripting nested elasticsearch-painless


【解决方案1】:

尽管这会很慢并且against the guidance 在搜索查询中访问_source,但该查询在 v6.4 之前可以工作。

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

话虽如此,您可以“劫持”一个 function_score 查询,其 Painless 上下文仍然可以访问 _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 计算字段大小,然后将其保存在文档的顶层。

或者,您可以使用copy_to,如my related answer 中所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2017-08-09
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多