Elasticsearch索引的父子关系(index parent-child)

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: #Elasticsearch允许给文档建立父子关系,这篇博客介绍文档的父子关系是如何映射的(Parent-Child Mapping)、如何索引父子文档(Indexing Parents and Children)、如何通过子文档查询父文档 (Finding Parents by Their Children)、如何通过父文档查询子文档(Finding Children by Their Parents)。

#Elasticsearch允许给文档建立父子关系,这篇博客介绍文档的父子关系是如何映射的(Parent-Child Mapping)、如何索引父子文档(Indexing Parents and Children)、如何通过子文档查询父文档
(Finding Parents by Their Children)、如何通过父文档查询子文档(Finding Children by Their Parents)。

一、父子文档的mapping

建立文档的父子关系最重要的一步是在创建索引的时候在mapping中声明哪个是父文档哪个是子文档。官网文档给了一个公司在很多城市的分支(branch)和每个分支有相关员工(employee)的例子,如果想把员工和他们工作的分公司关联起来,我们需要告诉Elasticsearch文档之间的父子关系,这里employee是child type,branch是parent type,在mapping中声明:

curl -XPUT  "http://192.168.0.224:9200/company" -d '
{
  "mappings": {
    "branch": {},
    "employee": {
      "_parent": {
        "type": "branch"
      }
    }
  }
}
'

这样我们创建了一个索引,并制定了2个type和它们之间的父子关系.

二、父子文档的索引

2.1索引父文档

索引父文档和索引一般的文档没有任何区别。
准备几条公司的数据,存在company.json文件中:

{ "index": { "_id": "london" }}
{ "name": "London Westminster", "city": "London", "country": "UK" }
{ "index": { "_id": "liverpool" }}
{ "name": "Liverpool Central", "city": "Liverpool", "country": "UK" }
{ "index": { "_id": "paris" }}
{ "name": "Champs Élysées", "city": "Paris", "country": "France" }

使用Bulk端点批量导入:


curl -XPOST "http://192.168.0.224:9200/company/branch/_bulk?pretty" --data-binary @company.json

2.2索引子文档

索引子文档需要制定子文档的父ID,给子文档的每条文档设置parent属性的value为父文档id即可:

curl -XPUT "http://192.168.0.224:9200/company/employee/1?parent=london&pretty" -d '
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}
'

上面的操作我们新增了一条数据,/company/employee/1的父文档为/company/branch/london。
同样可以批量索引子文档,把employee数据存入json文件中:

{ "index": { "_id": 2, "parent": "london" }}
{ "name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving" }

{ "index": { "_id": 3, "parent": "liverpool" }}
{ "name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking" }

{ "index": { "_id": 4, "parent": "paris" }}
{ "name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses" }

执行bulk命令:

curl -XPOST "http://192.168.0.224:9200/company/employee/_bulk?pretty" --data-binary @employee.json

三、通过子文档查询父文档

搜索含有1980年以后出生的employee的branch:

curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '
{           
  "query": {      
    "has_child": {       
      "type": "employee",
      "query": {  
        "range": {
          "dob": {             
            "gte": "1980-01-01"
          }
        }
      }
    }
  }
}
'
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "company",
      "_type" : "branch",
      "_id" : "paris",
      "_score" : 1.0,
      "_source" : {
        "name" : "Champs Élysées",
        "city" : "Paris",
        "country" : "France"
      }
    }, {
      "_index" : "company",
      "_type" : "branch",
      "_id" : "london",
      "_score" : 1.0,
      "_source" : {
        "name" : "London Westminster",
        "city" : "London",
        "country" : "UK"
      }
    } ]
  }
}

查询name中含有“Alice Smith”的branch:

curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '
{
  "query": {
    "has_child": {
      "type":       "employee",
      "score_mode": "max",
      "query": {
        "match": {
          "name": "Alice Smith"
        }
      }
    }
  }
}'



{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.2422675,
    "hits" : [ {
      "_index" : "company",
      "_type" : "branch",
      "_id" : "london",
      "_score" : 1.2422675,
      "_source" : {
        "name" : "London Westminster",
        "city" : "London",
        "country" : "UK"
      }
    }, {
      "_index" : "company",
      "_type" : "branch",
      "_id" : "liverpool",
      "_score" : 0.30617762,
      "_source" : {
        "name" : "Liverpool Central",
        "city" : "Liverpool",
        "country" : "UK"
      }
    } ]
  }
}

搜索最少含有2个employee的branch:

curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '{
"query": {
    "has_child": {
      "type":         "employee",
      "min_children": 2,
      "query": {
        "match_all": {}
      }
    }
  }
}
'
{
  "took" : 17,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "company",
      "_type" : "branch",
      "_id" : "london",
      "_score" : 1.0,
      "_source" : {
        "name" : "London Westminster",
        "city" : "London",
        "country" : "UK"
      }
    } ]
  }
}

四、通过父文档查询子文档

搜搜工作在UK的employee:

curl -XGET "http://192.168.0.224:9200/company/employee/_search?pretty" -d '{           
  "query": {       
    "has_parent": {    
      "type": "branch", 
      "query": {  
        "match": {       
          "country": "UK"
        }
      }
    }
  }
}'

{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "company",
      "_type" : "employee",
      "_id" : "3",
      "_score" : 1.0,
      "_parent" : "liverpool",
      "_routing" : "liverpool",
      "_source" : {
        "name" : "Barry Smith",
        "dob" : "1979-04-01",
        "hobby" : "hiking"
      }
    }, {
      "_index" : "company",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 1.0,
      "_parent" : "london",
      "_routing" : "london",
      "_source" : {
        "name" : "Alice Smith",
        "dob" : "1970-10-24",
        "hobby" : "hiking"
      }
    }, {
      "_index" : "company",
      "_type" : "employee",
      "_id" : "2",
      "_score" : 1.0,
      "_parent" : "london",
      "_routing" : "london",
      "_source" : {
        "name" : "Mark Thomas",
        "dob" : "1982-05-16",
        "hobby" : "diving"
      }
    } ]
  }
}
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
257 5
|
存储 分布式计算 大数据
大数据-169 Elasticsearch 索引使用 与 架构概念 增删改查
大数据-169 Elasticsearch 索引使用 与 架构概念 增删改查
205 3
|
存储 API 数据库
检索服务elasticsearch索引(Index)
【8月更文挑战第23天】
638 6
|
存储 缓存 监控
优化Elasticsearch 索引设计
优化Elasticsearch 索引设计
245 5
|
存储 JSON 关系型数据库
Elasticsearch 索引
【11月更文挑战第3天】
279 4
|
测试技术 API 开发工具
ElasticSearch7.6.x 模板及滚动索引创建及注意事项
ElasticSearch7.6.x 模板及滚动索引创建及注意事项
194 8
|
JSON 自然语言处理 数据库
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
概念、ik分词器、倒排索引、索引和文档的增删改查、RestClient对索引和文档的增删改查
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
|
存储 搜索推荐 数据建模
Elasticsearch 的数据建模与索引设计
【9月更文第3天】Elasticsearch 是一个基于 Lucene 的搜索引擎,广泛应用于全文检索、数据分析等领域。为了确保 Elasticsearch 的高效运行,合理的数据建模和索引设计至关重要。本文将探讨如何为不同的应用场景设计高效的索引结构,并分享一些数据建模的最佳实践。
537 2
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
自然语言处理 Java 索引
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
199 0