How to Use match_all Query in Elasticsearch: Syntax and Examples
Use the
match_all query in Elasticsearch to retrieve all documents from an index without filtering. It is a simple query that matches every document and is often used for testing or retrieving full data sets.Syntax
The match_all query has a simple JSON structure. It contains a query object with match_all as the key. Optionally, you can add a boost value to influence scoring.
- query: The main container for the query.
- match_all: The query type that matches all documents.
- boost (optional): A number to increase or decrease the relevance score.
json
{
"query": {
"match_all": {
"boost": 1.0
}
}
}Example
This example shows how to use the match_all query to retrieve all documents from an Elasticsearch index named products. It returns all documents with their default scores.
json
GET /products/_search
{
"query": {
"match_all": {}
}
}Output
{
"hits": {
"total": {"value": 3, "relation": "eq"},
"hits": [
{"_id": "1", "_source": {"name": "Laptop", "price": 1200}},
{"_id": "2", "_source": {"name": "Phone", "price": 800}},
{"_id": "3", "_source": {"name": "Tablet", "price": 450}}
]
}
}
Common Pitfalls
One common mistake is to expect match_all to filter or sort documents. It does not filter; it returns everything. Another pitfall is forgetting to wrap match_all inside the query object, which causes syntax errors.
Also, using match_all on very large indexes without pagination can overload your system.
json
{
"match_all": {}
}
{
"query": {
"match_all": {}
}
}Quick Reference
| Feature | Description |
|---|---|
| match_all | Matches all documents in the index |
| boost | Optional score multiplier to influence ranking |
| Usage | Must be inside a 'query' object |
| Performance | Use with pagination on large datasets |
Key Takeaways
The match_all query returns every document in the index without filtering.
Always place match_all inside the query object to avoid syntax errors.
Use boost to adjust the relevance score if needed.
Avoid retrieving large datasets without pagination to prevent performance issues.
match_all is useful for testing or when you need all documents.