[Recipes] Terms Lookup Mechanism (Multi-Index Relations)

Problem: 

Need to lookup terms from a document in another index and do queries in current index based on that.

Solution Summary: 

We can use the terms lookup mechanism to specify a terms filter with a lot of terms from documents in another index. 

Prerequisites: 

Working Elasticsearch and Kibana.

If following indexes exist, delete them as follows:

  • PUT hackathon_employees

  • PUT hackathon_teams

Solution Steps: 

Data Setup

PUT hackathon_employees
PUT hackathon_teams

PUT hackathon_employees/_doc/1
{
  "name": "Heartin",
  "empid" : "xxx"
}

PUT hackathon_employees/_doc/2
{
  "name": "KK",
  "empid" : "yyy"
}

PUT hackathon_employees/_doc/3
{
  "name": "John Doe",
  "empid" : "zzz"
}

PUT hackathon_teams/_doc/1
{
  "name": "Cloudericks",
  "members" : ["Heartin" , "KK"]
}

 

Cross index join search (terms lookup)

GET hackathon_employees/_search
{
  "query": {
    "terms": {
      "name.keyword": {
        "index": "hackathon_teams",
        "type": "_doc",
        "id" : "1",
        "path": "members"
      }
    }
  }
}

Note:

  1. This query will look for all terms within "members" for the given field and match it against "name.keyword".

  2. We use the keyword type for name as "terms" queries are not analyzed.

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