Elasticsearch权威指南里的聚合分析报错: Text fields are not optimised for operations that require per-document field data like aggregations and sorting。

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "distinct_colors": {
      "cardinality": {
        "field": "interests"
      }
    }
  }
}

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "distinct_colors": {
      "cardinality": {
        "field": "age"
      }
    }
  }
}

interests上进行聚合会报错,而age没问题。

  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [xxxx] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,

因为intereststypetext,而age不是, textannotated_text字段doc_values默认为false

解决方案一:  

简单理解,就是text字段作为一个整体,默认没有索引.不过text分词之后的keyword是有索引的,因而可以对interests.keyword进行聚合。

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "distinct_colors": {
      "cardinality": {
        "field": "interests.keyword"
      }
    }
  }
}

增加:keyword

解决方案二:

set fielddata=true,不过不推荐

PUT employee/_mapping 
{
  "properties": {
    "interests": {
      "type": "text",
      "fielddata": true
    }
  }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐