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
			}
		}
	}
}
相关推荐
Elasticsearch2 小时前
使用 Elasticsearch 和 Microsoft 的 Agent Framework 构建智能代理应用
elasticsearch
阿Y加油吧3 小时前
快速入门elasticsearch
elasticsearch
Elastic 中国社区官方博客3 小时前
在 Elastic Observability 中,启用 TSDS 集成可节省高达 70% 的指标存储
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索·时序数据库
半梦半醒*3 小时前
ELK2——logstash
linux·运维·elk·elasticsearch·centos·1024程序员节
会飞的小蛮猪3 小时前
ELK运维之路(Logstash-插件)
运维·经验分享·elk·elasticsearch·logstash
半梦半醒*3 小时前
ELK3——kibana
linux·运维·elasticsearch·centos·gitlab
CodeAmaz3 小时前
ELK(Elasticsearch + Logstash + Kibana + Filebeat)采集方案
java·elk·elasticsearch·1024程序员节
数据智能老司机3 小时前
OpenSearch 权威指南——OpenSearch 概览
elasticsearch·搜索引擎·aws
我要升天!20 小时前
Git的原理与使用 -- 基础操作
大数据·服务器·git·elasticsearch