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
			}
		}
	}
}
相关推荐
深思慎考2 小时前
ElasticSearch与Kibana 入门指南(7.x版本)
大数据·elasticsearch·jenkins
problc8 小时前
PostgreSQL + Redis + Elasticsearch 实时同步方案实践:从触发器到高性能搜索
redis·elasticsearch·postgresql
Elastic 中国社区官方博客9 小时前
如何减少 Elasticsearch 集群中的分片数量
大数据·数据库·elasticsearch·搜索引擎·全文检索
顧棟9 小时前
【ES实战】ES6.8到9.1.4的常用客户端变化
elasticsearch
知识浅谈9 小时前
Elasticsearch 核心知识点全景解读
大数据·elasticsearch·搜索引擎
Komorebi_999910 小时前
Git 常用命令完整指南
大数据·git·elasticsearch
还是鼠鼠11 小时前
《黑马商城》Elasticsearch基础-详细介绍【简单易懂注释版】
java·spring boot·spring·elasticsearch·搜索引擎·spring cloud·全文检索
Cikiss14 小时前
图解 bulkProcessor(调度器 + bulkAsync() + Semaphore)
java·分布式·后端·elasticsearch·搜索引擎
极限实验室15 小时前
Elasticsearch 备份:方案篇
elasticsearch
Elastic 中国社区官方博客1 天前
Elasticsearch:使用推理端点及语义搜索演示
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索