gpt4 book ai didi

node.js - 需要对聚合分组Elasticsearch分页

转载 作者:行者123 更新时间:2023-12-03 02:37:16 25 4
gpt4 key购买 nike

我们已经应用了聚合和分组,为此需要分页。

 let body = {
size: item_per_page,
"query": {
"bool": {
"must": [{
"terms": {
"log_action_master_id": action_type
}
}, {
"match": {
[search_by]: searchParams.user_id
}
}, {
"match": {
"unit_id": searchParams.unit_id
}
},
{
"range": {
[search_date]: {
gte: from,
lte: to
}
}

}
]
}
},
"aggs": {
"group": {
"terms": {
"field": "id",
"size": item_per_page,
"order": { "_key": sortdirction }

},
},
"types_count": {
"value_count": {
"field": "id.keyword"
}
},
},
};

最佳答案

您可以使用以下选项:-

  • Composite Aggregation:可以将多个数据源组合在一个存储桶中,并允许分页和排序。它只能使用after_key线性分页,即,您不能从第1页跳到第3页。您可以获取“n”条记录,然后在key之后传递返回并获取下一个“n”条记录。
  • GET index22/_search
    {
    "size": 0,
    "aggs": {
    "ValueCount": {
    "value_count": {
    "field": "id.keyword"
    }
    },
    "pagination": {
    "composite": {
    "size": 2,
    "sources": [
    {
    "TradeRef": {
    "terms": {
    "field": "id.keyword"
    }
    }
    }
    ]
    }
    }
    }
    }
  • Include partition:在查询时将字段的值分为多个分区,并且每个请求仅处理一个分区。术语字段平均分布在不同的分区中。因此,您必须事先知道术语数量。您可以使用cardinality aggregation获取计数
  • GET index22/_search
    {
    "size": 0,
    "aggs": {
    "TradeRef": {
    "terms": {
    "field": "id.keyword",
    "include": {
    "partition": 0,
    "num_partitions": 3
    }
    }
    }
    }
    }
  • Bucket Sort aggregation:排序其 parent 的水桶多水桶聚结。每个存储桶都可以根据其_key,_count或其子集合进行排序。它仅适用于从父聚合返回的存储桶。您需要将术语大小设置为10,000(最大值),然后在bucket_sort中截断存储桶。您可以像查询一样使用from和size进行分页。如果您有超过10,000个字词,那么您将无法使用它,因为它只能从按字词返回的存储桶中进行选择。
  • GET index22/_search
    {
    "size": 0,
    "aggs": {
    "valueCount":{
    "value_count": {
    "field": "TradeRef.keyword"
    }
    },
    "TradeRef": {
    "terms": {
    "field": "TradeRef.keyword",
    "size": 10000
    },
    "aggs": {
    "my_bucket": {
    "bucket_sort": {
    "sort": [
    {
    "_key": {
    "order": "asc"
    }
    }
    ],
    "from": 2,
    "size": 1
    }
    }
    }
    }
    }
    }

    就性能而言,复合聚合是一个更好的选择

    关于node.js - 需要对聚合分组Elasticsearch分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58638205/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com