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_teamsPUT 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:
-
This query will look for all terms within "members" for the given field and match it against "name.keyword".
-
We use the keyword type for name as "terms" queries are not analyzed.
Recent comments