elaticsearch 聚合操作出现 Fielddata is disabled on text fields by default

1、问题概述

执行语句:

GET /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}

出现错误

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "xytest",
        "node": "6h-7wPJmQqWGfz6nbgqjjQ",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
        }
      }
    ],
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
    }
  },
  "status": 400
}

2、原因和解决

原因: 是因为elaticsearch5.x 之后
聚合这些操作用单独的数据结构(fielddata)缓存到内存里了,需要单独开启。

解决:

PUT xytest/_mapping/sutdent
{
  "properties": {
    "region":{
      "type": "text",
      "fielddata": true
    }
  }
}

设置结果:
{
  "acknowledged": true
}

在次执行就ok。

猜你喜欢

转载自blog.csdn.net/zhongzunfa/article/details/81148115