目錄
1、Mapping字段映射概述
2、Mapping字段常用類型
3、映射中對(duì)時(shí)間類型詳解
1)采取自動(dòng)映射器來映射
2)手工映射提前指定日期類型
4、ES的keyword的屬性ignore_above
5、Mapping映射的查看和創(chuàng)建
1)查看mapping信息:GET 索引名/_mapping
2)創(chuàng)建映射:PUT /索引名
3)?查看所有索引映射關(guān)系
4)修改索引映射關(guān)系
5)一次性創(chuàng)建索引和映射
6、數(shù)據(jù)遷移-reindex
7、ik_max_word、ik_smart分詞器
1、Mapping字段映射概述
????????映射(Mapping)相當(dāng)于數(shù)據(jù)表的表結(jié)構(gòu)。ElasticSearch中的映射(Mapping)用來定義一個(gè)文檔,可以定義所包含的字段以及字段的類型、分詞器及屬性等等
????????映射可以分為動(dòng)態(tài)映射和靜態(tài)映射:
- 動(dòng)態(tài)映射(dynamic mapping):在關(guān)系數(shù)據(jù)庫中,需要事先創(chuàng)建數(shù)據(jù)庫,然后在該數(shù)據(jù)庫實(shí)例下創(chuàng)建數(shù)據(jù)表,然后才能在該數(shù)據(jù)表中插入數(shù)據(jù)。而ElasticSearch中不需要事先定義映射(Mapping),文檔寫入ElasticSearch時(shí),會(huì)根據(jù)文檔字段自動(dòng)識(shí)別類型。這種機(jī)制稱之為動(dòng)態(tài)映射
- 靜態(tài)映射 :在ElasticSearch中也可以事先定義好映射,包含文檔的各個(gè)字段及其類型等,這種方式稱之為靜態(tài)映射
2、Mapping字段常用類型
1)字符串(text、keyword)
- text:可分詞,不可參與聚合
- keyword:不可分詞,數(shù)據(jù)會(huì)作為完整字段進(jìn)行匹配,可以參與聚合
?2)整數(shù)
?3)浮點(diǎn)類型
?4)date類型
- 日期格式的字符串,比如"2018-01-13"或"2018-01-13 12:10:30"
- long類型的毫秒數(shù)(從1970年開始)
- integer的秒數(shù)
5)?boolean類型
????????邏輯類型(布爾類型)可以接受true/false
6)?binary類型
????????二進(jìn)制字段是指base64來表示索引中儲(chǔ)存的二進(jìn)制數(shù)據(jù),可用來儲(chǔ)存二進(jìn)制形式的數(shù)據(jù),例如圖像。默認(rèn)情況下,該類型的字段只儲(chǔ)存不索引。二進(jìn)制只支持index_name屬性
7)array類型
8)object類型
????????JSON天生具有層級(jí)關(guān)系,文檔會(huì)包含嵌套的對(duì)象
3、映射中對(duì)時(shí)間類型詳解
????????假如我們有如下索引tax,保存了一些公司的納稅或資產(chǎn)信息,單位為"萬元"。當(dāng)前這個(gè)例子里。索引達(dá)的含義并不重要,關(guān)鍵點(diǎn)在于字段的內(nèi)容格式。我們看到date字段其中包含了多種日期的格式:“yyyy-MM-dd”,"yyyy-MM-dd"還有時(shí)間戳。
1)采取自動(dòng)映射器來映射
????????如果按照dynamic mapping,采取自動(dòng)映射器來映射索引,我們自然而然的都會(huì)感覺字段應(yīng)該是一個(gè)date類型;我們把數(shù)據(jù)存入索引然后查看tax索引的mapping;
POST tax/_bulk
{"index":{}}
{"date": "2021-01-27 10:01:54","company": "騰訊","ratal": 6500}
{"index":{}}
{"date": "2021-01-28 10:01:32","company": "螞蟻金服","ratal": 5000}
{"index":{}}
{"date": "2021-01-29 10:01:21","company": "字節(jié)跳動(dòng)","ratal": 10000}
{"index":{}}
{"date": "2021-01-30 10:02:07","company": "中國(guó)石油","ratal": 18302097}
{"index":{}}
{"date": "1648100904","company": "中國(guó)石化","ratal": 32654722}
{"index":{}}
{"date": "2021-11-1 12:20:00","company": "國(guó)家電網(wǎng)","ratal": 82950000}
查看tax索引的mapping:
"properties" : {
"date" : {
"type" : "text","fields" : {
"keyword" : {
"type" : "keyword","ignore_above" : 256
}
}
}
}
? ? ? ? 我們可以看到date居然是一個(gè)text類型。這是為什么呢,原因就在于對(duì)時(shí)間類型的格式的要求是絕對(duì)嚴(yán)格的。要求必須是一個(gè)標(biāo)準(zhǔn)的UTC時(shí)間類型。上述字段的數(shù)據(jù)格式如果想要使用,就必須使用yyyy-MM-ddTHH:mm:ssZ格式(其中T個(gè)間隔符,Z代表 0 時(shí)區(qū));
????????錯(cuò)誤的時(shí)間格式均無法被自動(dòng)映射器識(shí)別為日期時(shí)間類型,例如:
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd
- 時(shí)間戳
- 2022-4-30T20:00:00Z
2)手工映射提前指定日期類型
PUT tax
{
"mappings": {
"properties": {
"date": {
"type": "date"
}
}
}
}
POST tax/_bulk
{"index":{}}
{"date": "2021-01-30 10:02:07","ratal": 32654722}
{"index":{}}
{"date": "2021-11-1T12:20:00Z","ratal": 82950000}
{"index":{}}
{"date": "2021-01-30T10:02:07Z","ratal": 18302097}
{"index":{}}
{"date": "2021-01-25","ratal": 5700000}
第一個(gè)(寫入失敗):2021-01-30 10:02:07
第二個(gè)(寫入成功):1648100904
第三個(gè)(寫入失敗):2021-11-1T12:20:00Z
第四個(gè)(寫入成功):2021-01-30T10:02:07Z
第五個(gè)(寫入成功):2021-01-25
總結(jié):
1.對(duì)于yyyy-MM-dd HH:mm:ss或2021-11-1T12:20:00Z,ES 的自動(dòng)映射器完全無法識(shí)別,即便是事先聲明日期類型,數(shù)據(jù)強(qiáng)行寫入也會(huì)失敗
2.對(duì)于時(shí)間戳和yyyy-MM-dd這樣的時(shí)間格式,ES 自動(dòng)映射器無法識(shí)別,但是如果事先說明了日期類型是可以正常寫入的
3.對(duì)于標(biāo)準(zhǔn)的日期時(shí)間類型是可以正常自動(dòng)識(shí)別為日期類型,并且也可以通過手工映射來實(shí)現(xiàn)聲明字段類型
實(shí)際開發(fā)中的解決方法:
//只需要在字段屬性中添加一個(gè)參數(shù):
“format”: “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”,這樣就可以避免因?yàn)閿?shù)據(jù)格式不統(tǒng)一而導(dǎo)致數(shù)據(jù)無法寫入的窘境
PUT test_index
{
"mappings": {
"properties": {
"time": {
"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
4、ES的keyword的屬性ignore_above
????????在es的5.x版本,keyword類型字段可以設(shè)置ignore_above,表示最大的字段值長(zhǎng)度,超出這個(gè)長(zhǎng)度的字段將不會(huì)被索引,但是會(huì)存儲(chǔ);舉個(gè)例子:設(shè)置message 的長(zhǎng)度最長(zhǎng)為20,超過20的不被索引,這里的不被索引是這個(gè)字段不被索引,但是其他字段有的話仍然被索引到;
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"message": {
"type": "keyword",
"ignore_above": 20
}
}
}
}
}
# 下面造點(diǎn)數(shù)據(jù)
PUT my_index/my_type/3
{
"message": "123456789"
}
PUT my_index/my_type/5
{
"message": "123456789012345678901"
}
全部查詢:
?模糊匹配:
????????我們可以發(fā)現(xiàn)你用模糊匹配是搜索不到的(注意上面的數(shù)據(jù)最后帶個(gè)1是21位下圖是20位的),同樣?用精確匹配前面20個(gè)仍然搜索不到;
5、Mapping映射的查看和創(chuàng)建
1)查看mapping信息:GET 索引名/_mapping
{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long" # long類型
},
"address" : {
"type" : "text", # 文本類型,會(huì)進(jìn)行全文檢索,進(jìn)行分詞
"fields" : {
"keyword" : { # addrss.keyword
"type" : "keyword", # 該字段必須全部匹配到
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"employer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
ElasticSearch7-去掉type概念:
1、關(guān)系型數(shù)據(jù)庫中兩個(gè)數(shù)據(jù)表示是獨(dú)立的,即使他們里面有相同名稱的列也不影響使用,但ES中不是這樣的。elasticsearch是基于Lucene開發(fā)的搜索引擎,而ES中不同type下名稱相同的filed最終在Lucene中的處理方式是一樣的
- (1)兩個(gè)不同type下的兩個(gè)user_name,在ES同一個(gè)索引下其實(shí)被認(rèn)為是同一個(gè)filed,你必須在兩個(gè)不同的type中定義相同的filed映射。否則,不同type中的相同字段名稱就會(huì)在處理中出現(xiàn)沖突的情況,導(dǎo)致Lucene處理效率下降。
- (2)去掉type就是為了提高ES處理數(shù)據(jù)的效率。
2、Elasticsearch 7.x URL中的type參數(shù)為可選。比如,索引一個(gè)文檔不再要求提供文檔類型
3、Elasticsearch 8.x 不再支持URL中的type參數(shù)
解決:將索引從多類型遷移到單類型,每種類型文檔一個(gè)獨(dú)立索引;將已存在的索引下的類型數(shù)據(jù),全部遷移到指定位置即可;
2)創(chuàng)建映射:PUT /索引名
創(chuàng)建Mapping映射語法:
PUT /索引庫名/_mapping
{
"properties": {
"字段名": {
"type": "類型", # type:類型,可以是text、long、short、date、integer、object等
"index": true, # index:是否索引,默認(rèn)為true
"store": false, # store:是否存儲(chǔ),默認(rèn)為false
"analyzer": "分詞器" # analyzer:指定分詞器
}
}
}
PUT /company-index/_mapping
{
"properties": {
"name": {
"type": "text",
"index": true,
"analyzer": "ik_max_word"
},
"job": {
"type": "text",
"analyzer": "ik_max_word"
},
"logo": {
"type": "keyword",
"index": false
},
"payment": {
"type": "float"
}
}
}
1、index影響字段的索引情況:
- true:字段會(huì)被索引,則可以用來進(jìn)行搜索。默認(rèn)值就是true
- false:字段不會(huì)被索引,不能用來搜索
????????index的默認(rèn)值就是true,也就是說你不進(jìn)行任何配置,所有字段都會(huì)被索引。但是有些字段是我們不希望被索引的,比如企業(yè)的logo圖片地址,就需要手動(dòng)設(shè)置index為false;
2、store:是否將數(shù)據(jù)進(jìn)行獨(dú)立存儲(chǔ):
????????原始的文本會(huì)存儲(chǔ)在_source里面,默認(rèn)情況下其他提取出來的字段都不是獨(dú)立存儲(chǔ)的,是從_source里面提取出來的。當(dāng)然你也可以獨(dú)立的存儲(chǔ)某個(gè)字段,只要設(shè)置store:true即可,獲取獨(dú)立存儲(chǔ)的字段要比從_source中解析快得多,但是也會(huì)占用更多的空間,所以要根據(jù)實(shí)際業(yè)務(wù)需求來設(shè)置,默認(rèn)為false;
3、analyzer:指定分詞器:
????????一般我們處理中文會(huì)選擇ik分詞器 ik_max_word、ik_smart
PUT /my_index
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword" # 指定為keyword
},
"name": {
"type": "text" # 全文檢索。保存時(shí)候分詞,檢索時(shí)候進(jìn)行分詞匹配
}
}
}
}
輸出:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my_index"
}
查看映射GET /my_index
輸出結(jié)果:
{
"my_index" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"email" : {
"type" : "keyword"
},
"employee-id" : {
"type" : "keyword",
"index" : false
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1588410780774",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "ua0lXhtkQCOmn7Kh3iUu0w",
"version" : {
"created" : "7060299"
},
"provided_name" : "my_index"
}
}
}
}
# 添加新的字段映射PUT /my_index/_mapping
PUT /my_index/_mapping
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false # 字段不能被檢索。檢索
}
}
}
這里的 "index": false,表明新增的字段不能被檢索,只是一個(gè)冗余字段。
注意:不能更新映射:對(duì)于已經(jīng)存在的字段映射,我們不能更新。更新必須創(chuàng)建新的索引,進(jìn)行數(shù)據(jù)遷移
3)?查看所有索引映射關(guān)系
- GET _mapping
- GET _all/_mapping
4)修改索引映射關(guān)系
PUT /索引庫名/_mapping
{
"properties": {
"字段名": {
"type": "類型",
"index": true,
"store": true,
"analyzer": "分詞器"
}
}
}
注意:這里修改映射是增加字段,做其它更改只能刪除索引重新建立映射
5)一次性創(chuàng)建索引和映射
put /索引庫名稱
{
"settings": {
"索引庫屬性名": "索引庫屬性值"
},
"mappings": {
"properties": {
"字段名": {
"映射屬性名": "映射屬性值"
}
}
}
}
6、數(shù)據(jù)遷移-reindex
????????先創(chuàng)建new_twitter的正確映射,然后使用如下方式進(jìn)行數(shù)據(jù)遷移
6.0以后寫法
POST reindex
{
"source":{
"index":"twitter"
},
"dest":{
"index":"new_twitters"
}
}
老版本寫法
POST reindex
{
"source":{
"index":"twitter",
"type":"twitter"
},
"dest":{
"index":"new_twitters"
}
}
案例:原來類型為account,新版本沒有類型了,所以我們把他去掉;想要將年齡修改為integer,先創(chuàng)建新的索引;
1.原索引:
GET /bank/_search
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "account",//原來類型為account,新版本沒有類型了,所以我們把他去掉
"_id" : "1",
"_score" : 1.0,
"_source" : {
"account_number" : 1,
"balance" : 39225,
"firstname" : "Amber",
"lastname" : "Duke",
"age" : 32,
"gender" : "M",
"address" : "880 Holmes Lane",
"employer" : "Pyrami",
"email" : "amberduke@pyrami.com",
"city" : "Brogan",
"state" : "IL"
}
},
...
GET /bank/_search
查出
"age":{"type":"long"}
2.創(chuàng)建新的索引:
PUT /newbank
{
"mappings": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text"
},
"age": {
"type": "integer"
},
"balance": {
"type": "long"
},
"city": {
"type": "keyword"
},
"email": {
"type": "keyword"
},
"employer": {
"type": "keyword"
},
"firstname": {
"type": "text"
},
"gender": {
"type": "keyword"
},
"lastname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "keyword"
}
}
}
}
查看"newbank"的映射:
GET /newbank/_mapping
能夠看到age的映射類型被修改為了integer.
"age":{"type":"integer"}
3.將bank中的數(shù)據(jù)遷移到newbank中:
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}
運(yùn)行輸出:
#! Deprecation: [types removal] Specifying types in reindex requests is deprecated.
{
"took" : 768,
"timed_out" : false,
"total" : 1000,
"updated" : 0,
"created" : 1000,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
4.查看newbank中的數(shù)據(jù):
GET /newbank/_search
輸出
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "newbank",
"_type" : "_doc", # 沒有了類型
7、ik_max_word、ik_smart分詞器
????????一個(gè)tokenizer(分詞器)接收一個(gè)字符流,將之分割為獨(dú)立的tokens(詞元,通常是獨(dú)立的單詞),然后輸出tokens流;例如:whitespace tokenizer遇到空白字符時(shí)分割文本。它會(huì)將文本"Quick brown fox!"分割為(Quick,brown,fox!);
????????該tokenizer(分詞器)還負(fù)責(zé)記錄各個(gè)terms(詞條)的順序或position位置(用于phrase短語和word proximity詞近鄰查詢),以及term(詞條)所代表的原始word(單詞)的start(起始)和end(結(jié)束)的character offsets(字符串偏移量)(用于高亮顯示搜索的內(nèi)容)。elasticsearch提供了很多內(nèi)置的分詞器(標(biāo)準(zhǔn)分詞器),可以用來構(gòu)建custom analyzers(自定義分詞器)。
? ? ? ? 分詞器的安裝:根據(jù)當(dāng)前es的版本,去找到對(duì)應(yīng)版本的分詞器,下載.zip文件,然后解壓到elasticsearch/plugins文件夾下即可,注意需要將權(quán)限進(jìn)行修改chmod -R 777 plugins/ik;安裝完畢后,需要重啟elasticsearch容器。
POST _analyze
{
"analyzer": "standard",
"text": "The 2 Brown-Foxes bone."
}
執(zhí)行結(jié)果:
{
"tokens" : [
{
"token" : "the",
"start_offset" : 0,
"end_offset" : 3,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "2",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<NUM>",
"position" : 1
},
{
"token" : "brown",
"start_offset" : 6,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "foxes",
"start_offset" : 12,
"end_offset" : 17,
"type" : "<ALPHANUM>",
"position" : 3
},
{
"token" : "bone",
"start_offset" : 18,
"end_offset" : 22,
"type" : "<ALPHANUM>",
"position" : 4
}
]
}
????????ik_max_word:會(huì)將文本做最細(xì)粒度的拆分,比如會(huì)將"湖南省岳陽縣"拆分為"湖南省、湖南、省、 岳陽縣、岳陽、縣等詞語(索引的時(shí)候用ik_max_word):
GET _analyze
{
"analyzer": "ik_max_word",
"text":"湖南省岳陽縣"
}
{
"tokens" : [
{
"token" : "湖南省",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "湖南",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "省",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "岳陽縣",
"start_offset" : 3,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "岳陽",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "縣",
"start_offset" : 5,
"end_offset" : 6,
"type" : "CN_CHAR",
"position" : 5
}
]
}
????????ik_smart:會(huì)做最粗粒度的拆分,比如會(huì)將"湖南省岳陽縣"拆分為湖南省、岳陽縣(前臺(tái)搜索的時(shí)候用 ik_smart):
GET _analyze
{
"analyzer": "ik_smart",
"text":"湖南省岳陽縣"
}
{
"tokens" : [
{
"token" : "湖南省",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "岳陽縣",
"start_offset" : 3,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
}
]
}
????????用戶還可以通過修改elasticsearch/plugins/ik/config中的IKAnalyzer.cfg.xml文件自定義分詞器;文章來源:http://www.zghlxwxcb.cn/news/detail-806765.html
參考原文地址:http://t.csdn.cn/WK4mn文章來源地址http://www.zghlxwxcb.cn/news/detail-806765.html
到了這里,關(guān)于【Elasticsearch學(xué)習(xí)筆記二】es的Mapping字段映射、Mapping字段常用類型、Mapping映射的創(chuàng)建、查看和更新、es數(shù)據(jù)遷移、ik分詞器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!