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
			}
		}
	}
}
相关推荐
小獾哥4 小时前
Centos8系统在安装Git包时,报错:“没有任何匹配: git”
大数据·git·elasticsearch
null不是我干的4 小时前
黑马SpringBoot+Elasticsearch作业2实战:商品搜索与竞价排名功能实现
spring boot·后端·elasticsearch
光仔December16 小时前
【Elasticsearch入门到落地】16、RestClient查询文档-快速入门
elasticsearch·全文检索·restclient·highlevelclient·match查询
Elasticsearch16 小时前
Kibana:如何使用企业微信来接收通知
elasticsearch
Elasticsearch2 天前
哈希、存储、连接:使用 ES|QL LOOKUP JOIN 的日志去重现代解决方案
elasticsearch
yangmf20402 天前
Easysearch 冷热架构实战
java·大数据·elasticsearch·搜索引擎
极限实验室2 天前
ES 调优帖:Gateway 批量写入性能优化实践
elasticsearch
wdfk_prog4 天前
实战教程:从“对象文件为空“到仓库重生——修复 Git 仓库损坏全记录
大数据·网络·笔记·git·学习·elasticsearch·全文检索
Hello.Reader4 天前
以 Eland 玩转 Elasticsearch 8.12 Learning-to-Rank
大数据·elasticsearch·jenkins
ALex_zry4 天前
Git Status 命令深度指南:洞悉仓库状态的核心艺术
大数据·git·elasticsearch