elasticsearch 第三节 mappings和常用字段类型

mappings 


#创建索引字段映射,_source默认为true
PUT shop_001
{
  "mappings": {
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引字段映射
GET shop_001/_mapping

#添加数据
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou"
}

#查看数据
GET shop_001/_doc/1


#删除索引
DELETE shop_001

#创建索引字段映射,_source为原始数据,禁用_source,数据量大时可以不返回数据,只返回id
#然后去数据库查询
PUT shop_001
{
  "mappings": {
    "_source": {
      "enabled": false
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引字段映射
GET shop_001/_mapping

#添加数据
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou"
}

#查看数据,_source为原始数据
GET shop_001/_doc/1

#删除索引
DELETE shop_001

#创建索引字段映射,includes 限制原始数据只返回shopName
PUT shop_001
{
  "mappings": {
    "_source": {
      "includes":[
        "shopName"
      ]
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引字段映射
GET shop_001/_mapping

#添加数据
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou"
}

#查看数据,includes数据
GET shop_001/_doc/1


#删除索引
DELETE shop_001

#创建索引字段映射,excludes限制原始数据不返回shopName
PUT shop_001
{
  "mappings": {
    "_source": {
      "excludes":[
        "shopName"
      ]
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引字段映射
GET shop_001/_mapping

#添加数据
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou"
}

#查看数据,excludes数据
GET shop_001/_doc/1


#删除索引
DELETE shop_001

#创建索引字段映射,dynamic默认为true,新增不存在的字段时会自动创建映射
PUT shop_001
{
  "mappings": {
    "dynamic": true, 
    "_source": {
      "enabled": true
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引映射
GET shop_001/_mapping

#添加数据
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou",
  "provice":"zheJiang"
}

#查看数据,数据
GET shop_001/_doc/1


#删除索引
DELETE shop_001

#创建索引字段映射,dynamic为false,新增不存在的字段时不会自动创建映射
PUT shop_001
{
  "mappings": {
    "dynamic": false, 
    "_source": {
      "enabled": true
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引映射
GET shop_001/_mapping

#添加数据
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou",
  "provice":"zheJiang"
}

#查看数据,数据
GET shop_001/_doc/1


#删除索引
DELETE shop_001

#创建索引字段映射,dynamic为struct,新增不存在的字段时会报错,可以防止字段乱加
PUT shop_001
{
  "mappings": {
    "dynamic": "strict", 
    "_source": {
      "enabled": true
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "createTime":{
        "type": "date"
      },
      "city":{
        "type": "text"
      }
    }
  }
}

#查看索引字段映射
GET shop_001/_mapping

#添加多一个省份数据,会报错
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24",
  "city":"hangzhou",
  "provice":"zheJiang"
}

#添加少字段数据,不会报错
PUT shop_001/_doc/1
{
  "shopName":"good good study",
  "createTime":"2020-08-24"
}

#查看数据,数据
GET shop_001/_doc/1

常用字段类型

字符型:

text:文本,按规则分词,至少氛围一个词,主要用于分词检索场景,如商品标题等。

keyword:关键词,不管多长都不分词,全字段匹配,固定文本,不需要全文检索场景,如省、市、县、姓名等。

 

整型:选择正确的类型节约存储空间

Long 64bit

Integer 32bit

short 16bit

Byte 8bit

 

浮点型:选择正确的类型节约存储空间

Double 64bit

Float 32bit

Half float 16bit

Scaled float 缩放浮点型,基于Long实现

浮点类型会出现精度问题!

 

日期类型:

date:底层基于Long类型实现

日期格式化

format支持多种格式,建议使用utc时间作为标准。


#删除索引
DELETE shop_001

#创建索引字段映射,text,keyword
PUT shop_001
{
  "mappings": {
    "dynamic":true, 
    "_source": {
      "enabled": true
    }, 
    "properties": {
      "shopName":{
        "type": "text"
      },
      "city":{
        "type": "keyword"
      }
    }
  }
}

#查看索引字段映射
GET shop_001/_mapping

#添加一个数据
PUT shop_001/_doc/1
{
  "shopName":"good to study",
  "city":"hangzhou"
}

#查看数据,数据
GET shop_001/_doc/1

POST _analyze
{
  "analyzer": "standard",
  "text": ["good to study"]
}



#整型
DELETE account_001

PUT account_001
{
  "mappings": {
    "properties": {
      "money":{
        "type":"long"
      },
      "year":{
        "type": "integer"
      },
      "month":{
        "type": "short"
      },
      "day":{
        "type": "byte"
      }
    }
  }
}

GET account_001/_mapping


#浮点型
DELETE account_001

#scaling_factor转换比例,默认2位小数,eg:存入10.11转换成 10.11 * 100 = 1011
PUT account_001
{
  "mappings": {
    "properties": {
      "money":{
        "type":"scaled_float",
        "scaling_factor": 100
      },
      "total":{
        "type":"scaled_float",
        "scaling_factor": 10000
      }
    }
  }
}

GET account_001/_mapping

#_source存储原始值,统计时会舍弃超出乘以比例精度部分
PUT account_001/_doc/1
{
  "money":12.3456789,
  "total":20000.123456
}

GET account_001/_doc/1

GET account_001/_search


#时间类型
DELETE my_time_001

PUT my_time_001
{
  "mappings": {
    "properties": {
      "date_1":{
        "type": "date"
      },
      "date_2":{
        "type": "date"
      },
      "date_3":{
        "type": "date"
      },
      "date_4":{
        "type": "date"
      }
    }
  }
}

#查看映射类型
GET my_time_001/_mapping

#放入不同时间类型数据
PUT my_time_001/_doc/1
{
  "date_1":"1598366287069",
  "date_2":"1598366307",
  "date_3":"2020-08-25",
  "date_4":"2020-08-25T22:22:22.123456789Z"
}

GET my_time_001/_doc/1



#限制存入时间类型,秒和毫秒比较时会出错,建议统一使用UTC,防止跨端转换错误
DELETE my_time_001

PUT my_time_001
{
  "mappings": {
    "properties": {
      "date_1":{
        "type": "date"
      },
      "date_2":{
        "type": "date",
        "format": ["yyyy-MM-dd HH:mm:ss"]
      }
    }
  }
}

#查看映射类型
GET my_time_001/_mapping

#放入错误时间类型数据报错
PUT my_time_001/_doc/1
{
  "date_1":"1598366287069",
  "date_2":"1598366287"
}

#放入正确的时间格式数据
PUT my_time_001/_doc/1
{
  "date_1":"1598366287069",
  "date_2":"2020-08-25 22:22:22"
}

GET my_time_001/_doc/1

字符类型基于基于倒排索引

整型、浮点型、日期类型基于BDK树算法、底层存储压缩

 

对象类型:表现为JSON,深度建议最大三层。

DELETE person_001

PUT person_001
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "area":{
        "properties": {
          "provice":{
            "type":"keyword"
          },
          "city":{
            "type":"keyword"
          },
          "country":{
            "properties": {
             "addr":{
                "type":"text" 
             }
            }
          }
        }
      }
    }
  }
}

#查看映射
GET person_001/_mapping

PUT person_001/_doc/1
{
  "name":"David",
  "area":{
    "provice":"zheJiang",
    "city":"hangZhou",
    "country":{
      "addr":"xi hu qu san dun zhen"
    }
  }
}

GET person_001/_doc/1
GET person_001/_search

对象、数组类型



#####对象、数组类型
DELETE person_001

PUT person_001
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "area":{
        "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": {
    "bool": {
      "must": [
        {
          "term": {
            "area.provice": {
              "value": "zhe jiang"
            }
          }
        },
        {
          "term": {
            "area.city": {
              "value": "guang zhou"
            }
          }
        }
      ]
    }
  }
}


#####作业:设计三层嵌套索引
DELETE order_001

PUT order_001
{
  "mappings": {
    "properties": {
      "orderSn":{
        "type": "keyword"
      },
      "sellerId":{
        "type": "keyword"
      },
      "sellerNick":{
        "type": "text"
      },
      "buyerId":{
        "type": "keyword"
      },
      "buyerNick":{
        "type": "text"
      },
      "createTime":{
        "type": "date",
        "format": ["yyyy-MM-dd HH:mm:ss"]
      },
      "payTime":{
        "type": "date",
        "format": ["yyyy-MM-dd HH:mm:ss"]
      },
      "receiveTime":{
        "type": "date",
        "format": ["yyyy-MM-dd HH:mm:ss"]
      },
      "payAmount":{
        "type": "long"
      },
      "orderStatus":{
        "type": "integer"
      },
      "confirmStatus":{
        "type": "integer"
      },
      "trackingNumber":{
        "type": "keyword"
      },
      "goods":{
        "properties": {
          "goodsName":{
            "type":"keyword"
          },
          "buyNumber":{
            "type":"integer"
          },
          "sku":{
            "properties": {
              "color":{
                "type":"keyword"
              },
              "size":{
                "type":"keyword"
              }
            }
          }
        }
      }
    }
  }
}

#查看索引
GET order_001/_mapping

PUT order_001/_doc/1
{
  "orderSn":"202012312412431-4322",
  "buyerId":"mb123456",
  "buyerNick":"DavidSoCool",
  "confirmStatus":0,
  "createTime":"2020-08-29 21:21:21",
  "goods":{
    "buyNumber":1,
    "goodsName":"滑板鞋",
    "sku":{
      "color":"七彩祥云",
      "size":"43(270cm)"
    }
  },
  "orderStatus":0,
  "payAmount":19900,
  "payTime":"2020-08-29 21:23:11",
  "receiveTime":"2020-08-29 21:33:41",
  "sellerId":"shop12345678",
  "trackingNumber":"SF123456789000"
}


### 整型范围类型
DELETE socks_001

#袜子
PUT socks_001
{
  "mappings": {
    "properties": {
      "socksName":{
        "type": "text"
      },
      "sex":{
        "type": "keyword"
      },
      "size":{
        "type": "integer_range"
      }
    }
  }
}

GET socks_001/_mapping

#?refresh强制刷新
PUT socks_001/_doc/1?refresh
{
  "shoesName":"时尚男袜韩版学生长袜",
  "sex":"男",
  "size":{
    "gte":42,
    "lte":44
  }
}
PUT socks_001/_doc/2?refresh
{
  "shoesName":"星期袜时尚男袜韩版学生长袜",
  "sex":"男",
  "size":{
    "gte":40,
    "lte":42
  }
}

GET socks_001/_search

# 范围查询
GET socks_001/_search
{
  "query": {
    "term": {
      "size": 43
    }
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值