【问题标题】:Elasticsearch Query DSL: Length of field, if field existsElasticsearch Query DSL:字段长度,如果字段存在
【发布时间】:2021-07-29 23:17:10
【问题描述】:

假设我有一个字段,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。

【问题讨论】:

    标签: elasticsearch kibana querydsl


    【解决方案1】:

    由于您使用的是版本 7.*,因此您需要在script query下方使用这个

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

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

    【讨论】:

    • 谢谢!尝试这个只会给我这个错误:"caused_by" : { "type" : "illegal_argument_exception", "reason" : "No field found for [data.url.keyword] in mapping with types []" } 有或没有.keyword
    • @jamesdeluk 你能分享你的索引映射吗?
    • 抱歉耽搁了。你是这个意思吗? Name: data.url | Type: string | Format &lt;blank, default is string&gt; | Searchable &lt;green&gt; | Aggregatable &lt;green&gt; | Excluded &lt;blank&gt;
    • @jamesdeluk 我要求索引映射。您可以使用 GET 映射 API 获取索引映射 --> elastic.co/guide/en/elasticsearch/reference/current/… 请分享您索引数据的格式?
    • @jamesdeluk 你能回复我之前的评论吗?
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多