GET /_search
{
"aggs" : {
"products" : {
"terms" : {
"field" : "product"
}
}
}
}
如果你的字段product对应是string,会发现报错无法正确查询。
If you try to sort, aggregate, or access values from a script on a text field, you will see this exception:
Fielddata is disabled on text fields by default. Set fielddata=true
on
[your_field_name
] in order to load fielddata in memory by uninverting the
inverted index. Note that this can however use significant memory.
还是不要百度,好好看官网,百度的好多都是老版本的。
其实官网有注释:
terms aggregation should be a field of type keyword or any other data type suitable for bucket aggregations. In order to use it with text you will need to enable fielddata.
解决办法:
You can enable fielddata on an existing text field using the PUT mapping API as follows:
PUT my_index/_mapping
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}