为什么使用join datatype(父子文档)
- Join类型用于描述一对多的关系。
- ES6.3中一个index只能对应一个type,这样要想存储树形结构的数据,也就只能用join datatype
- ES6.3中已经删除了_parent,所以只能用join来做父子文档了
- join datatype官网地址
- type即将删除
创建过程
1.索引描述
shop索引对应一个cloth类型,cloth类型中的文档(衣服品牌,颜色,大小)
2. 创建索引
curl -X PUT -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/' -d '{
"mappings": {
"cloth": {
"properties": {
"brand": {
"type": "text"
},
"size":{"type" : "text"},
"color":{"type" : "text"},
"info": {
"type": "join",
"relations": {
"base": "next"
}
}
}
}
}
}'
返回{"acknowledged":true,"shards_acknowledged":true,"index":"shop"}
说明成功
3. 索引数据
- 索引父文档(nike)
curl -X PUT -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/cloth/1' -d '{
"brand": "nike",
"info": {
"name": "base"
}
}'
我就写一个吧,Address 自行完成吧,注意_id
- 索引子文档
curl -X PUT -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/cloth/3?routing=1' -d '{
"color":"red",
"size":"XXL",
"info": {
"name": "next",
"parent": "1"
}
}'
注意_id和routing、parent,routing是必须有的
Address的文档用以上方式添加,结果如下
- has_child 查询含有XL尺寸衣服的品牌
has_child 用于查询包含特定子文档的父文档。has_child相对其他查询是比较慢的查询。
该查询查出的结果不能利用sort进行排序。
查询报文中type字段是必须有。
curl -X GET -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/_search?pretty' -d '{
"query": {
"has_child": {
"type": "next",
"query": {
"common": {
"size": {
"query": "xl"
}
}
}
}
}
}'
4. has_parents 查看 nike牌子有哪些衣服
相对其他查询来说,是一个比较慢的查询,如果对性能有要求请使用其他查询代替
curl -X GET -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/_search?pretty' -d '{
"query": {
"has_parent": {
"parent_type": "base",
"query": {
"term": {
"brand": "nike"
}
}
}
}
}'
5、Parent Id 查询用于查询某个特定父文档下的子文档
type和id字段必须有
{
"query": {
"parent_id": {
"type": "my_child",
"id": "1"
}
}
}
其他
- join 类型可以是一种父文档对应多种子文档。
"info": {
"type": "join",
"relations": {
"base": ["next1","next2"]
}
}
- 子文档也可以作为父文档
"info": {
"type": "join",
"relations": {
"base": ["next1","next2"],
"next1":"other"
}
}
使用限制
- 一个索引中只能有一个Join 类型
- 父文档和子文档必须在一个分片上
- 子文档只能有一个父文档
logstash 同步mysql的记录到ES的join type
https://blog.csdn.net/u014686399/article/details/84798525
先写到这里了,有问题进QQ群630300475