Elasticsearch 父子关系维护和检索案例分享,展示has_child和has_parent的基本用法。
本案例针对elasticsearch 5.x进行父子关系进行介绍,6.x版本通过join type来实现父子关系检索,参考文档后面提供的demo。
本文涉及技术点:
1.删除和创建公司/雇员父子关系的索引表
2.bulk批量导入json格式数据
3.ormapping方式bulk批量导入数据
4.采用@ESId指定文档_id
5.采用@ESParentId制定子文档的parent信息
6.基本的has_child和has_parent公司/雇员父子关系检索
1.准备工作
2.定义dsl文档
建立dsl配置文件-esmapper/indexparentchild.xml
"settings": {
"number_of_shards": 6,
"index.refresh_interval": "5s"
},
"mappings": {
"company": { ##公司
"properties": {
"name": {
"type": "text",
"fields": { ##dsl注释 定义精确查找的内部keyword字段
"keyword": {
"type": "keyword"
}
}
},
"city": {
"type": "text",
"fields": { ##dsl注释 定义精确查找的内部keyword字段
"keyword": {
"type": "keyword"
}
}
},
"country": {
"type": "text",
"fields": { ##dsl注释 定义精确查找的内部keyword字段
"keyword": {
"type": "keyword"
}
}
},
"companyId": {
"type": "keyword"
}
}
},
"employee":
{ ##雇员
"_parent": {##定义雇员和公司父子关联关系
"type": "company"
},
"_routing": {
"required": false
},
"properties": {
"name": {
"type": "text",
"fields": { ##dsl注释 定义精确查找的内部keyword字段
"keyword": {
"type": "keyword"
}
}
},
"birthday": {
"type": "date",
"format":"yyyy-MM-dd||epoch_millis"
},
"hobby": {