我必须建立一个价格比较系统。我的想法是使用Elasticsearch进行构建。
现在我有这个问题。如何汇总每种产品的卖方价格。
作为示例,请参见以下屏幕截图:
让我说我有一个简单的映射:
products: {
product: {
properties: {
id: {
type: "long"
},
name: {
type: "string"
},
....
sellers: {
dynamic: "true",
properties: {
sellerId: {
type: "long"
},
price: {
type: "float"
}
}
}
}
}
}
我可以汇总或分摊每种产品的价格(最低,最高和卖家计数)吗?
还是有办法用亲子关系来构建这个东西?
最佳答案
假设您使用的是1.0而不是0.90,那么可以使用min,max和value_count聚合轻松地做到这一点。
{
"query": {
"match": {
"name": "item1"
}
},
"aggs": {
"Min": {
"min": {
"field": "sellers.price"
}
},
"Max": {
"max": {
"field": "sellers.price"
}
},
"SellerCount": {
"value_count": {
"field": "sellers.sellerId"
}
}
}
}
或者,您可以使用子聚合返回每种产品而不是特定产品的信息。
{
"aggs": {
"Products": {
"terms": {
"field": "name",
"size": 10
},
"aggs": {
"Min": {
"min": {
"field": "sellers.price"
}
},
"Max": {
"max": {
"field": "sellers.price"
}
},
"SellerCount": {
"value_count": {
"field": "sellers.sellerId"
}
}
}
}
}
}
关于elasticsearch - 如何使用Elasticsearch建立价格比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22696958/