elasticsearch嵌套查询聚合数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch嵌套查询聚合数据相关的知识,希望对你有一定的参考价值。

先展示数据结构如下:

{
"itemId": "132417862061",
"site": "US",
"sellerId": "aaaaa",
"increamentId": "3753595",
"ebayTrackList": [
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2017-12-15"
	},
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2017-12-29"
	},
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2018-02-03"
	},
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2018-03-12"
	},
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2018-03-20"
	},
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2018-03-24"
	},
	{
		"numCount": 0,
		"pageView": 2,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2018-04-01"
	},
	{
		"numCount": 0,
		"pageView": 1,
		"userView": 1,
		"amountPaid": 0,
		"conversionRate": 0,
		"date": "2018-04-02"
	}
],
"publisher": "2714",
"title": "MOTOSPEED LED Backlight 87 keys Bluetooth Mechanical Keyboard Red Switches T1W8",
"sku": [
"C5060"
],
"img": "https://i.ebayimg.com/00/s/MTAwMFgxMDAw/z/dy8AAOSw63FaH6zU/$_1.JPG?set_id=8800005007",
"price": 55.05,
"status": 1,
"currency": "USD",
"createTime": "1512074612",
"updateTime": "1522694170",
"mark": null,
"fromCountry": "Shen Zhen",
"oldSku": null
}


目前的需求是假如我要知道2018-04-02,2018-04-03号的pageView之和,很明显需要ebayTrackList.date过滤求和ebayTrackList.pageView字段


语句如下

{
	"query":{
		"bool" : {
            		"must" : [
            			{
            				"term" : {"itemId":"132417862061"}
            			},
            			{
            				"nested" : {
            					"path" : "ebayTrackList",
            					"query" : {
            						"bool" : {
            							"must" : [
            								{ "terms" : {"ebayTrackList.date" : ["2018-04-03","2018-04-02"]} }
            							]
            						}
            					}
            				}
            			}
            		]
		}
	},
	"aggs":{
		"numTotalCount" : {
                        "nested" : {
                            "path" : "ebayTrackList"
                        },
                        "aggs" : {
                        	"sd_value" : {
                        		"filter" : { "terms" : {"ebayTrackList.date" : ["2018-04-03","2018-04-02"]} },
                        		"aggs" : {
                        			"sum_count" : { "sum" : { "field" : "ebayTrackList.pageView" } }
                        		}
                        	}
                        }
        }
	}
}


以上是关于elasticsearch嵌套查询聚合数据的主要内容,如果未能解决你的问题,请参考以下文章

elasticsearch aggs 嵌套查询 多个select 统计/聚合部分查询结果

ElasticSearch 聚合嵌套查询

跟我学Elasticsearch(7) es的嵌套聚合,下钻分析,聚合分析

Elasticsearch - 计算嵌套聚合相对于父存储桶的百分比

elasticsearch无痛组合聚合密钥及其嵌套聚合密钥

七.全文检索ElasticSearch经典入门-聚合查询