Created
February 29, 2020 18:18
-
-
Save VarunVats9/c2d561e8312326793c381edfc872ca4a to your computer and use it in GitHub Desktop.
Elasticsearch - nested objects, nested inner hits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mapping | |
PUT /departments >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"mappings": { | |
"properties": { | |
"name": { | |
"type": "text" | |
}, | |
"employees": { | |
"type": "nested" | |
} | |
} | |
} | |
} | |
{ | |
"acknowledged" : true, | |
"shards_acknowledged" : true, | |
"index" : "departments" | |
} | |
// Test documents | |
PUT /departments/_doc/1 | |
{ | |
"name": "Development", | |
"employees": [ | |
{ | |
"name": "Eric Green", | |
"age": 39, | |
"gender": "M", | |
"position": "Big Data Specialist" | |
}, | |
{ | |
"name": "James Taylor", | |
"age": 27, | |
"gender": "M", | |
"position": "Software Developer" | |
}, | |
{ | |
"name": "Gary Jenkins", | |
"age": 21, | |
"gender": "M", | |
"position": "Intern" | |
}, | |
{ | |
"name": "Julie Powell", | |
"age": 26, | |
"gender": "F", | |
"position": "Intern" | |
}, | |
{ | |
"name": "Benjamin Smith", | |
"age": 46, | |
"gender": "M", | |
"position": "Senior Software Engineer" | |
} | |
] | |
} | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "1", | |
"_version" : 1, | |
"result" : "created", | |
"_shards" : { | |
"total" : 2, | |
"successful" : 2, | |
"failed" : 0 | |
}, | |
"_seq_no" : 0, | |
"_primary_term" : 1 | |
} | |
PUT /departments/_doc/2 | |
{ | |
"name": "HR & Marketing", | |
"employees": [ | |
{ | |
"name": "Patricia Lewis", | |
"age": 42, | |
"gender": "F", | |
"position": "Senior Marketing Manager" | |
}, | |
{ | |
"name": "Maria Anderson", | |
"age": 56, | |
"gender": "F", | |
"position": "Head of HR" | |
}, | |
{ | |
"name": "Margaret Harris", | |
"age": 19, | |
"gender": "F", | |
"position": "Intern" | |
}, | |
{ | |
"name": "Ryan Nelson", | |
"age": 31, | |
"gender": "M", | |
"position": "Marketing Manager" | |
}, | |
{ | |
"name": "Kathy Williams", | |
"age": 49, | |
"gender": "F", | |
"position": "Senior Marketing Manager" | |
}, | |
{ | |
"name": "Jacqueline Hill", | |
"age": 28, | |
"gender": "F", | |
"position": "Junior Marketing Manager" | |
}, | |
{ | |
"name": "Donald Morris", | |
"age": 39, | |
"gender": "M", | |
"position": "SEO Specialist" | |
}, | |
{ | |
"name": "Evelyn Henderson", | |
"age": 24, | |
"gender": "F", | |
"position": "Intern" | |
}, | |
{ | |
"name": "Earl Moore", | |
"age": 21, | |
"gender": "M", | |
"position": "Junior SEO Specialist" | |
}, | |
{ | |
"name": "Phillip Sanchez", | |
"age": 35, | |
"gender": "M", | |
"position": "SEM Specialist" | |
} | |
] | |
} | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_version" : 1, | |
"result" : "created", | |
"_shards" : { | |
"total" : 2, | |
"successful" : 2, | |
"failed" : 0 | |
}, | |
"_seq_no" : 1, | |
"_primary_term" : 1 | |
} | |
// Querying nested fields, fetch the matched docs. | |
GET /departments/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"nested": { | |
"path": "employees", | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"match": { | |
"employees.position": "intern" | |
} | |
}, | |
{ | |
"term": { | |
"employees.gender.keyword": "F" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
{ | |
"took" : 5, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 2.3905568, | |
"hits" : [ | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "1", | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "Development", | |
"employees" : [ | |
{ | |
"name" : "Eric Green", | |
"age" : 39, | |
"gender" : "M", | |
"position" : "Big Data Specialist" | |
}, | |
{ | |
"name" : "James Taylor", | |
"age" : 27, | |
"gender" : "M", | |
"position" : "Software Developer" | |
}, | |
{ | |
"name" : "Gary Jenkins", | |
"age" : 21, | |
"gender" : "M", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Julie Powell", | |
"age" : 26, | |
"gender" : "F", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Benjamin Smith", | |
"age" : 46, | |
"gender" : "M", | |
"position" : "Senior Software Engineer" | |
} | |
] | |
} | |
}, | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "HR & Marketing", | |
"employees" : [ | |
{ | |
"name" : "Patricia Lewis", | |
"age" : 42, | |
"gender" : "F", | |
"position" : "Senior Marketing Manager" | |
}, | |
{ | |
"name" : "Maria Anderson", | |
"age" : 56, | |
"gender" : "F", | |
"position" : "Head of HR" | |
}, | |
{ | |
"name" : "Margaret Harris", | |
"age" : 19, | |
"gender" : "F", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Ryan Nelson", | |
"age" : 31, | |
"gender" : "M", | |
"position" : "Marketing Manager" | |
}, | |
{ | |
"name" : "Kathy Williams", | |
"age" : 49, | |
"gender" : "F", | |
"position" : "Senior Marketing Manager" | |
}, | |
{ | |
"name" : "Jacqueline Hill", | |
"age" : 28, | |
"gender" : "F", | |
"position" : "Junior Marketing Manager" | |
}, | |
{ | |
"name" : "Donald Morris", | |
"age" : 39, | |
"gender" : "M", | |
"position" : "SEO Specialist" | |
}, | |
{ | |
"name" : "Evelyn Henderson", | |
"age" : 24, | |
"gender" : "F", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Earl Moore", | |
"age" : 21, | |
"gender" : "M", | |
"position" : "Junior SEO Specialist" | |
}, | |
{ | |
"name" : "Phillip Sanchez", | |
"age" : 35, | |
"gender" : "M", | |
"position" : "SEM Specialist" | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
// Nested - inner hits | |
GET /departments/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"nested": { | |
"path": "employees", | |
"inner_hits": {}, | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"match": { | |
"employees.position": "intern" | |
} | |
}, | |
{ | |
"term": { | |
"employees.gender.keyword": "F" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
{ | |
"took" : 17, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 2.3905568, | |
"hits" : [ | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "1", | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "Development", | |
"employees" : [ | |
{ | |
"name" : "Eric Green", | |
"age" : 39, | |
"gender" : "M", | |
"position" : "Big Data Specialist" | |
}, | |
{ | |
"name" : "James Taylor", | |
"age" : 27, | |
"gender" : "M", | |
"position" : "Software Developer" | |
}, | |
{ | |
"name" : "Gary Jenkins", | |
"age" : 21, | |
"gender" : "M", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Julie Powell", | |
"age" : 26, | |
"gender" : "F", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Benjamin Smith", | |
"age" : 46, | |
"gender" : "M", | |
"position" : "Senior Software Engineer" | |
} | |
] | |
}, | |
"inner_hits" : { | |
"employees" : { | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 2.3905568, | |
"hits" : [ | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "1", | |
"_nested" : { | |
"field" : "employees", | |
"offset" : 3 | |
}, | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "Julie Powell", | |
"age" : 26, | |
"gender" : "F", | |
"position" : "Intern" | |
} | |
} | |
] | |
} | |
} | |
} | |
}, | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "HR & Marketing", | |
"employees" : [ | |
{ | |
"name" : "Patricia Lewis", | |
"age" : 42, | |
"gender" : "F", | |
"position" : "Senior Marketing Manager" | |
}, | |
{ | |
"name" : "Maria Anderson", | |
"age" : 56, | |
"gender" : "F", | |
"position" : "Head of HR" | |
}, | |
{ | |
"name" : "Margaret Harris", | |
"age" : 19, | |
"gender" : "F", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Ryan Nelson", | |
"age" : 31, | |
"gender" : "M", | |
"position" : "Marketing Manager" | |
}, | |
{ | |
"name" : "Kathy Williams", | |
"age" : 49, | |
"gender" : "F", | |
"position" : "Senior Marketing Manager" | |
}, | |
{ | |
"name" : "Jacqueline Hill", | |
"age" : 28, | |
"gender" : "F", | |
"position" : "Junior Marketing Manager" | |
}, | |
{ | |
"name" : "Donald Morris", | |
"age" : 39, | |
"gender" : "M", | |
"position" : "SEO Specialist" | |
}, | |
{ | |
"name" : "Evelyn Henderson", | |
"age" : 24, | |
"gender" : "F", | |
"position" : "Intern" | |
}, | |
{ | |
"name" : "Earl Moore", | |
"age" : 21, | |
"gender" : "M", | |
"position" : "Junior SEO Specialist" | |
}, | |
{ | |
"name" : "Phillip Sanchez", | |
"age" : 35, | |
"gender" : "M", | |
"position" : "SEM Specialist" | |
} | |
] | |
}, | |
"inner_hits" : { | |
"employees" : { | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 2.3905568, | |
"hits" : [ | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_nested" : { | |
"field" : "employees", | |
"offset" : 2 | |
}, | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "Margaret Harris", | |
"age" : 19, | |
"gender" : "F", | |
"position" : "Intern" | |
} | |
}, | |
{ | |
"_index" : "departments", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_nested" : { | |
"field" : "employees", | |
"offset" : 7 | |
}, | |
"_score" : 2.3905568, | |
"_source" : { | |
"name" : "Evelyn Henderson", | |
"age" : 24, | |
"gender" : "F", | |
"position" : "Intern" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment