Problem:
Do multi-index search and aggregations in Elasticsearch.
Solution Summary:
We can use the same syntax for regular search and aggregations, but by supplying more than one index seperated by commas.
Prerequisites:
Need to have a working Elasticsearch cluster with Kibana.
Create two documents in two indexes:
PUT /employee/_doc/1?pretty
{
"name": "Heartin",
"role": "Dev"
}
PUT /student/_doc/1?pretty
{
"name": "Heartin",
"role": "Student"
}
Solution Steps:
Multi-index search with request URI
GET /employee,student/_search?q=name:Heartin
Note: Result will have both the records.
Multi-Index Search with request body
GET /employee,student/_search
{
"query": {
"match": { "name": "Heartin" }
}
}
Note: Result will have both the records (same as with request uri)
Multi-Index Aggregation (Value Count)
POST /employee,student/_search?size=0
{
"aggs" : {
"count_agg" : {
"value_count" : {
"field" : "_id"
}
}
}
}
Note: Value count of 2 will be returned.
Also read about Joining queries in Elasticsearch.
Recent comments