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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014686399/article/details/81136990

为什么使用join datatype(父子文档)

  • 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": {
        "name": {
          "type": "text"
        },
        "size":{"type" : "text"},
        "color":{"type" : "text"},
        "info": {
          "type": "join",
          "relations": {
            "base": "next"
          }
        }
      }
    }
  }
}'

返回{"acknowledged":true,"shards_acknowledged":true,"index":"shop"}说明成功

3. 索引数据
  1. 索引父文档(nike Address)
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是必须有的

剩下的文档自己解决吧
这里写图片描述

  1. has_child 查询含有XL尺寸衣服的品牌
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"
        }
      }
    }
  }
}'

这里写图片描述


先写到这里了,有问题进QQ群630300475

猜你喜欢

转载自blog.csdn.net/u014686399/article/details/81136990
今日推荐