Elasticsearch Query All Data: Complete Guide

In Elasticsearch, you can use the query API to search all data under an index. There are multiple ways to achieve this goal.

  1. Perform a match_all query: This is a simple query that returns all documents in the index. An example request is shown below:
GET /your_index/_search
{
  "query": {
    "match_all": {}
  }
}
  1. Utilize the scroll API for pagination: When there is a large amount of data in an index, a single query may not be able to return all results. You can use the scroll API for pagination in order to gradually retrieve all data.示例请求如下:
POST /your_index/_search?scroll=1m
{
  "size": 100,
  "query": {
    "match_all": {}
  }
}

Next, you can use the scroll_id to retrieve the results of the next page until all data is retrieved.

GET /_search/scroll
{
  "scroll": "1m",
  "scroll_id": "your_scroll_id"
}
  1. Performing paginated queries using the scan and scroll API (for older versions): Although the scan and scroll API has been deprecated in newer versions of Elasticsearch, you can still utilize this method if you are using an older version. Here is an example request:
POST /your_index/_search?search_type=scan&scroll=1m
{
  "size": 100,
  "query": {
    "match_all": {}
  }
}

Next, you can use the scan and scroll parameters to fetch the next page of results until all the data is retrieved.

GET /_search/scroll?scroll=1m&scroll_id=your_scroll_id

Here are several methods for querying and retrieving all data under an index – choose the most suitable method based on your specific situation.

bannerAds