天天看點

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": "h-wPJmQqWGfz6nbgqjjQ",
        "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。