我正在使用Elastic Search创建搜索过滤器,并且需要查找“cambio”列的数据库中保存的所有值,而无需重复这些值。
这些值按如下方式保存:“5月手动设置”或“6月手动设置”...。
我创建了此查询以返回所有保存的值:
GET /crawler10/crawler-vehicles10/_search
{
"size": 0,
"aggregations": {
"my_agg": {
"terms": {
"field": "cambio"
}
}
}
}
但是,当我运行返回的值时,它们看起来像这样:
"aggregations": {
"my_agg": {
"doc_count_error_upper_bound": 2,
"sum_other_doc_count": 2613,
"buckets": [
{
"key": "de",
"doc_count": 2755
},
{
"key": "marchas",
"doc_count": 2714
},
{
"key": "manual",
"doc_count": 2222
},
{
"key": "modo",
"doc_count": 1097
},
{
"key": "5",
"doc_count": 1071
},
{
"key": "d",
"doc_count": 1002
},
{
"key": "n",
"doc_count": 1002
},
{
"key": "automática",
"doc_count": 935
},
{
"key": "com",
"doc_count": 919
},
{
"key": "6",
"doc_count": 698
}
]
}
}
最佳答案
聚合基于已保存字段的映射类型。 cambio
的字段类型似乎设置为analyzed
(默认情况下)。请使用字段not_analyzed
的映射cambio
创建一个索引。
您可以使用PUT
请求创建索引,如下所示(如果您的ES版本小于5),则需要在crawler10
索引中重新索引数据。
PUT crawler10/_mapping/
{
"mappings": {
"crawler-vehicles10": {
"properties": {
"cambio": {
"type": "string"
"index": "not_analyzed"
}
}
}
}
}
对于ES v5或更高版本
PUT crawler10/_mapping/
{
"mappings": {
"crawler-vehicles10": {
"properties": {
"cambio": {
"type": "keyword"
}
}
}
}
}
关于elasticsearch - 如何在Elastic Search中使用聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41617749/