首页 > 解决方案 > Elasticsearch Query DSL:字段的长度,如果字段存在

问题描述

假设我有一个字段,data.url. 我们的一些日志包含此字段,有些则不包含。我只想返回data.url长度超过 50 个字符的结果。真的,我只需要一个 URL 列表。

我正在努力:

GET _search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['data.url'].value.length() > 50",
            "lang": "painless"
          }
        }
      }
    }
  }
}

但得到混合错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
          "doc['data.url'].value.length() > 50",
          "    ^---- HERE"
        ],
        "script" : "doc['data.url'].value.length() > 50",
        "lang" : "painless",
        "position" : {
          "offset" : 4,
          "start" : 0,
          "end" : 35
        }
      },

或者

        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "org.elasticsearch.index.fielddata.ScriptDocValues$Strings.get(ScriptDocValues.java:496)",
          "org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:503)",
          "doc['data.url'].value.length() > 50",
          "               ^---- HERE"
        ],
        "script" : "doc['data.url'].value.length() > 50",
        "lang" : "painless",
        "position" : {
          "offset" : 15,
          "start" : 0,
          "end" : 35
        }

          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "No field found for [data.url] in mapping with types []"
          }

而有时

          "caused_by" : {
            "type" : "illegal_state_exception",
            "reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
          }

这个领域肯定存在;我可以在日志中看到它,在搜索字段中搜索,并使用术语作品:

GET _search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "data.url": "www.google.com"
        }
      }
    }
  }
}

我错过了什么?

我正在使用 Elasticsearch 7.8。

标签: elasticsearchkibanaquerydsl

解决方案


由于您使用的是版本 7.*,因此您需要使用以下脚本查询

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['data.url.keyword'].length > 50",
            "lang": "painless"
          }
        }
      }
    }
  }
}

如果data.url字段是keyword类型,则忽略".keyword"字段末尾的


推荐阅读