ES错误处理:Fielddata is disabled on text fields by default. Set fielddata=true on

ES报错:Fielddata is disabled on text fields by default
报错信息:

{
    "error":{
        "root_cause":[
            {
                "type":"illegal_argument_exception",
                "reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [state] 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."
            }
        ],
        "type":"search_phase_execution_exception",
        "reason":"all shards failed",
        "phase":"query",
        "grouped":true,
        "failed_shards":[
            {
                "shard":0,
                "index":"bank",
                "node":"HuFlhO8OSLSGr3RP6J2z6Q",
                "reason":{
                    "type":"illegal_argument_exception",
                    "reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [state] 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."
                }
            }
        ]
    },
    "status":400
}

 

解决办法:

查询的时候添加keyword

如上面的查询使用的是:

{
    "size":0,
    "aggs":{
        "group_by_state":{
            "terms":{
                "field":"state"
            }
        }
    }
}

就会报错。

使用下面的语句就不会错误了

{
    "size":0,
    "aggs":{
        "group_by_state":{
            "terms":{
                "field":"state.keyword"
            }
        }
    }
}