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
			}
		}
	}
}
相关推荐
OYangxf18 小时前
Git Ignore
大数据·git·elasticsearch
Elastic 中国社区官方博客19 小时前
jina-embeddings-v5-omni:用于文本、图像、音频和视频的 embeddings
大数据·人工智能·elasticsearch·搜索引擎·ai·音视频·jina
泓博19 小时前
Openclaw-Ubuntu常用命令
大数据·elasticsearch·搜索引擎·ai
WhoAmI19 小时前
Elasticsearch实战指南:构建实时全文检索系统
elasticsearch·kafka
Elastic 中国社区官方博客21 小时前
Elasticsearch ES|QL “读取时模式”:你的未映射字段一直都在那里
大数据·数据库·sql·elasticsearch·搜索引擎·全文检索
Elastic 中国社区官方博客21 小时前
Elasticsearch 查询日志:每个查询一行协调器级别日志,适用于 ES|QL、DSL、SQL 和 EQL
大数据·数据库·sql·elasticsearch·搜索引擎·全文检索·可用性测试
risc1234562 天前
Elasticsearch的shrink为啥不用软链接用硬链接
elasticsearch·lucene
Elasticsearch2 天前
jina-embeddings-v5-omni:用于文本、图像、音频和视频的 embeddings
elasticsearch
森屿~~2 天前
CMA-ES:从搜索分布自适应到协方差矩阵学习
学习·elasticsearch·矩阵
剑神一笑2 天前
Linux tree 命令深度解析:从目录遍历到树形可视化的完整实现
linux·运维·elasticsearch