elasticsearch - 如何计算 Elasticsearch 中每个产品的平均评分
问题描述
我有products
以下映射的索引:
{
"products" : {
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
},
"name":{
"type": "text"
},
"price" : {
"type" : "integer"
},
"product_review_rel" : {
"type" : "join",
"eager_global_ordinals" : true,
"relations" : {
"product" : "review"
}
},
"rate" : {
"type" : "integer"
}
}
}
}
}
该索引包含产品和评论,您可以在product_review_rel
字段中看到。产品包含名称、价格、...字段。评论包含费率、... 字段。
我想获得每种产品的平均评分。怎么做?另一个问题,是否可以在同一请求中从以下查询返回的产品中返回平均评分:
{
"query": {
"nested": {
"path": "translations",
"query": {
"multi_match": {
"query": "kem chống nắng",
"fields": [
"name"
],
"analyzer":"vi_analyzer"
}
}
}
}
}
更新 1:复合聚合
GET products/_search
{
"query": {
"nested": {
"path": "translations",
"query": {
"multi_match": {
"query": "kem chống nắng",
"fields": [
"translations.name",
"translations.description"
],
"analyzer": "vi_analyzer"
}
}
}
},
"aggs": {
"products": {
"composite": {
"sources": [
{
"id": {
"terms": {
"field": "_id"
}
}
}
]
},
"aggs": {
"reviews": {
"children": {
"type": "review"
},
"aggs": {
"rating": {
"avg": {
"field": "rate"
}
}
}
}
}
}
}
}```
解决方案
推荐阅读
- tensorflow - Colab 资源和 Self-Attention(分配张量时的 OOM)
- java - Java:在某个字符之前获取子字符串
- c++ - 如何访问使用创建的列表中的结构元素
- 图书馆?
- css - Angular Material Buttons 与主题不匹配
- ibm-cloud - IBM Watson Assistant:检查输入日期是否大于或小于 18 岁的条件
- python - 是否有一个函数可以在 pandas 单元格列表中搜索一个值,如果该值是单独的或唯一的类型,则返回行?
- julia - 为什么@objective 值为负数?
- powershell - 当 IE 安全级别区域为高时,Connect-PnPOnline -UseWebLogin 登录弹出窗口不加载
- c - 如何使用 SSE 检查字符串是否包含一些字母
- json - 可以在 json.dump 中使用 open inline