下面先簡單描述一下mapping是什麼?html
自動或手動爲index中的type創建的一種數據結構和相關配置,簡稱爲mapping
dynamic mapping,自動爲咱們創建index,建立type,以及type對應的mapping,mapping中包含了每一個field對應的數據類型,以及如何分詞等設置web
當咱們插入幾條數據,讓ES自動爲咱們創建一個索引數組
PUT /website/article/1 { "post_date": "2019-08-21", "title": "my first article", "content": "this is my first article in this website", "author_id": 11400 } PUT /website/article/2 { "post_date": "2019-08-22", "title": "my second article", "content": "this is my second article in this website", "author_id": 11400 } PUT /website/article/3 { "post_date": "2019-08-23", "title": "my third article", "content": "this is my third article in this website", "author_id": 11400 }
查看mapping數據結構
GET /website/_mapping { "website": { "mappings": { "article": { "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 } } } } } } } }
上面是插入數據自動生成的mapping,還有手動生成的mapping。這種自動或手動爲index中的type創建的一種數據結構和相關配置,稱爲mapping。app
嘗試各類搜索ide
GET /website/article/_search?q=2019 //3條結果 GET /website/article/_search?q=2019-08-21 //3條結果 GET /website/article/_search?q=post_date:2019-08-21 //1條結果 GET /website/article/_search?q=post_date:2019 //0條結果
搜索結果爲何不一致,由於es自動創建mapping的時候,設置了不一樣的field不一樣的data type。不一樣的data type的分詞、搜索等行爲是不同的。因此出現了_all field和post_date field的搜索表現徹底不同。
下面是手動建立的mapping。post
PUT /test_mapping { "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 } } } } } }
也就是某個field必須所有匹配才能返回相應的document
示例:測試
GET /website/article/_search?q=post_date:2019-08-21 //1條結果 GET /website/article/_search?q=post_date:2019 //0條結果
exact value,搜索的時候,必須輸入2019-08-21,才能搜索出來
若是你輸入一個21,是搜索不出來的ui
full text與exact value不同,不是說單純的只是匹配完整的一個值,而是能夠對值進行拆分詞語後(分詞)進行匹配,也能夠經過縮寫、時態、大小寫、同義詞等進行匹配。
示例:this
GET /website/article/_search?q=2019 //3條結果 GET /website/article/_search?q=2019-08-21 //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.
分詞,初步的倒排索引的創建
word doc1 doc2 I * * really * liked * * my * * small * dogs * and * think * mom * * also * them * He * never * any * so * hope * that * will * not * expect * me * to * him *
搜索 mother like little dog, 不會有任何結果
mother
like
little
dog
這確定不是咱們想要的結果。好比mother和mom其實根本就沒有區別。可是卻檢索不到。可是作下測試發現ES是能夠查到的。實際上ES在創建倒排索引的時候,還會執行一個操做,就是會對拆分的各個單詞進行相應的處理,以提高後面搜索的時候可以搜索到相關聯的文檔的機率。像時態的轉換,單複數的轉換,同義詞的轉換,大小寫的轉換。這個過程稱爲正則化(normalization)
mother-> mom
liked -> like
small -> little
dogs -> dog
這樣從新創建倒排索引:
word doc1 doc2 I * * really * like * * my * * little * dog * and * think * mom * * also * them * He * never * any * so * hope * that * will * not * expect * me * to * him *
查詢:mother like little dog 分詞正則化
mother -> mom
like -> like
little -> little
dog -> dog
doc1和doc2都會搜索出來
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.
切分詞語,normalization(提高recall召回率)
給你一段句子,而後將這段句子拆分紅一個一個的單個的單詞,同時對每一個單詞進行normalization(時態轉換,單複數轉換),分瓷器
recall,召回率:搜索的時候,增長可以搜索到的結果的數量
一個分詞器,很重要,將一段文本進行各類處理,最後處理好的結果纔會拿去創建倒排索引
內置分詞器的介紹:
待分詞:Set the shape to semi-transparent by calling set_trans(5) standard analyzer:set, the, shape, to, semi, transparent, by, calling, set_trans, 5(默認的是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
mapping引入案例遺留問題大揭祕
GET /_search?q=2019
搜索的是_all field,document全部的field都會拼接成一個大串,進行分詞
2019-01-02 my second article this is my second article in this website 11400
doc1 doc2 doc3 2019 * * * 01 * 02 * 03 *
_all,2017,天然會搜索到3個docuemnt
GET /_search?q=post_date:2019-01-01
date,會做爲exact value去創建索引
doc1 doc2 doc3 2017-01-01 * 2017-01-02 * 2017-01-03 *
語法:
GET /_analyze { "analyzer": "standard", "text": "Text to analyze" }
{ "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 } ] }
mapping本質上就是index的type的元數據,決定了數據類型,創建倒排索引的行爲,還有進行搜索的行爲。
string text:字符串類型 byte:字節類型 short:短整型 integer:整型 long:長整型 float:浮點型 boolean:布爾類型 date:時間類型
固然還有一些高級類型,像數組,對象object,但其底層都是text字符串類型
true or false -> boolean 123 -> long 123.45 -> float 2017-01-01 -> date "hello world" -> string text
查看mapping
語法:GET /{index}/_mapping
GET /{index}/_mapping/{type}
注意:只能建立index時手動創建mapping,或者新增field mapping,可是不能update field mapping。
"analyzer": "standard":自動分詞
date:日期
keyword:不分詞
# 建立索引 PUT /website { "mappings": { "properties": { "author_id": { "type": "long" }, "title": { "type": "text", "analyzer": "standard" }, "content": { "type": "text" }, "post_date": { "type": "date" }, "publisher_id": { "type": "keyword" } } } } #修改字段的mapping PUT /website { "mappings": { "properties": { "author_id": { "type": "text" } } } } { "error": { "root_cause": [ { "type": "resource_already_exists_exception", "reason": "index [website/5xLohnJITHqCwRYInmBFmA] already exists", "index_uuid": "5xLohnJITHqCwRYInmBFmA", "index": "website" } ], "type": "resource_already_exists_exception", "reason": "index [website/5xLohnJITHqCwRYInmBFmA] already exists", "index_uuid": "5xLohnJITHqCwRYInmBFmA", "index": "website" }, "status": 400 } #增長mapping的字段 PUT /website/_mapping { "properties": { "new_field": { "type": "text" } } } { "acknowledged" : true }
{ "tags": ["tag1", "tag2"] }
創建索引時與string是同樣的,數據類型不能混
null,[],[null]
PUT /company/employee/1 { "address": { "country": "china", "province": "guangdong", "city": "guangzhou" }, "name": "jack", "age": 27, "join_date": "2017-01-01" }
查看mapping
GET /company/_mapping/employee
{ "company": { "mappings": { "employee": { "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 field底層解析
{ "address": { "country": "china", "province": "guangdong", "city": "guangzhou" }, "name": "jack", "age": 27, "join_date": "2017-01-01" }
↓↓↓↓
{ "name": [jack], "age": [27], "join_date": [2017-01-01], "address.country": [china], "address.province": [guangdong], "address.city": [guangzhou] }
{ "authors": [ { "age": 26, "name": "Jack White"}, { "age": 55, "name": "Tom Jones"}, { "age": 39, "name": "Kitty Smith"} ] }
↓↓↓↓
{ "authors.age": [26, 55, 39], "authors.name": [jack, white, tom, jones, kitty, smith] }