[Recipes] Querying Arrays of Nested Objects - The Problem

Problem: 

Why cannot you query arrays of objects like other nested objects. Give an example.

Solution Summary: 

Lucene has no concept of inner objects, and hence Elasticsearch flattens object hierarchies into a simple list of field names and values. 

Before:

"employees" : [ 
    {
      "name" : "Abc",
      "age" :  30
    },
    {
      "name" : "Def",
      "age" :  35
    }
  ]

After:

"employees.name" : [ "abc", "def" ],
"employees.age" :  [ 30, 35 ]

Note: The association between Abc and 30 is lost.

Prerequisites: 

Working Elasticsearch setup with Kibana.

Data is created based on the accounts data we were working on from here, but you can follow this lab independently as we are setting up data again.

Solution Steps: 

Data Setup

I will create arrays of employees in two departments.

POST /department/_doc/1{
{
  "name":"accounts",
  "employees": [
    {
      "firstname": "Garrett",
      "lastname": "Langley",
      "age": 36,
      "gender": "M",
      "address": "331 Bowne Street"
    },
    {
      "firstname": "Mallory",
      "lastname": "Emerson",
      "age": 30,
      "gender": "F",
      "address": "318 Dunne Court"
    }
    ]
}

POST /department/_doc/2
{
  "name":"hr",
  "employees": [
    {
      "firstname": "Lavonne",
       "lastname": "Reyes",
       "age": 31,
       "gender": "F",
       "address": "983 Newport Street"
    },
    {
      "firstname": "Lidia",
      "lastname": "Guerrero",
      "age": 30,
      "gender": "M",
      "address": "254 Stratford Road"
    }
    ]
}

 

We will try querying for femal employees with age 30.

GET /department/_doc/_search
{
"query": {
  "bool": {
    "must": [
      { "term": { "employees.age": 30 }},
      { "term": { "employees.gender.keyword":  "F" }} 
      ]
    }
  }
}

Note: There is only one female employee with age 30, and is present in "accounts". However, above query will match both the departments as  the association between gender and age is lost. We will see the solution for this problem in the next recipe.

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