I have location index with following properties. Documents are related through entityId and parentEntityId, so there's hierarchical data. And also there's entityPath field which contains path of current document all the way to root document.
Can someone guide how to get all children entityIds given an entityId?
PUT location
{
"mappings": {
"properties": {
"entityId": {"type": "text"},
"entityType": {"type": "text"},
"entityTypeId": {"type": "text"},
"resellerMSISDN": {"type": "text"},
"parentEntityId": {"type": "text"},
"entityName": {"type": "text"},
"entityPath": {"type": "text"},
"snic_category": {"type": "text"},
"address": {"type": "object"},
"location": {"type": "geo_point"},
"createdAt": {"type": "date"},
"updatedAt": {"type": "date"}
}
}
}
Below are sample documents:
{
"_index" : "location",
"_type" : "_doc",
"_id" : "UyJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "FranchisesShop1",
"entityType" : "Franchises Shop",
"entityTypeId" : "franchisesshop",
"resellerMSISDN" : "254714410000",
"parentEntityId" : "OPERATOR",
"entityName" : "FranchisesShop 1",
"entityPath" : "OPERATOR/FranchisesShop1",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "VCJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "ShopStaff1-1",
"entityType" : "Franchises Shop Staff",
"entityTypeId" : "shopstaff",
"resellerMSISDN" : "254714410100",
"parentEntityId" : "FranchisesShop1",
"entityName" : "ShopStaff 1",
"entityPath" : "OPERATOR/FranchisesShop1/ShopStaff1-1",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "VSJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "ThirdPartyAgent1",
"entityType" : "Third Party Agents",
"entityTypeId" : "3pl_agents",
"resellerMSISDN" : "254714510000",
"parentEntityId" : "OPERATOR",
"entityName" : "ThirdPartyAgent 1",
"entityPath" : "OPERATOR/ThirdPartyAgent1",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "ViJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "ThirdPartyAgentStaff1-1",
"entityType" : "Third Party Agents Staff",
"entityTypeId" : "3pl_agents_staff",
"resellerMSISDN" : "254714510100",
"parentEntityId" : "ThirdPartyAgent1",
"entityName" : "ThirdPartyAgentStaff 1",
"entityPath" : "OPERATOR/ThirdPartyAgent1/ThirdPartyAgentStaff1-1",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "VyJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "Director2",
"entityType" : "Director",
"entityTypeId" : "director",
"resellerMSISDN" : "254714020000",
"parentEntityId" : "OPERATOR",
"entityName" : "Director 2",
"entityPath" : "OPERATOR/Director2",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "WCJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "HoD2-1",
"entityType" : "Regional HoD",
"entityTypeId" : "hod",
"resellerMSISDN" : "254714021000",
"parentEntityId" : "Director2",
"entityName" : "HoD 1",
"entityPath" : "OPERATOR/Director2/HoD2-1",
"address" : {
"street" : "",
"zip" : null,
"city" : "",
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : "[email protected]"
},
"snic_category" : "Dealer"
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "WSJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "RSM2-1-1",
"entityType" : "Regional Sales Manager",
"entityTypeId" : "rsm",
"resellerMSISDN" : "254714021100",
"parentEntityId" : "HoD2-1",
"entityName" : "RSM 1",
"entityPath" : "OPERATOR/Director2/HoD2-1/RSM2-1-1",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "WiJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "ASM2-1-1-1",
"entityType" : "Area Sales Manager",
"entityTypeId" : "asm",
"resellerMSISDN" : "254714021110",
"parentEntityId" : "RSM2-1-1",
"entityName" : "ASM 1",
"entityPath" : "OPERATOR/Director2/HoD2-1/RSM2-1-1/ASM2-1-1-1",
"address" : {
"street" : "2433",
"zip" : null,
"city" : "lhr",
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : "[email protected]"
},
"snic_category" : "Dealer"
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "WyJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "TDR2-1-1-1-1",
"entityType" : "Trade Dealer Representative",
"entityTypeId" : "tdr",
"resellerMSISDN" : "254714021111",
"parentEntityId" : "ASM2-1-1-1",
"entityName" : "TDR 1",
"entityPath" : "OPERATOR/Director2/HoD2-1/RSM2-1-1/ASM2-1-1-1/TDR2-1-1-1-1",
"address" : {
"street" : "111 wall street",
"zip" : null,
"city" : "lahore",
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : "[email protected]"
},
"snic_category" : "Agent"
}
},
{
"_index" : "location",
"_type" : "_doc",
"_id" : "XCJgTX4BCWiVeWu9nnuM",
"_score" : 1.0,
"_source" : {
"entityId" : "FranchisesShop2",
"entityType" : "Franchises Shop",
"entityTypeId" : "franchisesshop",
"resellerMSISDN" : "254714420000",
"parentEntityId" : "OPERATOR",
"entityName" : "FranchisesShop 2",
"entityPath" : "OPERATOR/FranchisesShop2",
"address" : {
"street" : null,
"zip" : null,
"city" : null,
"country" : "Kenya",
"phone" : null,
"fax" : null,
"homepage" : null,
"email" : null
}
}
}
CodePudding user response:
There's no way to do that in Elasticsearch with your current data structure.
There are two options:
- Use multi-level nested data structures and model them at indexing time
- Use recursive calls in your application
CodePudding user response:
create a custom analyzer i.e slash-analyzer use to analyse entityPath field.
Modified Index Creation
PUT location
{
"settings": {
"analysis": {
"analyzer": {
"slash_analyzer": {
"type": "pattern",
"pattern": "/"
}
}
}
},
"mappings": {
"properties": {
"entityId": {"type": "text"},
"entityType": {"type": "text"},
"entityTypeId": {"type": "text"},
"resellerMSISDN": {"type": "text"},
"parentEntityId": {"type": "text"},
"entityName": {"type": "text"},
"entityPath": {
"type": "text",
"analyzer": "slash_analyzer"
},
"snic_category": {"type": "text"},
"address": {"type": "object"},
"location": {"type": "geo_point"},
"createdAt": {"type": "date"},
"updatedAt": {"type": "date"}
}
}
}
Below is the search query to get all the children for FranchisesShop1 including itself.
GET location/_search
{
"query": {
"match": {
"entityPath": {
"query": "FranchisesShop1"
}
}
}
}
