PUT my-data-stream/_bulk
{"create":{}}
{"timestamp":"2022-05-06T18:25:42","search_term":"hello", "counter": 10}
{"create":{}}
{"timestamp":"2022-05-06T18:25:42","search_term":"bye", "counter": 5}
{"create":{}}
{"timestamp":"2022-05-06T17:25:42","search_term":"hello", "counter": 9}
{"create":{}}
{"timestamp":"2022-05-06T17:25:42","search_term":"bye", "counter": 7}
{"create":{}}
{"timestamp":"2022-05-06T16:25:42","search_term":"hello", "counter": 5}
{"create":{}}
{"timestamp":"2022-05-06T16:25:42","search_term":"bye", "counter": 2}
鑒于上述資料集。我想按大于特定時間戳的計數器的總和對搜索詞進行排序(DESC)。
例如:對于大于2022-05-06T16:35:42(前 4 條記錄)的時間戳。結果應該是
Hello, 19 (10 9)
Bye, 12 (5 7)
uj5u.com熱心網友回復:
特長;
您可以order在聚合中使用您的資料進行排序。
根據此處的檔案
在這種情況下,存盤桶按實際術語值排序,例如關鍵字的字典順序或數字的數字順序。這種排序在升序和降序上都是安全的,并產生準確的結果。
解決方案
GET /74006495/_search
{
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"timestamp": {
"gte": "2022-05-06T16:35:42"
}
}
}
]
}
},
"aggs": {
"search_terms": {
"terms": {
"field": "search_term.keyword",
"size": 10,
"order": { "count": "desc" } // <- Here is the order by count agg
},
"aggs": {
"count": {
"sum": {
"field": "counter"
}
}
}
}
}
}
uj5u.com熱心網友回復:
我使用了按日期過濾,然后聚合結果。
{
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"timestamp": {
"gte": "2022-05-06T16:35:42"
}
}
}
]
}
},
"aggs": {
"search_by_day": {
"date_histogram": {
"field": "timestamp",
"calendar_interval": "day"
},
"aggs": {
"search_term": {
"terms": {
"field": "search_term.keyword",
"size": 10
},
"aggs": {
"total_counter": {
"sum": {
"field": "counter"
}
},
"counter_bucket_sort": {
"bucket_sort": {
"sort": [
{
"total_counter": {
"order": "desc"
}
}
],
"size": 3
}
}
}
}
}
}
}
}
結果:
"aggregations": {
"search_by_day": {
"buckets": [
{
"key_as_string": "2022-05-06T00:00:00.000Z",
"key": 1651795200000,
"doc_count": 4,
"search_term": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "hello",
"doc_count": 2,
"total_counter": {
"value": 19
}
},
{
"key": "bye",
"doc_count": 2,
"total_counter": {
"value": 12
}
}
]
}
}
]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/513910.html
標籤:弹性搜索
上一篇:如何正確使用空白分析器?
