admin管理员组

文章数量:1530279

es 聚合查询,根据tags字段进行聚合查询

GET /ecommerce/product/_search
{
  "aggs":{
    "group_by_tags" :{
      "terms": {
        "field": "tags"
      }
    }
  }
}

发生异常

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 [tags] in order to load field data by uninverting the inverted index. 
Note that this can use significant memory.

完整错误信息:

{
  "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 [tags] 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,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "ecommerce",
        "node" : "jxkP5bULTYaYdJ0k9GWdPw",
        "reason" : {
          "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 [tags] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
        }
      }
    ],
    "caused_by" : {
      "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 [tags] in order to load field data by uninverting the inverted index. Note that this can use significant memory.",
      "caused_by" : {
        "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 [tags] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
      }
    }
  },
  "status" : 400
}

解决办法: 更新索引mapping,将需要聚合的字段fielddata设置为true

#修改索引的mapping
PUT /ecommerce/_mapping
{
  "properties" : {
    "tags" : {
      "type":"text",
      "fielddata": true
    }
  }
}

本文标签: 异常发生