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
Below is an example docker-compose.yml file to configure and start the 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
To experiment with multiple indices, index some data into users-2020-11 and users-2020-12 using the curl command:
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:
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 CSV Format
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
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.
Related posts
Querying Amazon Neptune with Gremlin
This note shows how to query Amazon Neptune graph data using Gremlin.
Calculating Distances Between Geographic Points with MySQL 5.7
MySQL 5.7 provides the STDistanceSphere function for calculating the great-circle distance between two geographic points.
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
This example guides you through the process of developing API backends with FastAPI using Lambda Web Adapter.
API Gateway WebSocket: Implementing a Mock Integration
Building an API Gateway WebSocket API entirely with mock integrations, returning canned responses with no backend Lambda involved.
