我是ES新手并创建了一个名为vehicle的索引,其数据如下:
_source": {
"catalogName": "EPE",
"makeId": 1,
"modelName": "GRAND PRIX SPECIAL",
"catalogId": 1,
"modelId": 22,
"regionId": 6,
"regionName": "All Regions",
"vehicleId": 302,
"engineName": "2-399 .4L",
"makeName": "AMC-EAGLE",
"engineId": 26,
"yearId": 1991
}
我想传递yearId并获得makeId和makeName的所有不同值。
一个sql查询应该是这样的
"Select distinct makeId, makeName from vehicle where yearId =1991".
请告诉我,如何在Elasticsearch(5.1)中实现相同的目标
答案 0 :(得分:0)
您可以使用terms
aggregation来实现:
{
"query": {
"term": {
"yearId": 1991
}
},
"aggs": {
"make_ids": {
"terms": {
"field": "makeId"
}
},
"make_names": {
"terms": {
"field": "makeName"
}
}
}
}
但是,我非常确定您的makeName
字段属于text
类型,因此您无法获得您期望的结果。如果您将该字段改为keyword
,那么您将获得所需的输出。
<强>更新强>
{
"query": {
"term": {
"yearId": 1991
}
},
"aggs": {
"make_ids": {
"terms": {
"field": "makeId"
}
"aggs": {
"make_names": {
"terms": {
"field": "makeName"
}
}
}
}
}
}