Elasticsearch6.3.1 创建父子文档(join datatype)

为什么使用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. 索引数据
  1. 索引父文档(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
这里写图片描述

  1. 索引子文档
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的文档用以上方式添加,结果如下
这里写图片描述

  1. 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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值