這篇文章主要講解了“ElasticSearch的映射mapping是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ElasticSearch的映射mapping是什么”吧!

武山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,武山網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為武山上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的武山做網(wǎng)站的公司定做!
mapping是類似于數(shù)據(jù)庫中的表結(jié)構(gòu)定義,主要作用如下:
定義index下的字段名
定義字段類型,比如數(shù)值型、浮點型、布爾型等
定義倒排索引相關(guān)的設(shè)置,比如是否索引、記錄position等
在具體介紹mapping前,有個概念必須先了解:ElasticSearch7-去掉type概念
? 關(guān)系型數(shù)據(jù)庫中兩個數(shù)據(jù)表示是獨立的,即使他們里面有相同名稱的列也不影響使用,但ES中不是這樣的。 elasticsearch是基于Lucene開發(fā)的搜索引擎,而ES中不同type下名稱相同
的filed最終在Lucene中的處理方式是一樣的。
? 兩個不同type下的兩個user_name,在ES同一個索引下其實被認為是同一個filed,你必
須在兩個不同的type中定義相同的filed映射。否則,不同type中的相同字段名稱就會在
處理中出現(xiàn)沖突的情況,導(dǎo)致Lucene處理效率下降。
? 去掉type就是為了提高ES處理數(shù)據(jù)的效率。
? Elasticsearch 7.x
? URL中的type參數(shù)為可選。比如,索引一個文檔不再要求提供文檔類型。
? Elasticsearch 8.x
? 不再支持URL中的type參數(shù)。
? 解決:將索引從多類型遷移到單類型,每種類型文檔一個獨立索引
GET /[index_name]/_mapping
PUT /my-index-000001
{
"mappings": {
"properties": {
"age": { "type": "integer" },
"email": { "type": "keyword" },
"name": { "type": "text" }
}
}
}說明:
integer:數(shù)字類型,默認是long
keyword:檢索時只能精確匹配
text:全文檢索,保存數(shù)據(jù)時會進行分詞
PUT /my-index-000001/_mapping
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false
}
}
}說明
index:默認是true,被索引,也就是參與檢索,這邊設(shè)為false,說明該字段不參與檢索。
對于已經(jīng)存在的映射字段, 禁止直接修改 。更新必須創(chuàng)建新的索引進行數(shù)據(jù)遷移。 因為 lucene實現(xiàn)的倒排索引生成后不允許修改,應(yīng)該重新建立新的索引,然后做reindex操作。
但是可以新增字段,通過 dynamic 參數(shù)來控制字段的新增,這個參數(shù)的值如下:
true:默認值,表示允許選自動新增字段
false:不允許自動新增字段,但是文檔可以正常寫入,但無法對字段進行查詢等操作
strict:嚴格模式,文檔不能寫入,報錯
首先創(chuàng)建名為 my_dynamic_false的索引并設(shè)置mapping:
PUT my_dynamic_false
{
"mappings": {
"dynamic": false,
"properties": {
"title": {
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}然后寫入一個文檔:
PUT my_dynamic_false/_doc/1
{
"title": "hello world",
"desc": "this is book"
}注意,這里在mapping設(shè)置中,”dynamic”: false,表示在寫入文檔時,如果寫入字段不存在也不會報錯。這里的desc字段就是不存在的字段。
查詢一下寫入的文檔:
GET my_dynamic_false/_search
{
"query": {
"match": {
"title": "hello"
}
}
}結(jié)果: 可以通過 title字段查詢到文檔的內(nèi)容
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "my_dynamic_false",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"title" : "hello world",
"desc" : "this is book"
}
}
]
}
}通過desc字段查詢文檔
GET my_dynamic_false/_search
{
"query": {
"match": {
"desc": "book"
}
}
}結(jié)果是查不到
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}驗證一下”dynamic”: strict模式:
創(chuàng)建mapping
PUT my_dynamic_strict
{
"mappings": {
"dynamic": "strict",
"properties": {
"title": {
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}創(chuàng)建文檔
PUT my_dynamic_strict/_doc/1
{
"title": "hello world",
"desc": "this is book"
}結(jié)果發(fā)現(xiàn)直接報錯了
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [desc] within [_doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [desc] within [_doc] is not allowed"
},
"status": 400
}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" : "text"
},
"lastname" : {
"type" : "text"
},
"state" : {
"type" : "keyword"
}
}
}
}POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest":{
"index": "newbank"
}
}作用是將該字段的值復(fù)制到目標字段,不會出現(xiàn)在_source中,只能用來搜索。
PUT my_index_copy
{
"mappings": {
"properties": {
"first_name": {
"type": "text"
, "copy_to": "full_name"
},
"last_name": {
"type": "text"
, "copy_to": "full_name"
},
"full_name" : {
"type": "text"
}
}
}
}然后創(chuàng)建一個新的文檔,文檔只需要寫first_name 和 last_name即可:
PUT my_index_copy/_doc/1
{
"first_name": "john",
"last_name": "smith"
}查詢:
GET my_index_copy/_search
{
"query": {
"match": {
"full_name": {
"query": "john smith"
}
}
}
}結(jié)果可以查到數(shù)據(jù),但結(jié)果集中沒有full_name
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "my_index_copy",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"first_name" : "john",
"last_name" : "smith"
}
}
]
}
}感謝各位的閱讀,以上就是“ElasticSearch的映射mapping是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對ElasticSearch的映射mapping是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
當前名稱:ElasticSearch的映射mapping是什么
文章出自:http://chinadenli.net/article4/pgjiie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、企業(yè)網(wǎng)站制作、動態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計、自適應(yīng)網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)