join类型,父子节点必须在同一个分片
#### join类型,父子节点必须在同一个分片
DELETE menus_001
#举例菜单
PUT menus_001
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 3
},
"mappings": {
"properties": {
"menuId":{
"type": "integer"
},
"menuName":{
"type": "keyword"
},
"sort":{
"type": "integer"
},
"join":{
"type": "join",
"relations":{
"parent":"child"
}
}
}
}
}
GET menus_001/_mapping
#放入父节点,设置分片路由为1,默认为_doc/id
PUT menus_001/_doc/1?routing=1
{
"menuId":1,
"menuName":"用户设置",
"sort":0,
"join":{
"name":"parent"
}
}
#放入子节点,设置分片路由为1,默认为_doc/id,parent设置为父类的_doc/id
PUT menus_001/_doc/11?routing=1
{
"menuId":11,
"menuName":"修改密码",
"sort":1,
"join":{
"name":"child",
"parent":1
}
}
#放入子节点,设置分片路由为1,默认为_doc/id,parent设置为父类的_doc/id
PUT menus_001/_doc/12?routing=1
{
"menuId":12,
"menuName":"绑定邮箱",
"sort":2,
"join":{
"name":"child",
"parent":1
}
}
#放入子节点,设置分片路由为1,默认为_doc/id,parent设置为父类的_doc/id
PUT menus_001/_doc/13?routing=1
{
"menuId":13,
"menuName":"修改绑定手机",
"sort":3,
"join":{
"name":"child",
"parent":1
}
}
#放入父节点,设置分片路由为1,默认为_doc/id
PUT menus_001/_doc/2?routing=1
{
"menuId":2,
"menuName":"商品设置",
"sort":1,
"join":{
"name":"parent"
}
}
#放入子节点,设置分片路由为1,默认为_doc/id,parent设置为父类的_doc/id
PUT menus_001/_doc/21?routing=1
{
"menuId":21,
"menuName":"批量修改sku",
"sort":1,
"join":{
"name":"child",
"parent":2
}
}
#放入父节点,设置分片路由为1,默认为_doc/id
PUT menus_001/_doc/3?routing=1
{
"menuId":3,
"menuName":"订单管理",
"sort":3,
"join":{
"name":"parent"
}
}
GET menus_001/_search
#查询id为1的子菜单
GET menus_001/_search
{
"query": {
"parent_id":{
"type":"child",
"id":1
}
}
}
#查询有子菜单的父节点
GET menus_001/_search
{
"query": {
"has_child":{
"type":"child",
"query": {
"match_all": {}
}
}
}
}
#查询有父节的菜单
GET menus_001/_search
{
"query": {
"has_parent": {
"parent_type": "parent",
"query": {
"match_all": {}
}
}
}
}
nested,嵌套(注意区别于数组类型)
#### nested,嵌套(注意区别于数组类型)
DELETE person_001
PUT person_001
{
"mappings": {
"properties": {
"name":{
"type": "keyword"
},
"area":{
"type": "nested",
"properties": {
"provice":{
"type":"keyword"
},
"city":{
"type":"keyword"
},
"country":{
"properties": {
"addr":{
"type":"text"
}
}
}
}
},
"child":{
"type": "keyword"
},
"child2":{
"type": "keyword"
}
}
}
}
#查看映射
GET person_001/_mapping
PUT person_001/_doc/1
{
"name": "David",
"area": [
{
"city":"hang zhou",
"provice":"zhe jiang",
"country":{
"addr":"xi hu qu"
}
},
{
"city":"guang zhou",
"provice":"guang dong",
"country":{
"addr":"yue xiu qu"
}
}
],
"child":["LiLi","Luck"],
"child2":[["LiLi","Luck"],["Sunny","Sasyd"]]
}
GET person_001/_doc/1
#area为nested完善了数组的弊端,查询语法有区别,查询浙江广州不会有数据
GET person_001/_search
{
"query": {
"nested": {
"path": "area",
"query": {
"bool": {
"must": [
{
"term": {
"area.provice": {
"value": "zhe jiang"
}
}
},
{
"term": {
"area.city": {
"value": "guang zhou"
}
}
}
]
}
}
}
}
}
#查询语法有区别,查询浙江杭州会有数据
GET person_001/_search
{
"query": {
"nested": {
"path": "area",
"query": {
"bool": {
"must": [
{
"term": {
"area.provice": {
"value": "zhe jiang"
}
}
},
{
"term": {
"area.city": {
"value": "hang zhou"
}
}
}
]
}
}
}
}
}
别名类型和嵌套别名
### 别名类型,字段别名
DELETE product_001
# gName不太友好不便于理解,可以加个别名goodsName更形象,不能更新不存储
PUT product_001
{
"mappings": {
"properties": {
"gName":{
"type": "keyword"
},
"goodsName":{
"type": "alias",
"path":"gName"
}
}
}
}
#别名无法正确插入
PUT product_001/_doc/1
{
"goodsName":"my goods"
}
#原有字段可以正确插入
PUT product_001/_doc/1
{
"gName":"my goods"
}
# 别名可以用于查询
GET product_001/_search
{
"query": {
"term": {
"goodsName": {
"value": "my goods"
}
}
}
}
DELETE product_001
### 嵌套别名
PUT product_001
{
"mappings": {
"properties": {
"gName":{
"properties": {
"gg":{
"type":"keyword"
}
}
},
"goodsName":{
"type": "alias",
"path":"gName.gg"
}
}
}
}
#别名无法正确插入
PUT product_001/_doc/1
{
"goodsName":"my goods"
}
#原有字段可以正确插入
PUT product_001/_doc/1
{
"gName":{
"gg":"my goods"
}
}
# 别名可以用于查询
GET product_001/_search
{
"query": {
"term": {
"goodsName": {
"value": "my goods"
}
}
}
}
flattened 平铺类型,标记flattened字段如果为object只会当做一个值,对象中的字段不创建索引,不做于查询使用字段
#### flattened 平铺类型,标记flattened字段如果为object只会当做一个值
#### 对象中的字段不创建索引,不做于查询使用字段
DELETE order_001
PUT order_001
{
"mappings": {
"properties": {
"orderSn":{
"type": "keyword"
},
"goods":{
"type": "flattened"
}
}
}
}
GET order_001/_mapping
### goods相当于一个值,存储了值
PUT order_001/_doc/1
{
"orderSn":"202012353141-4324",
"goods":{
"sku":{
"color":"red",
"size":"xxl"
}
}
}
GET order_001/_search
constant keyword 固定值类型,无论是否填写都有固定值,不可修改,安全限制、标识可用
#### constant keyword 固定值类型,无论是否填写都有固定值,不可修改
#### 安全限制、标识可用
DELETE order_001
PUT order_001
{
"mappings": {
"properties": {
"orderSn":{
"type": "keyword"
},
"goods":{
"type": "text"
},
"auth":{
"type": "constant_keyword",
"value":"David auth"
}
}
}
}
# 插入会报错,无法填充auth
PUT order_001/_doc/1
{
"orderSn":"202012353141-4324",
"goods":" shoes",
"auth":"my auth"
}
# 正常插入
PUT order_001/_doc/1
{
"orderSn":"202012353141-4324",
"goods":" shoes"
}
##### multi-fileds 多字段类型、属性
DELETE product_001
# 基于keyword检索比数值效率更高
PUT product_001
{
"mappings": {
"properties": {
"goodsName":{
"type": "text",
"fields": {
"gn":{
"type":"keyword"
}
}
},
"number":{
"type": "integer",
"fields": {
"ns":{
"type":"keyword"
}
}
}
}
}
}
PUT product_001/_doc/1
{
"goodsName":"seven days shoes",
"number":9999
}
GET product_001/_mapping
#倒排索引查询效率更高
GET product_001/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"number.ns": {
"value": "9999"
}
}
}
]
}
}
}
# 范围检索
GET product_001/_search
{
"query": {
"range": {
"number": {
"gte": 9888,
"lte": 10000
}
}
}
}
练习
##### 1、设计行业nested类型索引
DELETE order_001
# 订单为例
PUT order_001
{
"mappings": {
"properties": {
"orderSn":{
"type": "keyword"
},
"goods":{
"type": "nested",
"properties": {
"name":{
"type":"keyword"
},
"color":{
"type":"keyword"
},
"number":{
"type":"integer"
}
}
}
}
}
}
GET order_001/_mapping
PUT order_001/_doc/1
{
"orderSn": "20200812353344-2341",
"goods": [
{
"name": "seven days shoes",
"color": "red",
"number": 1
},
{
"name": "five days shoes",
"color": "black",
"number": 2
}
]
}
PUT order_001/_doc/2
{
"orderSn": "202008123533123-2311",
"goods": [
{
"name": "one days shoes",
"color": "white",
"number": 1
},
{
"name": "tow days shoes",
"color": "white",
"number": 2
}
]
}
GET order_001/_search
{
"query": {
"nested": {
"path": "goods",
"query": {
"bool": {
"must": [
{
"term": {
"goods.name": {
"value": "seven days shoes"
}
}
},
{
"term": {
"goods.color": {
"value": "red"
}
}
}
]
}
}
}
}
}
##### 2、设计行业多字段类型索引
DELETE shop_001
#店铺为例
PUT shop_001
{
"mappings": {
"properties": {
"shopName":{
"type": "text",
"fields": {
"sn":{
"type":"keyword"
}
}
},
"account":{
"type":"keyword"
},
"amount":{
"type": "integer",
"fields": {
"money":{
"type":"keyword"
}
}
}
}
}
}
GET shop_001/_mapping
PUT shop_001/_doc/1
{
"shopName":"David has a shop",
"amount":10000,
"account":"David"
}
#倒排索引查询效率更高
GET shop_001/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"amount.money": {
"value": "10000"
}
}
}
]
}
}
}
# 范围检索
GET shop_001/_search
{
"query": {
"range": {
"amount": {
"gte": 9998,
"lte": 10000
}
}
}
}