Elasticsearch数据建模实战之祖孙三层数据关系建模以及搜索实战
父子关系,祖孙三层关系的数据建模,搜索
PUT /company
{
"mappings": {
"country": {},
"rd_center": {
"_parent": {
"type": "country"
}
},
"employee": {
"_parent": {
"type": "rd_center"
}
}
}
}country -> rd_center -> employee,祖孙三层数据模型
POST /company/country/_bulk
{ "index": { "_id": "1" }}
{ "name": "中国" }
{ "index": { "_id": "2" }}
{ "name": "美国" }POST /company/rd_center/_bulk
{ "index": { "_id": "1", "parent": "1" }}
{ "name": "北京研发总部" }
{ "index": { "_id": "2", "parent": "1" }}
{ "name": "上海研发中心" }
{ "index": { "_id": "3", "parent": "2" }}
{ "name": "硅谷人工智能实验室" }PUT /company/employee/1?parent=1&routing=1
{
"name": "张三",
"dob": "1970-10-24",
"hobby": "爬山"
}
routing参数的讲解,必须跟grandparent相同,否则有问题
country,用的是自己的id去路由; rd_center,parent,用的是country的id去路由; employee,如果也是仅仅指定一个parent,那么用的是rd_center的id去路由,这就导致祖孙三层数据不会在一个shard上
孙子辈儿,要手动指定routing,指定为爷爷辈儿的数据的id
搜索有爬山爱好的员工所在的国家
GET /company/country/_search
{
"query": {
"has_child": {
"type": "rd_center",
"query": {
"has_child": {
"type": "employee",
"query": {
"match": {
"hobby": "爬山"
}
}
}
}
}
}
}
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "country",
"_id": "1",
"_score": 1,
"_source": {
"name": "中国"
}
}
]
}
}
相关文章
- WIN10系统如何隐藏底部搜索框
- Java实现 LeetCode 212 单词搜索 II(二)
- Oracle 查询搜索字符串在哪些存储过程中包含
- Atitit 非结构化数据管理法 目录 1. 什么是非结构化数据?1 2. 对非结构化数据也即对全文数据的搜索主要有两种方法:2 2.1. 顺序扫描法(Serial Scanning):2 2
- 700. 二叉搜索树中的搜索-dfs法
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
- Elasticsearch - 搜索类型与搜索位置
- findstr 搜索多关键字的方法
- python里使用正则表达式的search()函数实现指定位置搜索
- Everything 搜索工具的原理与实现
- BFS算法的优化 双向宽度优先搜索
- 搜索引擎——用户搜索意图的理解及其难点解析,本质是利用机器学习用户的意图分类
- 【Leetcode刷题Python】33. 搜索旋转排序数组
- LeetCode 700. 二叉搜索树中的搜索