[Recipes] Multi Index Search and Aggregations in Elasticsearch

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.

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream