Elasticsearch之mapping映射入门
10.1. 什么是mapping映射
概念:自動(dòng)或手動(dòng)為index中的_doc建立的一種數(shù)據(jù)結(jié)構(gòu)和相關(guān)配置,簡稱為mapping映射。
插入幾條數(shù)據(jù),讓es自動(dòng)為我們建立一個(gè)索引
PUT /website/_doc/1 {"post_date": "2019-01-01","title": "my first article","content": "this is my first article in this website","author_id": 11400 } ? PUT /website/_doc/2 {"post_date": "2019-01-02","title": "my second article","content": "this is my second article in this website","author_id": 11400 }PUT /website/_doc/3 {"post_date": "2019-01-03","title": "my third article","content": "this is my third article in this website","author_id": 11400 }對(duì)比數(shù)據(jù)庫建表語句
create table website(post_date date,title varchar(50), ? ? content varchar(100),author_id int(11) );動(dòng)態(tài)映射:dynamic mapping,自動(dòng)為我們建立index,以及對(duì)應(yīng)的mapping,mapping中包含了每個(gè)field對(duì)應(yīng)的數(shù)據(jù)類型,以及如何分詞等設(shè)置。
重點(diǎn):我們當(dāng)然,后面會(huì)講解,也可以手動(dòng)在創(chuàng)建數(shù)據(jù)之前,先創(chuàng)建index,以及對(duì)應(yīng)的mapping
GET /website/_mapping {"website" : {"mappings" : {"properties" : {"author_id" : {"type" : "long"},"content" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"post_date" : {"type" : "date"},"title" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}} }嘗試各種搜索
GET /website/_search?q=2019 ? ? ? 0條結(jié)果 ? ? ? ? ? ? GET /website/_search?q=2019-01-01 ? ? ? ? ? 1條結(jié)果 GET /website/_search?q=post_date:2019-01-01 ? ? 1條結(jié)果 GET /website/_search?q=post_date:2019 ? ? ? ? 0 條結(jié)果搜索結(jié)果為什么不一致,因?yàn)閑s自動(dòng)建立mapping的時(shí)候,設(shè)置了不同的field不同的data type。不同的data type的分詞、搜索等行為是不一樣的。所以出現(xiàn)了_all field和post_date field的搜索表現(xiàn)完全不一樣。
10.2. 精確匹配與全文搜索的對(duì)比分析
10.2.1 exact value 精確匹配
2019-01-01,exact value,搜索的時(shí)候,必須輸入2019-01-01,才能搜索出來
如果你輸入一個(gè)01,是搜索不出來的
select * from book where name= 'java'
10.2.2 full text 全文檢索
搜“筆記電腦”,筆記本電腦詞條會(huì)不會(huì)出現(xiàn)。
select * from book where name like '%java%'
(1)縮寫 vs. 全稱:cn vs. china
(2)格式轉(zhuǎn)化:like liked likes
(3)大小寫:Tom vs tom
(4)同義詞:like vs love
?
2019-01-01,2019 01 01,搜索2019,或者01,都可以搜索出來
china,搜索cn,也可以將china搜索出來
likes,搜索like,也可以將likes搜索出來
Tom,搜索tom,也可以將Tom搜索出來
like,搜索love,同義詞,也可以將like搜索出來
就不是說單純的只是匹配完整的一個(gè)值,而是可以對(duì)值進(jìn)行拆分詞語后(分詞)進(jìn)行匹配,也可以通過縮寫、時(shí)態(tài)、大小寫、同義詞等進(jìn)行匹配。深入 NPL,自然語義處理。
10.3. 全文檢索下倒排索引核心原理快速揭秘
doc1:I really liked my small dogs, and I think my mom also liked them.
doc2:He never liked any dogs, so I hope that my mom will not expect me to liked him.
分詞,初步的倒排索引的建立
| I | * | * |
| really | * | ? |
| liked | * | * |
| my | * | * |
| small | * | ? |
| dogs | * | ? |
| and | * | ? |
| think | * | ? |
| mom | * | * |
| also | * | ? |
| them | * | ? |
| He | ? | * |
| never | ? | * |
| any | ? | * |
| so | ? | * |
| hope | ? | * |
| that | ? | * |
| will | ? | * |
| not | ? | * |
| expect | ? | * |
| me | ? | * |
| to | ? | * |
| him | ? | * |
演示了一下倒排索引最簡單的建立的一個(gè)過程
搜索
mother like little dog,不可能有任何結(jié)果
mother
like
little
dog
這不是我們想要的結(jié)果。同義詞mom\mother在我們?nèi)祟惪磥硎且粯印O脒M(jìn)行標(biāo)準(zhǔn)化操作。
重建倒排索引
normalization正規(guī)化,建立倒排索引的時(shí)候,會(huì)執(zhí)行一個(gè)操作,也就是說對(duì)拆分出的各個(gè)單詞進(jìn)行相應(yīng)的處理,以提升后面搜索的時(shí)候能夠搜索到相關(guān)聯(lián)的文檔的概率
時(shí)態(tài)的轉(zhuǎn)換,單復(fù)數(shù)的轉(zhuǎn)換,同義詞的轉(zhuǎn)換,大小寫的轉(zhuǎn)換
mom ―> mother
liked ―> like
small ―> little
dogs ―> dog
重新建立倒排索引,加入normalization,再次用mother liked little dog搜索,就可以搜索到了
| I | * | * | ? |
| really | * | ? | ? |
| like | * | * | liked ―> like |
| my | * | * | ? |
| little | * | ? | small ―> little |
| dog | * | ? | dogs ―> dog |
| and | * | ? | ? |
| think | * | ? | ? |
| mother | * | * | mom ―> mother |
| also | * | ? | ? |
| them | * | ? | ? |
| He | ? | * | ? |
| never | ? | * | ? |
| any | ? | * | ? |
| so | ? | * | ? |
| hope | ? | * | ? |
| that | ? | * | ? |
| will | ? | * | ? |
| not | ? | * | ? |
| expect | ? | * | ? |
| me | ? | * | ? |
| to | ? | * | ? |
| him | ? | * | ? |
重新搜索
搜索:mother liked little dog,
對(duì)搜索條件經(jīng)行分詞 normalization
mother
liked -》like
little
dog
?
doc1和doc2都會(huì)搜索出來
10.4. 分詞器 analyzer
10.4.1什么是分詞器 analyzer
作用:切分詞語,normalization(提升recall召回率)
給你一段句子,然后將這段句子拆分成一個(gè)一個(gè)的單個(gè)的單詞,同時(shí)對(duì)每個(gè)單詞進(jìn)行normalization(時(shí)態(tài)轉(zhuǎn)換,單復(fù)數(shù)轉(zhuǎn)換)
recall,召回率:搜索的時(shí)候,增加能夠搜索到的結(jié)果的數(shù)量
analyzer 組成部分:
1、character filter:在一段文本進(jìn)行分詞之前,先進(jìn)行預(yù)處理,比如說最常見的就是,過濾html標(biāo)簽(<span>hello<span> --> hello),& --> and(I&you --> I and you)
2、tokenizer:分詞,hello you and me --> hello, you, and, me
3、token filter:lowercase,stop word,synonymom,dogs --> dog,liked --> like,Tom --> tom,a/the/an --> 干掉,mother --> mom,small --> little
stop word 停用詞: 了 的 呢。
?
一個(gè)分詞器,很重要,將一段文本進(jìn)行各種處理,最后處理好的結(jié)果才會(huì)拿去建立倒排索引。
10.4.2內(nèi)置分詞器的介紹
例句:Set the shape to semi-transparent by calling set_trans(5)
?
standard analyzer標(biāo)準(zhǔn)分詞器:set, the, shape, to, semi, transparent, by, calling, set_trans, 5(默認(rèn)的是standard)
simple analyzer簡單分詞器:set, the, shape, to, semi, transparent, by, calling, set, trans
whitespace analyzer:Set, the, shape, to, semi-transparent, by, calling, set_trans(5)
language analyzer(特定的語言的分詞器,比如說,english,英語分詞器):set, shape, semi, transpar, call, set_tran, 5
?
官方文檔:
https://www.elastic.co/guide/en/elasticsearch/reference/7.4/analysis-analyzers.html
?
10.5. query string根據(jù)字段分詞策略
10.5.1query string分詞
query string必須以和index建立時(shí)相同的analyzer進(jìn)行分詞
query string對(duì)exact value和full text的區(qū)別對(duì)待
如: date:exact value 精確匹配
text: full text 全文檢索
10.5.2測試分詞器
GET /_analyze {"analyzer": "standard","text": "Text to analyze 80" }返回值:
{"tokens" : [{"token" : "text","start_offset" : 0,"end_offset" : 4,"type" : "<ALPHANUM>","position" : 0},{"token" : "to","start_offset" : 5,"end_offset" : 7,"type" : "<ALPHANUM>","position" : 1},{"token" : "analyze","start_offset" : 8,"end_offset" : 15,"type" : "<ALPHANUM>","position" : 2},{"token" : "80","start_offset" : 16,"end_offset" : 18,"type" : "<NUM>","position" : 3}] }token 實(shí)際存儲(chǔ)的term 關(guān)鍵字
position 在此詞條在原文本中的位置
start_offset/end_offset字符在原始字符串中的位置
10.6. mapping回顧總結(jié)
(1)往es里面直接插入數(shù)據(jù),es會(huì)自動(dòng)建立索引,同時(shí)建立對(duì)應(yīng)的mapping。(dynamic mapping)
(2)mapping中就自動(dòng)定義了每個(gè)field的數(shù)據(jù)類型
(3)不同的數(shù)據(jù)類型(比如說text和date),可能有的是exact value,有的是full text
(4)exact value,在建立倒排索引的時(shí)候,分詞的時(shí)候,是將整個(gè)值一起作為一個(gè)關(guān)鍵詞建立到倒排索引中的;full text,會(huì)經(jīng)歷各種各樣的處理,分詞,normaliztion(時(shí)態(tài)轉(zhuǎn)換,同義詞轉(zhuǎn)換,大小寫轉(zhuǎn)換),才會(huì)建立到倒排索引中。
(5)同時(shí)呢,exact value和full text類型的field就決定了,在一個(gè)搜索過來的時(shí)候,對(duì)exact value field或者是full text field進(jìn)行搜索的行為也是不一樣的,會(huì)跟建立倒排索引的行為保持一致;比如說exact value搜索的時(shí)候,就是直接按照整個(gè)值進(jìn)行匹配,full text query string,也會(huì)進(jìn)行分詞和normalization再去倒排索引中去搜索
(6)可以用es的dynamic mapping,讓其自動(dòng)建立mapping,包括自動(dòng)設(shè)置數(shù)據(jù)類型;也可以提前手動(dòng)創(chuàng)建index和mapping,自己對(duì)各個(gè)field進(jìn)行設(shè)置,包括數(shù)據(jù)類型,包括索引行為,包括分詞器,等。
10.7. mapping的核心數(shù)據(jù)類型以及dynamic mapping
10.7.1 核心的數(shù)據(jù)類型
string :text and keyword
byte,short,integer,long,float,double
boolean
date
詳見:https://www.elastic.co/guide/en/elasticsearch/reference/7.3/mapping-types.html
下圖是ES7.3核心的字段類型如下:
?
10.7.2 dynamic mapping 推測規(guī)則
true or false --> boolean
123 --> long
123.45 --> double
2019-01-01 --> date
"hello world" --> text/keywod
10.7.3 查看mapping
GET /index/_mapping/
10.8 手動(dòng)管理mapping
10.8.1查詢所有索引的映射
GET /_mapping
10.8.2 創(chuàng)建映射 !!
創(chuàng)建索引后,應(yīng)該立即手動(dòng)創(chuàng)建映射
PUT book/_mapping {"properties": {"name": {"type": "text"},"description": {"type": "text","analyzer":"english","search_analyzer":"english"},"pic":{"type":"text","index":false},"studymodel":{"type":"text"}} }Text 文本類型
1)analyzer
通過analyzer屬性指定分詞器。
上邊指定了analyzer是指在索引和搜索都使用english,如果單獨(dú)想定義搜索時(shí)使用的分詞器則可以通過search_analyzer屬性。
2)index
index屬性指定是否索引。
默認(rèn)為index=true,即要進(jìn)行索引,只有進(jìn)行索引才可以從索引庫搜索到。
但是也有一些內(nèi)容不需要索引,比如:商品圖片地址只被用來展示圖片,不進(jìn)行搜索圖片,此時(shí)可以將index設(shè)置為false。
刪除索引,重新創(chuàng)建映射,將pic的index設(shè)置為false,嘗試根據(jù)pic去搜索,結(jié)果搜索不到數(shù)據(jù)。
3)store
是否在source之外存儲(chǔ),每個(gè)文檔索引后會(huì)在 ES中保存一份原始文檔,存放在"source"中,一般情況下不需要設(shè)置store為true,因?yàn)樵趕ource中已經(jīng)有一份原始文檔了。
插入文檔:
PUT /book/_doc/1 {"name":"Bootstrap開發(fā)框架","description":"Bootstrap是由Twitter推出的一個(gè)前臺(tái)頁面開發(fā)框架,在行業(yè)之中使用較為廣泛。此開發(fā)框架包含了大量的CSS、JS程序代碼,可以幫助開發(fā)者(尤其是不擅長頁面開發(fā)的程序人員)輕松的實(shí)現(xiàn)一個(gè)不受瀏覽器限制的精美界面效果。","pic":"group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg","studymodel":"201002" }GET /book/_search?q=name:開發(fā)
GET /book/_search?q=description:開發(fā)
GET /book/_search?q=pic:group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg
GET /book/_search?q=studymodel:201002
通過測試發(fā)現(xiàn):name和description都支持全文檢索,pic不可作為查詢條件。
keyword關(guān)鍵字字段
目前已經(jīng)取代了"index": false。上邊介紹的text文本字段在映射時(shí)要設(shè)置分詞器,keyword字段為關(guān)鍵字字段,通常搜索keyword是按照整體搜索,所以創(chuàng)建keyword字段的索引時(shí)是不進(jìn)行分詞的,比如:郵政編碼、手機(jī)號(hào)碼、身份證等。keyword字段通常用于過慮、排序、聚合等。
date日期類型
日期類型不用設(shè)置分詞器。
通常日期類型的字段用于排序。
format
通過format設(shè)置日期格式
例子:
下邊的設(shè)置允許date字段存儲(chǔ)年月日時(shí)分秒、年月日及毫秒三種格式。
{"properties": {"timestamp": {"type": ? "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"}} }插入文檔:
POST book/_doc/3 {"name": "spring開發(fā)基礎(chǔ)","description": "spring 在java領(lǐng)域非常流行,java程序員都在用。","studymodel": "201001","pic":"group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg","timestamp":"2018-07-04 18:28:58" }數(shù)值類型
下邊是ES支持的數(shù)值類型
?
1、盡量選擇范圍小的類型,提高搜索效率
2、對(duì)于浮點(diǎn)數(shù)盡量用比例因子,比如一個(gè)價(jià)格字段,單位為元,我們將比例因子設(shè)置為100這在ES中會(huì)按 分 存儲(chǔ),映射如下:
"price": {"type": "scaled_float","scaling_factor": 100},由于比例因子為100,如果我們輸入的價(jià)格是23.45則ES中會(huì)將23.45乘以100存儲(chǔ)在ES中。
如果輸入的價(jià)格是23.456,ES會(huì)將23.456乘以100再取一個(gè)接近原始值的數(shù),得出2346。
使用比例因子的好處是整型比浮點(diǎn)型更易壓縮,節(jié)省磁盤空間。
如果比例因子不適合,則從下表選擇范圍小的去用:
更新已有映射,并插入文檔:
PUT book/doc/3 { "name": "spring開發(fā)基礎(chǔ)", "description": "spring 在java領(lǐng)域非常流行,java程序員都在用。", "studymodel": "201001","pic":"group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg","timestamp":"2018-07-04 18:28:58","price":38.6 }?
10.8.3修改映射
只能創(chuàng)建index時(shí)手動(dòng)建立mapping,或者新增field mapping,但是不能update field mapping。
因?yàn)橐延袛?shù)據(jù)按照映射早已分詞存儲(chǔ)好。如果修改,那這些存量數(shù)據(jù)怎么辦。
新增一個(gè)字段mapping
PUT /book/_mapping/ {"properties" : {"new_field" : {"type" : ? "text","index": ? "false"}} }如果修改mapping,會(huì)報(bào)錯(cuò)
PUT /book/_mapping/ {"properties" : {"studymodel" : {"type" : ? ?"keyword"}} }返回:
{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "mapper [studymodel] of different type, current_type [text], merged_type [keyword]"}],"type": "illegal_argument_exception","reason": "mapper [studymodel] of different type, current_type [text], merged_type [keyword]"},"status": 400 }10.8.4刪除映射
通過刪除索引來刪除映射。
10.9 復(fù)雜數(shù)據(jù)類型
10.9 .1 multivalue field
{ "tags": [ "tag1", "tag2" ]}
建立索引時(shí)與string是一樣的,數(shù)據(jù)類型不能混
10.9 .2. empty field
null,[],[null]
10.9 .3. object field
PUT /company/_doc/1 {"address": {"country": "china","province": "guangdong","city": "guangzhou"},"name": "jack","age": 27,"join_date": "2019-01-01" }address:object類型
查詢映射
GET /company/_mapping
{"company" : {"mappings" : {"properties" : {"address" : {"properties" : {"city" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"country" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"province" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}},"age" : {"type" : "long"},"join_date" : {"type" : "date"},"name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}} }object
{"address": {"country": "china","province": "guangdong","city": "guangzhou"},"name": "jack","age": 27,"join_date": "2017-01-01" }底層存儲(chǔ)格式
{"name": ? ? ? ? ? [jack],"age": ? ? ? ? [27],"join_date": ? ? [2017-01-01],"address.country": ? ? ? ? [china],"address.province": ? [guangdong],"address.city": [guangzhou] }對(duì)象數(shù)組:
{"authors": [{ "age": 26, "name": "Jack White"},{ "age": 55, "name": "Tom Jones"},{ "age": 39, "name": "Kitty Smith"}] }存儲(chǔ)格式:
{"authors.age": ? [26, 55, 39],"authors.name": ? [jack, white, tom, jones, kitty, smith] }總結(jié)
以上是生活随笔為你收集整理的Elasticsearch之mapping映射入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Elasticsearch之文档docu
- 下一篇: Elasticsearch之type底层