Elasticsearch Elasticsearch Query Match All

By Opster Expert Team - May 2023

Updated: Aug 17, 2023

| 2 min read

Quick links

Introduction

What is the Elasticsearch query match all?

The match_all query is a simple yet essential component of Elasticsearch, as it allows users to retrieve all documents within an index or multiple indices. This query is particularly useful when you need to perform operations on all documents, such as applying filters, aggregations, or sorting.

In this article, we will explore the match_all query in detail, including its syntax, usage, and examples.

Syntax and Usage

The match_all query is straightforward to use, as it does not require any specific field or value to match. Here is the basic syntax for the match_all query:

GET /_search
{
  "query": {
    "match_all": {}
  }
}

By default, the match_all query returns the first 10 documents in the index, sorted by their relevance score. However, you can customize the number of documents returned and their sorting order using the “size” and “sort” parameters, respectively.

Examples

1. Retrieve all documents from an index:

To retrieve all documents from a specific index, simply replace the index name in the request URL. For example, to get all documents from the “products” index, use the following query:

GET /products/_search
{
  "query": {
    "match_all": {}
  }
}

2. Retrieve documents from multiple indices:

To retrieve documents from multiple indices, you can use a comma-separated list of index names or a wildcard pattern. For example, to get all documents from the “products” and “orders-*” indices (assuming we’ll have a new order index created every day and they will be named something like “orders-2023.05.05”), use the following query:

GET /products,orders-*/_search
{
  "query": {
    "match_all": {}
  }
}

3. Customize the number of documents returned:

By default, Elasticsearch returns the first 10 documents. To change this, use the size parameter. For example, to retrieve the first 50 documents, use the following query:

GET /_search
{
  "size": 50,
  "query": {
    "match_all": {}
  }
}

4. Sort documents by a specific field:

To sort the documents by a specific field, use the sort parameter. For example, to sort the documents by the “price” field in ascending order, use the following query:

GET /_search
{
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "match_all": {}
  }
}

5. Combine match_all with other queries:

You can also combine the match_all query with other queries, such as filters or aggregations. For example, to retrieve all documents with a “price” greater than 100 and calculate the average price, use the following query:

GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "price": {
            "gt": 100
          }
        }
      }
    }
  },
  "aggs": {
    "average_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}

You can learn more about more specific types of match queries by taking a look at this guide, which contains a very detailed explanation on how the match, multi_match and match_phrase work.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?