Skip to content

Instantly share code, notes, and snippets.

@VarunVats9
Last active February 29, 2020 18:40
Show Gist options
  • Save VarunVats9/7332c165b0eb2725ed751223b26b64b0 to your computer and use it in GitHub Desktop.
Save VarunVats9/7332c165b0eb2725ed751223b26b64b0 to your computer and use it in GitHub Desktop.
Elasticsearch - parent-child inner hits, multi level relations
// Inner hits, has_child query
GET /department/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"query": {
"has_child": {
"type": "employee",
"inner_hits": {},
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 10
}
}
}
],
"should": [
{
"term": {
"gender.keyword": "M"
}
}
]
}
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "department",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Development",
"join_field" : "department"
},
"inner_hits" : {
"employee" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.1335313,
"hits" : [
{
"_index" : "department",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.1335313,
"_routing" : "1",
"_source" : {
"name" : "Bo Andersen",
"age" : 28,
"gender" : "M",
"join_field" : {
"name" : "employee",
"parent" : 1
}
}
},
{
"_index" : "department",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.1335313,
"_routing" : "1",
"_source" : {
"name" : "James Evans",
"age" : 32,
"gender" : "M",
"join_field" : {
"name" : "employee",
"parent" : 1
}
}
}
]
}
}
}
},
{
"_index" : "department",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Marketing",
"join_field" : "department"
},
"inner_hits" : {
"employee" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.1335313,
"hits" : [
{
"_index" : "department",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.1335313,
"_routing" : "2",
"_source" : {
"name" : "John Doe",
"age" : 44,
"gender" : "M",
"join_field" : {
"name" : "employee",
"parent" : 2
}
}
}
]
}
}
}
}
]
}
}
// Inner hits, has_parent
GET /department/_search
{
"query": {
"has_parent": {
"inner_hits": {},
"parent_type": "department",
"query": {
"term": {
"name.keyword": "Development"
}
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "department",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"name" : "Bo Andersen",
"age" : 28,
"gender" : "M",
"join_field" : {
"name" : "employee",
"parent" : 1
}
},
"inner_hits" : {
"department" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862942,
"hits" : [
{
"_index" : "department",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.3862942,
"_source" : {
"name" : "Development",
"join_field" : "department"
}
}
]
}
}
}
},
{
"_index" : "department",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"name" : "James Evans",
"age" : 32,
"gender" : "M",
"join_field" : {
"name" : "employee",
"parent" : 1
}
},
"inner_hits" : {
"department" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862942,
"hits" : [
{
"_index" : "department",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.3862942,
"_source" : {
"name" : "Development",
"join_field" : "department"
}
}
]
}
}
}
}
]
}
}
// Multi level mapping
PUT /company
{
"mappings": {
"properties": {
"join_field": {
"type": "join",
"relations": {
"company": ["department", "supplier"],
"department": "employee"
}
}
}
}
}
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "company"
}
PUT /company/_doc/1
{
"name": "My Company Inc.",
"join_field": "company"
}
{
"_index" : "company",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
PUT /company/_doc/2?routing=1
{
"name": "Development",
"join_field": {
"name": "department",
"parent": 1
}
}
{
"_index" : "company",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
PUT /company/_doc/3?routing=1
{
"name": "Bo Andersen",
"join_field": {
"name": "employee",
"parent": 2
}
}
{
"_index" : "company",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
PUT /company/_doc/4
{
"name": "Another Company, Inc.",
"join_field": "company"
}
{
"_index" : "company",
"_type" : "_doc",
"_id" : "4",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
PUT /company/_doc/5?routing=4
{
"name": "Marketing",
"join_field": {
"name": "department",
"parent": 4
}
}
{
"_index" : "company",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
PUT /company/_doc/6?routing=4
{
"name": "John Doe",
"join_field": {
"name": "employee",
"parent": 5
}
}
{
"_index" : "company",
"_type" : "_doc",
"_id" : "6",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 1
}
// Querying multi level relations
GET /company/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"query": {
"has_child": {
"type": "department",
"query": {
"has_child": {
"type": "employee",
"query": {
"term": {
"name.keyword": "John Doe"
}
}
}
}
}
}
}
{
"took" : 609,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "company",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "Another Company, Inc.",
"join_field" : "company"
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment