Searching Across Multiple Elasticsearch Indices

Searching Across Multiple Elasticsearch Indices

Querying multiple Elasticsearch indices at once with a wildcard pattern versus an explicit comma-separated list.

Takahiro Iwasa
2 min read

Elasticsearch supports querying across several indices at once, either with a wildcard pattern or an explicit comma-separated list.

Launching Elasticsearch

Below is an example docker-compose.yml file to configure and start the cluster.

docker-compose.yml
version: '3'
services:
elasticsearch:
image: elasticsearch:7.10.1
container_name: elasticsearch
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
ports:
- 9200:9200

Start the cluster:

Terminal window
docker-compose up -d

Preparing Data

To experiment with multiple indices, index some data into users-2020-11 and users-2020-12 using the curl command:

Terminal window
curl -X POST -H 'Content-Type: application/json' -d '{"name": "hoge"}' localhost:9200/users-2020-11/_doc/
curl -X POST -H 'Content-Type: application/json' -d '{"name": "fuga"}' localhost:9200/users-2020-12/_doc/

Searching Data

Searching with Wildcards

A wildcard expression matches data across multiple indices in a single query:

Terminal window
curl localhost:9200/users-2020-*/_search | jq .hits.hits

The response will contain data from both users-2020-11 and users-2020-12:

[
{
"_index": "users-2020-11",
"_type": "_doc",
"_id": "PNQ3tXYBKT-fwQ71grcz",
"_score": 1,
"_source": {
"name": "hoge"
}
},
{
"_index": "users-2020-12",
"_type": "_doc",
"_id": "PdQ3tXYBKT-fwQ71p7cy",
"_score": 1,
"_source": {
"name": "fuga"
}
}
]

Searching with CSV Format

Another way to search is by specifying multiple indices as a comma-separated list:

🔥 Caution

Ensure that your URL length does not exceed the maximum limit when using this approach.

Terminal window
curl localhost:9200/users-2020-11,users-2020-12/_search | jq .hits.hits

The response will contain data from both users-2020-11 and users-2020-12:

[
{
"_index": "users-2020-11",
"_type": "_doc",
"_id": "PNQ3tXYBKT-fwQ71grcz",
"_score": 1,
"_source": {
"name": "hoge"
}
},
{
"_index": "users-2020-12",
"_type": "_doc",
"_id": "PdQ3tXYBKT-fwQ71p7cy",
"_score": 1,
"_source": {
"name": "fuga"
}
}
]

Conclusion

Querying users-2020-11 and users-2020-12 with both a wildcard pattern and an explicit comma-separated list returned identical hits, so the real difference between the two approaches shows up only in how the query scales. A wildcard pattern like users-2020-* stays a fixed length no matter how many monthly indices accumulate, which makes it the more durable choice for time-based index naming schemes, while the comma-separated form is easier to reason about when the exact set of indices is small and known in advance — though it’s the one to watch for the URL length limit as that list grows.

About the author

Takahiro Iwasa

Takahiro Iwasa

Software Developer

This blog shares technical notes from hands-on projects—architecture, implementation, and AWS service integrations.