Elasticsearch paginating a sorted, aggregated result

后端 未结 2 547
太阳男子
太阳男子 2020-12-11 04:29

As far as I\'m aware, there isn\'t a way to do something like the following in Elasticsearch:

SELECT * FROM myindex
GROUP BY agg_field1, agg_field2, agg_fiel         


        
2条回答
  •  粉色の甜心
    2020-12-11 04:55

    Field collapsing is the answer.

    Field collapsing feature is used when we want to group the hits on a specific field (as in group by agg_field).

    Before Elastic 6, the way to group the fields is to use aggregation. This approach was lacking an ability to do efficient paging.

    But now, with the field collapse provided out of the box by elastic, it is pretty easy.

    Below is a sample query with field collapse taken from above link.

    GET /twitter/_search
    {
      "query": {
          "match": {
              "message": "elasticsearch"
          }
      },
      "collapse" : {
          "field" : "user", 
          "inner_hits": {
              "name": "last_tweets", 
              "size": 5, 
              "sort": [{ "date": "asc" }] 
          },
          "max_concurrent_group_searches": 4 
      },
      "sort": ["likes"]
    

    }

提交回复
热议问题