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

The following docker-compose.yml file configures a single-node 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

Use curl to add a document to each of the users-2020-11 and users-2020-12 indices:

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 searches multiple indices in a single request:

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 a Comma-Separated List

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

Both the wildcard pattern and the explicit comma-separated list return the same hits from users-2020-11 and users-2020-12. The main practical difference is how the target indices are specified and maintained.

A wildcard pattern such as users-2020-* remains the same length as monthly indices accumulate, making it convenient for time-based index names. A comma-separated list is more explicit when the target set is small and known in advance, but the URL length must be considered as the 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.