关于ES出现Fielddata is disabled on text fields by default的错误

跟着ElasticSearch官网的文档学习,执行以下代码

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

报错如下

{
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] 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."
}

百度查到需要在aggs前执行以下代码

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

但还是不行,继续报错

{
        "type": "illegal_argument_exception",
        "reason": "Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true."
}

后面查到可以直接在属性后加keyword

GET /megacorp/employee/_search
{
  "size":0,
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests.keyword"
      }
    }
  }
}

问题解决!

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "all_interests" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "music",
          "doc_count" : 2
        },
        {
          "key" : "forestry",
          "doc_count" : 1
        },
        {
          "key" : "sports",
          "doc_count" : 1
        }
      ]
    }
  }
}

感谢以上两位博主!

你可能感兴趣的:(ElasticSearch)