溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ElasticSearch中g(shù)roup by + avg + sort等聚合分析是什么樣的

發(fā)布時(shí)間:2021-10-20 10:49:51 來源:億速云 閱讀:178 作者:柒染 欄目:大數(shù)據(jù)

本篇文章為大家展示了ElasticSearch中g(shù)roup by + avg + sort等聚合分析是什么樣的,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

將文本fields的Fielddata屬性設(shè)置true

PUT http://{{es-host}}/ecommerce/_mapping/produce
{
	"properties":{
		"tags":{
			"type":"text",
			"fielddata":true
		}
	}
}

1、計(jì)算每個(gè)tag下的商品數(shù)量

GET http://{{es-host}}/ecommerce/produce/_search
{
	"size":0,
	"aggs":{
		"group_by_tags":{
			"terms":{
				"field":"tags"
			}
		}
	}
}

group_by_tags 代表聚合分組名稱,可以隨意寫,表述清楚含義即可;

field的值對應(yīng)要聚合的字段

結(jié)果:

{
    "took": 43,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_tags": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "fangzhu",
                    "doc_count": 2
                },
                {
                    "key": "meibai",
                    "doc_count": 2
                },
                {
                    "key": "qingxin",
                    "doc_count": 1
                }
            ]
        }
    }
}

2、按商品名稱搜索并聚合

GET http://{{es-host}}/ecommerce/produce/_search
{
	"query":{
		"match_phrase":{
			"name":"yagao"
		}
	},
	"aggs":{
		"group_by_tags":{
			"terms":{
				"field":"tags"
			}
		}
	},
	"size":0
}

檢索結(jié)果:

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_tags": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "fangzhu",
                    "doc_count": 2
                },
                {
                    "key": "meibai",
                    "doc_count": 2
                },
                {
                    "key": "qingxin",
                    "doc_count": 1
                }
            ]
        }
    }
}

3、先分組,再計(jì)算每個(gè)分組的平均值

GET http://{{es-host}}/ecommerce/produce/_search
{
	"size":0,
	"aggs":{
		"group_by_tags":{
			"terms":{
				"field":"tags"
			},
			"aggs":{
				"avg_price":{
					"avg":{
						"field":"price"
					}
				}
			}
		}
	}
}

結(jié)果:

{
    "took": 83,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_tags": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "fangzhu",
                    "doc_count": 2,
                    "avg_price": {
                        "value": 27.5
                    }
                },
                {
                    "key": "meibai",
                    "doc_count": 2,
                    "avg_price": {
                        "value": 40
                    }
                },
                {
                    "key": "qingxin",
                    "doc_count": 1,
                    "avg_price": {
                        "value": 40
                    }
                }
            ]
        }
    }
}

上述內(nèi)容就是ElasticSearch中g(shù)roup by + avg + sort等聚合分析是什么樣的,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI