elasticsearch查询错误

elasticsearch7.12.1 使用查询语句aggs 聚合查询的时候

{
    "aggs": {
        "price_group": {
            "terms": {
                "field": "price"
            }
        }
    }
}

报错

{
    "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 [price] 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": "shopping",
                "node": "Tk4LCCTpQRi8MqZa91v4UQ",
                "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 [price] 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 [price] 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 [price] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
            }
        }
    },
    "status": 400
}

解决方案:

加上  .keyword 记住带.

{
    "aggs": {
        "price_group": {
            "terms": {
                "field": "price.keyword"
            }
        }
    }
}

返回结果正确

 

相关文章

TCP/IP套接字登录方法是MySQL在一切服务平台都提供的一种登录...
easy-rule规则引擎最佳落地
Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分...
最近壹哥的一个学生,在利用spring-data-elasticsearch访问E...
java 操作elasticsearch详细总结
原文链接:http://www.ruanyifeng.com/blog/2017/08/elastic...