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
			}
		}
	}
}
相关推荐
华农第一蒟蒻13 小时前
Elasticsearch赋能3D打印机任务统计分析
java·大数据·spring boot·后端·elasticsearch·adb·maven
Elasticsearch14 小时前
转变数据交互:在 Amazon Bedrock AgentCore Runtime 上部署 Elastic 的 MCP 服务器以构建 agentic AI 应用
elasticsearch
赤月幼狼19 小时前
elasticsearch学习(五)文档CRUD
学习·elasticsearch
在未来等你20 小时前
Elasticsearch面试精讲 Day 8:聚合分析与统计查询
大数据·分布式·elasticsearch·搜索引擎·面试
知彼解己1 天前
Elasticsearch 核心知识与常见问题解析
大数据·elasticsearch·搜索引擎
Mr.Entropy2 天前
Elasticsearch(text和keyword)区别分析
大数据·elasticsearch·搜索引擎
Cosmoshhhyyy2 天前
Elasticsearch常用DSL快速查询指南
java·elasticsearch
可以吧可以吧2 天前
自建es 通过Flink同步mysql数据 Docker Compose
mysql·elasticsearch·flink
赤月幼狼2 天前
elasticsearch学习(一) 下载、安装和初次部署
学习·elasticsearch·jenkins
Elasticsearch2 天前
Elasticsearch:向量搜索过滤 - 保持相关性
elasticsearch