以下是存盤在 ElasticSearch 中的資料:
[
{
"id": 1,
"class": "class 1",
"name": "Scott",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2011, "score": 80 },
{ "year": 2003, "score": 70 }
]
},
{
"id": 2,
"class": "class 1",
"name": "Gabriel",
"scores": [
{ "year": 2015, "score": 90 },
{ "year": 2011, "score": 70 }
]
},
{
"id": 3,
"class": "class 2",
"name": "Scott",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2021, "score": 100 },
{ "year": 2003, "score": 80 }
]
},
{
"id": 4,
"class": "class 2",
"name": "Pierce",
"scores": [
{ "year": 2022, "score": 70 }
]
}
]
ElasticSearch中有沒有辦法按特定組將分數合并/組合到一個陣列中?(保留重復值)
例如:
- 按班級分組,會顯示班級 1和班級 2的成績,只保留班級和成績欄位,結果會是:
[
{
"class": "class 1",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2015, "score": 90 },
{ "year": 2011, "score": 80 },
{ "year": 2011, "score": 70 },
{ "year": 2003, "score": 70 }
]
},
{
"class": "class 2",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2022, "score": 70 },
{ "year": 2021, "score": 100 },
{ "year": 2003, "score": 80 }
]
}
]
- 按name分組,會將Scott的所有分數放入一個陣列中,只保留name和scores欄位:
[
{
"name": "Scott",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2022, "score": 100 },
{ "year": 2021, "score": 100 },
{ "year": 2011, "score": 80 },
{ "year": 2003, "score": 80 },
{ "year": 2003, "score": 70 }
]
},
{
"name": "Gabriel",
"scores": [
{ "year": 2015, "score": 90 },
{ "year": 2011, "score": 70 }
]
},
{
"name": "Pierce",
"scores": [
{ "year": 2022, "score": 70 }
]
}
]
謝謝!
uj5u.com熱心網友回復:
免責宣告:系好安全帶,這將是冗長的^^
TLDR;
是的,可以使用術語聚合。
例如,按類分組:
您會發現一個名為byClasstype的存盤桶term。Elastic 將在欄位類中為每個值創建檔案桶。
->class 1和class 2
但是你會注意到它在這個聚合中創建了更多的聚合。
- >nestedAGG和byyearbynotes
第一個是Elastic 的特殊性。
其他2個則按年進一步細分,然后分別說明。
GET /71128503/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"byClass": {
"terms": {
"field": "class",
"size": 10
},
"aggs": {
"nestedAGG": {
"nested": {
"path": "scores"
},
"aggs": {
"byyear": {
"terms": {
"field": "scores.year",
"size": 10
},
"aggs": {
"bynotes": {
"terms": {
"field": "scores.score",
"size": 10
}
}
}
}
}
}
}
}
}
}
{
...
"aggregations" : {
"byClass" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "class 1",
"doc_count" : 2,
"nestedAGG" : {
"doc_count" : 5,
"byyear" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 2011,
"doc_count" : 2,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 70,
"doc_count" : 1
},
{
"key" : 80,
"doc_count" : 1
}
]
}
},
{
"key" : 2003,
"doc_count" : 1,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 70,
"doc_count" : 1
}
]
}
},
{
"key" : 2015,
"doc_count" : 1,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 90,
"doc_count" : 1
}
]
}
},
{
"key" : 2022,
"doc_count" : 1,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 100,
"doc_count" : 1
}
]
}
}
]
}
}
},
{
"key" : "class 2",
"doc_count" : 2,
"nestedAGG" : {
"doc_count" : 4,
"byyear" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 2022,
"doc_count" : 2,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 70,
"doc_count" : 1
},
{
"key" : 100,
"doc_count" : 1
}
]
}
},
{
"key" : 2003,
"doc_count" : 1,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 80,
"doc_count" : 1
}
]
}
},
{
"key" : 2021,
"doc_count" : 1,
"bynotes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 100,
"doc_count" : 1
}
]
}
}
]
}
}
}
]
}
}
}
重現
攝取資料。注意自定義映射:
- 關鍵字(不應在文本型別欄位上按術語分組)
- 嵌套(彈性默認平展物件)
PUT /71128503/
{
"settings": {},
"mappings": {
"properties": {
"class": {
"type": "keyword"
},
"name":{
"type": "keyword"
},
"scores":{
"type": "nested",
"properties": {
"score": {
"type": "integer"
},
"year": {
"type": "integer"
}
}
}
}
}
}
POST /_bulk
{"index":{"_index":"71128503","_id":1}}
{"class":"class 1","name":"Scott","scores":[{"year":2022,"score":100},{"year":2011,"score":80},{"year":2003,"score":70}]}
{"index":{"_index":"71128503","_id":2}}
{"class":"class 1","name":"Gabriel","scores":[{"year":2015,"score":90},{"year":2011,"score":70}]}
{"index":{"_index":"71128503","_id":3}}
{"class":"class 2","name":"Scott","scores":[{"year":2022,"score":100},{"year":2021,"score":100},{"year":2003,"score":80}]}
{"index":{"_index":"71128503","_id":4}}
{"class":"class 2","name":"Pierce","scores":[{"year":2022,"score":70}]}
然后查詢資料:
按類別/按名稱
GET /71128503/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"byName": { <- Name of your bucket
"terms": { <- Type of grouping, Elastic support many, like sum, avg on numeric value ....
"field": "name", <- Field you grouping on
"size": 10
},
"aggs": {
"nestedAGG": {
"nested": {
"path": "scores"
},
"aggs": {
"byyear": {
"terms": {
"field": "scores.year",
"size": 10
},
"aggs": {
"bynotes": {
"terms": {
"field": "scores.score",
"size": 10
}
}
}
}
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425454.html
