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 中国社区官方博客9 小时前
在 Windows 上使用 Docker 运行 Elastic Open Crawler
大数据·windows·爬虫·elasticsearch·搜索引擎·docker·容器
Britz_Kevin15 小时前
从零开始的云计算生活——第三十三天,关山阻隔,ELK日志分析
elasticsearch·#elk·#logstash·#kibana
Elasticsearch15 小时前
AI 驱动的仪表板:从愿景到 Kibana-CSDN博客
elasticsearch
EmpressBoost2 天前
docker run elasticsearch 报错
elasticsearch·docker·容器
不平衡的叉叉树2 天前
ES组合使用must与should时的注意事项
elasticsearch
小阳睡不醒2 天前
小白成长之路-Elasticsearch 7.0 配置
大数据·elasticsearch·excel
极限实验室2 天前
Console 纳管 Elasticsearch 9(二):日志监控
数据库·elasticsearch
Roc-xb2 天前
如何关闭Elasticsearch的安全认证的解决方法
java·elasticsearch
TinpeaV3 天前
Elasticsearch8 Windows安装教程
windows·spring boot·后端·elasticsearch
舒一笑3 天前
Elasticsearch 的模糊查询相关概念
elasticsearch