0 results found
DeCo
Elasticsearch脚本查询

es查询通过字段值的长度过滤

GET api-monitor-2019.12.28/_search
{
  "size": 10, 
  "query": {
    "script": {
      "script": "doc['tget.keyword'].value.length() > 4"
    }
  }
}

ES Lucene expressions language 文档

文档中的 doc['field_name'].length 取的值是数组长度而不是字段值的长度
使用doc['field_name'].value.length()

text类型的字段会出现错误。

GET api-monitor-2019.12.28/_search
{
  "size": 10, 
  "query": {
    "script": {
      "script": "doc['tget'].value.length() > 4"
    }
  }
}
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:670)",
          "org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116)",
          "org.elasticsearch.index.query.QueryShardContext.lambda$lookup$0(QueryShardContext.java:283)",
          "org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:88)",
          "org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:85)",
          "java.base/java.security.AccessController.doPrivileged(Native Method)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:85)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
          "doc['tget'].value.length() > 2",
          "    ^---- HERE"
        ],
        "script": "doc['tget'].value.length() > 2",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "api-monitor-2019.12.28",
        "node": "hTWnl3yCTw6oqUiWqBkSiQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:670)",
            "org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116)",
            "org.elasticsearch.index.query.QueryShardContext.lambda$lookup$0(QueryShardContext.java:283)",
            "org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:88)",
            "org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:85)",
            "java.base/java.security.AccessController.doPrivileged(Native Method)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:85)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
            "doc['tget'].value.length() > 2",
            "    ^---- HERE"
          ],
          "script": "doc['tget'].value.length() > 2",
          "lang": "painless",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tget] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
          }
        }
      }
    ]
  },
  "status": 500
}

ES fielddata官方文档

解决方法

  1. 使用 tget.keyword
  2. 字段的fileddata设置为true
    PUT api-monitor-2019.11.04/_mapping/doc
    {
      "properties": {
        "tget": {
          "type": "text"
          , "fielddata": true
        }
      }
    }
请杯咖啡呗~
支付宝
微信
本文作者:DeCo
版权声明:本文首发于DeCo的博客,转载请注明出处!