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
			}
		}
	}
}
相关推荐
Luck_ff08104 小时前
Elasticsearch 快速入门指南
大数据·elasticsearch·搜索引擎
言之。4 小时前
Makefile 在 Go 项目中的实践
开发语言·elasticsearch·golang
好吃的肘子7 小时前
ElasticSearch进阶
大数据·开发语言·分布式·算法·elasticsearch·kafka·jenkins
老友@7 小时前
Spring Data Elasticsearch 中 ElasticsearchOperations 构建查询条件的详解
java·后端·spring·elasticsearch·operations
Clown958 小时前
go-zero(十八)结合Elasticsearch实现高效数据检索
开发语言·elasticsearch·golang
愚者大大9 小时前
小白入门:GitHub 远程仓库使用全攻略
大数据·elasticsearch·搜索引擎
程序员沉梦听雨10 小时前
【Elasticsearch】DSL 篇
elasticsearch
会飞的架狗师1 天前
【SpringBoot实战指南】集成Easy ES
spring boot·elasticsearch
Elastic 中国社区官方博客1 天前
在 Elasticsearch 中删除文档中的某个字段
大数据·数据库·elasticsearch·搜索引擎
lizz6662 天前
Python查询ES错误ApiError(406, ‘Content-Type ...is not supported
python·elasticsearch