我的弹性搜索索引中有这个映射模型:
{
"my_index": {
"mappings": {
"vehicules": {
"properties": {
"name": {
"type": "text"
},
"category_id": {
"type": "integer"
},
"price": {
"type": "float"
},
}
}
}
}
}
在这个索引中,我插入了一些演示数据:
+--------------+-------------+-------+
| Name | Category ID | Price |
+--------------+-------------+-------+
| Car 1 | 1 | 1500 |
| Car 2 | 1 | 4000 |
| Car 3 | 1 | 2500 |
| Motorcycle 1 | 2 | 3000 |
| Motorcycle 2 | 2 | 1400 |
| Motorcycle 3 | 2 | 2700 |
| Truck 1 | 3 | 19000 |
| Truck 2 | 3 | 15000 |
+--------------+-------------+-------+
我想根据价格值ASC对所有产品进行排序,并按类别对结果进行分组。类别本身必须使用其子数据的价格值进行排序。哪个给:
{
"2": [ <= Category where the price start with lower price (1400)
{
"name": "Motorcycle 2",
"price": 1400
},
{
"name": "Motorcycle 3",
"price": 2700
},
{
"name": "Motorcycle 1",
"price": 3000
}
],
"1": [
{
"name": "Car 1",
"price": 1500
},
{
"name": "Car 3",
"price": 2500
},
{
"name": "Car 2",
"price": 4000
}
],
"3": [
{
"name": "Truck 2",
"price": 15000
},
{
"name": "Truck 1",
"price": 19000
}
]
}
是否有可能通过ES获得那种结果或与之相近的结果?我是ES的初学者,我在Kibana的DevTool中尝试了很多不同的查询,没有成功。
我认为我发现查询具有所需的结果。我不确定它是否已经完全优化,但它确实有效。
GET my_index/my_type/_search
{
"size": 0,
"aggs": {
"grouped_by_cat": {
"terms": {
"field": "category_id",
"order": {
"min_price_aggs": "asc"
}
},
"aggs": {
"min_price_aggs": {
"min": {
"field": "price"
}
},
"list_top_hits": {
"top_hits": {
"_source": {
"includes": [
"name",
"price"
]
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"size": 10000
}
}
}
}
}
}
这个查询对你来说是否正确?
答案 0 :(得分:0)
因此,您可以通过以下查询
来实现上述结果GET my_index/vehicles/_search
{
"size": 0,
"aggs" : {
"category_id_aggs": {
"terms" : {
"field" : "category_id"
},
"aggs": {
"data": {
"top_hits": {
"size": 10,
"sort": [{"price":"asc"}],
"_source": {
"includes" : ["name","price"]
}
}
}
}
}
}
}