Searching Across Multiple Elasticsearch Indices
Querying multiple Elasticsearch indices at once with a wildcard pattern versus an explicit comma-separated list.
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.
version: '3'
services: elasticsearch: image: elasticsearch:7.10.1 container_name: elasticsearch environment: - discovery.type=single-node - bootstrap.memory_lock=true ports: - 9200:9200Start the cluster:
docker-compose up -dPreparing Data
Use curl to add a document to each of the users-2020-11 and users-2020-12 indices:
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:
curl localhost:9200/users-2020-*/_search | jq .hits.hitsThe 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:
Ensure that your URL length does not exceed the maximum limit when using this approach.
curl localhost:9200/users-2020-11,users-2020-12/_search | jq .hits.hitsThe 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.
Related posts
Querying Amazon Neptune with Gremlin
Load property graph data into Amazon Neptune and query relationships with Gremlin traversals.
Calculating Distances Between Geographic Points with MySQL 5.7
MySQL 5.7 provides the ST_Distance_Sphere function for calculating the great-circle distance between two geographic points.
Improving Cross-Team Communication with C4 Diagrams
Learn the characteristics of the C4 model, how it differs from UML and infrastructure diagrams, and how to communicate architecture at the right level for each role.
Sign in with Slack Using Cognito User Pools and OIDC
Federating Cognito user pools with Slack over OIDC and wiring "Sign in with Slack" into a Next.js app with Amplify.
Deploying FastAPI on AWS Lambda with Lambda Web Adapter
Containerizing a FastAPI backend and deploying it to a single Lambda function with Lambda Web Adapter and AWS CDK.
