简体   繁体   中英

How to do pagination (from, size) with elastic search aggregation query in nodejs

I am currently have an API that returns user usage logs to front end but the data is too large so I need pagination. I am not able to do pagination in elastic search aggregation query. Currently using this code

const result = await esClient.search({
      index: 'user-logs',
      body: {
        size: 0,
        query: {
          bool: {
            filter: [
              {
                range: {
                  timestamp: {
                    gte: 1596804771643,
                    lte: 1596904771643,
                    format: 'epoch_millis'
                  }
                }
              },
              {
                query_string: {
                  analyze_wildcard: true,
                  query: `*`
                }
              }
            ]
          }
        },
        aggs: {
          3: {
            terms: {
              field: 'email.keyword',
              size: 10,
              order: {
                _term: 'desc'
              },
              min_doc_count: 1
            },
            aggs: {
              4: {
                terms: {
                  field: 'mobile.keyword',
                  order: {
                    _term: 'desc'
                  },
                  min_doc_count: 1
                },
                aggs: {
                  5: {
                    terms: {
                      field: 'fullName.keyword',
                      order: {
                        _term: 'desc'
                      },
                      min_doc_count: 1
                    },
                    aggs: {
                      6: {
                        terms: {
                          field: 'branchName.keyword',
                          order: {
                            _term: 'desc'
                          },
                          min_doc_count: 1
                        },
                        aggs: {
                          1: {
                            sum: {
                              field: 'timespent'
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    });

In simple query, I use "from" and "size", but in case of aggregation query, they are not working. I am new to Elastic Search.

The terms doesn't support pagination.

Look into composite aggregations w/ sub-aggregations which indeed do support pagination using after_key .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM