在 ElasticSearch 中获取文档的所有子项

Get All Children of a document in ElasticSearch

我有 location 具有以下属性的索引。文档通过 entityIdparentEntityId 关联,因此存在分层数据。还有 entityPath 字段,它包含当前文档一直到根文档的路径。

有人可以指导如何在给定实体 ID 的情况下获取所有子实体 ID 吗?

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"}
      }
    }
}

以下是示例文档:

{
    "_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" : "shahida.lashari@seamless.se"
      },
      "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" : "muhammadumar.butt@seamless.se"
      },
      "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" : "zaeem.jawad@seamless.se"
      },
      "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
      }
    }
  }

在 Elasticsearch 中,您当前的数据结构无法做到这一点。

有两种选择:

  • 使用多级嵌套数据结构并在索引时对其建模
  • 在您的应用程序中使用递归调用

创建自定义 analyzerslash-analyzer 用于分析 entityPath 字段。

修改索引创建

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"}
      }
    }
}

以下是获取 FranchisesShop1 的所有子项(包括其自身)的搜索查询。

GET location/_search
{
  "query": {
    "match": {
      "entityPath": {
        "query":  "FranchisesShop1"
      }
    }
  }
}