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.
Recent comments