Elasticsearch terms使用脚本修改统计的数据源

如:索引中有两个 styleCode(类似spu)跟 goodsCode(类似sku)区分商品;现在需要根据品牌信息为七匹狼的数据使用goodsCode就行统计。

json 复制代码
POST xxx_goods_info/_search
{
	"query": {
		"bool": {
			"must": [{
					"term": {
						"brand": {
							"value": "七匹狼"
						}
					}
				},
				{
					"match": {
						"ngStatus": "sj"
					}
				}
			]
		}

	},
	"aggs": {
		"brand_terms_count": {
			"terms": {
				"script": "if(doc.brand.value=='七匹狼'){return doc.goodsCode}else{return doc.styleCode}",
				"size": 10
			}
		}
	}
}
  1. 查询时使用term和match过滤指定的品牌信息和商品状态
  2. aggs.brand_terms_count 使用script对统计来源进行处理,逻辑较为简单,如果是七匹狼品牌,返回goodsCode,其他使用styleCode。

参数也可以使用params传递,script脚本改成如下:

json 复制代码
POST xxx_goods_info/_search
{
	"query": {
		"bool": {
			"must": [{
					"term": {
						"brand": {
							"value": "七匹狼"
						}
					}
				},
				{
					"match": {
						"ngStatus": "sj"
					}
				}
			]
		}

	},
	"aggs": {
		"brand_terms_count": {
			"terms": {
				"script": {
				  "source": "if(doc.brand.value==params.brand){return doc.goodsCode}else{return doc.styleCode}",
				  "lang": "painless", 
				  "params": {
				    "brand":"七匹狼"
				  }
				},
				"size": 10
			}
		}
	}
}
相关推荐
Elastic 中国社区官方博客1 小时前
Elasticsearch 向量数据库:几分钟内完成部署,以经济高效的方式扩展至数千亿规模
大数据·运维·数据库·elasticsearch·搜索引擎·ai·全文检索
Elasticsearch4 小时前
将 Vercel 数据导入 Elastic:无需安装任何东西的无服务器可观测性
elasticsearch
Elastic 中国社区官方博客18 小时前
Elasticsearch Python DSL 客户端开发
大数据·数据库·python·elasticsearch·搜索引擎·全文检索
阿里云大数据AI技术19 小时前
Al Search x ES Agent Builder:让数据活起来,从搜索走向行动
人工智能·elasticsearch·agent
Elasticsearch21 小时前
Elastic Agent Builder 中能够引用证据的 AI 根因分析
elasticsearch
考虑考虑1 天前
elasticSearch中的element_type
运维·后端·elasticsearch
CHANCE V1 天前
Elasticsearch 进阶
大数据·elasticsearch·搜索引擎
Elasticsearch1 天前
Elasticsearch 向量数据库:几分钟内完成部署,以经济高效的方式扩展至数千亿规模
elasticsearch
vx-Biye_Design2 天前
SSM伴侣动物伴护星小程序06330-计算机课程设计、毕业设计
spring boot·后端·elasticsearch·小程序·架构·课程设计·idea