Mapping
類似于數(shù)據(jù)庫(kù)中的表結(jié)構(gòu)定義,主要作用如下:
- 定義Index下字段名(Field Name)
- 定義字段的類型,比如數(shù)值型,字符串型、布爾型等
- 定義倒排索引的相關(guān)配置,比如是否索引、記錄postion等
需要注意的是,在索引中定義太多字段可能會(huì)導(dǎo)致索引膨脹,出現(xiàn)內(nèi)存不足和難以恢復(fù)的情況,下面有幾個(gè)設(shè)置:
- index.mapping.total_fields.limit:一個(gè)索引中能定義的字段的最大數(shù)量,默認(rèn)是 1000
- index.mapping.depth.limit:字段的最大深度,以內(nèi)部對(duì)象的數(shù)量來(lái)計(jì)算,默認(rèn)是20
- index.mapping.nested_fields.limit:索引中嵌套字段的最大數(shù)量,默認(rèn)是50
數(shù)據(jù)類型
核心數(shù)據(jù)類型
- 字符串 - text
- 用于全文索引,該類型的字段將通過(guò)分詞器進(jìn)行分詞,最終用于構(gòu)建索引
- 字符串 - keyword
- 不分詞,只能搜索該字段的完整的值,只用于 filtering
- 數(shù)值型
- long:有符號(hào)64-bit integer:-2^63 ~ 2^63 - 1
- integer:有符號(hào)32-bit integer,-2^31 ~ 2^31 - 1
- short:有符號(hào)16-bit integer,-32768 ~ 32767
- byte: 有符號(hào)8-bit integer,-128 ~ 127
- double:64-bit IEEE 754 浮點(diǎn)數(shù)
- float:32-bit IEEE 754 浮點(diǎn)數(shù)
- half_float:16-bit IEEE 754 浮點(diǎn)數(shù)
- scaled_float
- 布爾 - boolean
- 值:false, "false", true, "true"
- 日期 - date
- 由于Json沒(méi)有date類型,所以es通過(guò)識(shí)別字符串是否符合format定義的格式來(lái)判斷是否為date類型
- format默認(rèn)為:
strict_date_optional_time||epoch_millisformat
- 二進(jìn)制 - binary
- 該類型的字段把值當(dāng)做經(jīng)過(guò) base64 編碼的字符串,默認(rèn)不存儲(chǔ),且不可搜索
- 范圍類型
- 范圍類型表示值是一個(gè)范圍,而不是一個(gè)具體的值
- 譬如 age 的類型是 integer_range,那么值可以是 {"gte" : 10, "lte" : 20};搜索 "term" : {"age": 15} 可以搜索該值;搜索 "range": {"age": {"gte":11, "lte": 15}} 也可以搜索到
- range參數(shù) relation 設(shè)置匹配模式
- INTERSECTS :默認(rèn)的匹配模式,只要搜索值與字段值有交集即可匹配到
- WITHIN:字段值需要完全包含在搜索值之內(nèi),也就是字段值是搜索值的子集才能匹配
- CONTAINS:與WITHIN相反,只搜索字段值包含搜索值的文檔
- integer_range
- float_range
- long_range
- double_range
- date_range:64-bit 無(wú)符號(hào)整數(shù),時(shí)間戳(單位:毫秒)
- ip_range:IPV4 或 IPV6 格式的字符串
# 創(chuàng)建range索引
PUT range_index
{
"mappings": {
"_doc": {
"properties": {
"expected_attendees": {
"type": "integer_range"
},
"time_frame": {
"type": "date_range",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
# 插入一個(gè)文檔
PUT range_index/_doc/1
{
"expected_attendees" : {
"gte" : 10,
"lte" : 20
},
"time_frame" : {
"gte" : "2015-10-31 12:00:00",
"lte" : "2015-11-05"
}
}
# 12在 10~20的范圍內(nèi),可以搜索到文檔1
GET range_index/_search
{
"query" : {
"term" : {
"expected_attendees" : {
"value": 12
}
}
}
}
# within可以搜索到文檔
# 可以修改日期,然后分別對(duì)比CONTAINS,WITHIN,INTERSECTS的區(qū)別
GET range_index/_search
{
"query" : {
"range" : {
"time_frame" : {
"gte" : "2015-11-02",
"lte" : "2015-11-03",
"relation" : "within"
}
}
}
}
復(fù)雜數(shù)據(jù)類型
- 數(shù)組類型 Array
- 字符串?dāng)?shù)組 [ "one", "two" ]
- 整數(shù)數(shù)組 [ 1, 2 ]
- 數(shù)組的數(shù)組 [ 1, [ 2, 3 ]],相當(dāng)于 [ 1, 2, 3 ]
- Object對(duì)象數(shù)組 [ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 }]
- 同一個(gè)數(shù)組只能存同類型的數(shù)據(jù),不能混存,譬如 [ 10, "some string" ] 是錯(cuò)誤的
- 數(shù)組中的 null 值將被 null_value 屬性設(shè)置的值代替或者被忽略
- 空數(shù)組 [] 被當(dāng)做 missing field 處理
- 對(duì)象類型 Object
- 對(duì)象類型可能有內(nèi)部對(duì)象
- 被索引的形式為:manager.name.first
# tags字符串?dāng)?shù)組,lists 對(duì)象數(shù)組
PUT my_index/_doc/1
{
"message": "some arrays in this document...",
"tags": [ "elasticsearch", "wow" ],
"lists": [
{
"name": "prog_list",
"description": "programming list"
},
{
"name": "cool_list",
"description": "cool stuff list"
}
]
}
- 嵌套類型 Nested
- nested 類型是一種對(duì)象類型的特殊版本,它允許索引對(duì)象數(shù)組,獨(dú)立地索引每個(gè)對(duì)象
嵌套類型與Object類型的區(qū)別
通過(guò)例子來(lái)說(shuō)明:
- 插入一個(gè)文檔,不設(shè)置mapping,此時(shí) user 字段被自動(dòng)識(shí)別為對(duì)象數(shù)組
DELETE my_index
PUT my_index/_doc/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
- 查詢 user.first為 Alice,user.last 為 Smith的文檔,理想中應(yīng)該找不到匹配的文檔
- 結(jié)果是查到了文檔1,為什么呢?
GET my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "Smith" }}
]
}
}
}
- 是由于Object對(duì)象類型在內(nèi)部被轉(zhuǎn)化成如下格式的文檔:
{
"group" : "fans",
"user.first" : [ "alice", "john" ],
"user.last" : [ "smith", "white" ]
}
user.first 和 user.last 扁平化為多值字段,alice 和 white 的關(guān)聯(lián)關(guān)系丟失了。導(dǎo)致這個(gè)文檔錯(cuò)誤地匹配對(duì) alice 和 smith 的查詢
如果最開(kāi)始就把user設(shè)置為 nested 嵌套對(duì)象呢?
DELETE my_index
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"user": {
"type": "nested"
}
}
}
}
}
PUT my_index/_doc/1
{
"group": "fans",
"user": [
{
"first": "John",
"last": "Smith"
},
{
"first": "Alice",
"last": "White"
}
]
}
- 再來(lái)進(jìn)行查詢,可以發(fā)現(xiàn)以下第一個(gè)查不到文檔,第二個(gè)查詢到文檔1,符合我們預(yù)期
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "Smith" }}
]
}
}
}
}
}
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "White" }}
]
}
},
"inner_hits": {
"highlight": {
"fields": {
"user.first": {}
}
}
}
}
}
}
nested對(duì)象將數(shù)組中每個(gè)對(duì)象作為獨(dú)立隱藏文檔來(lái)索引,這意味著每個(gè)嵌套對(duì)象都可以獨(dú)立被搜索
需要注意的是:
- 使用 nested 查詢來(lái)搜索
- 使用 nested 和 reverse_nested 聚合來(lái)分析
- 使用 nested sorting 來(lái)排序
- 使用 nested inner hits 來(lái)檢索和高亮
地理位置數(shù)據(jù)類型
- geo_point
- 地理位置,其值可以有如下四中表現(xiàn)形式:
- object對(duì)象:"location": {"lat": 41.12, "lon": -71.34}
- 字符串:"location": "41.12,-71.34"
- geohash:"location": "drm3btev3e86"
- 數(shù)組:"location": [ -71.34, 41.12 ]
- 查詢的時(shí)候通過(guò) Geo Bounding Box Query 進(jìn)行查詢
- 地理位置,其值可以有如下四中表現(xiàn)形式:
- geo_shape
專用數(shù)據(jù)類型
- 記錄IP地址 ip
- 實(shí)現(xiàn)自動(dòng)補(bǔ)全 completion
- 記錄分詞數(shù) token_count
- 記錄字符串hash值 murmur3
- Percolator
# ip類型,存儲(chǔ)IP
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"ip_addr": {
"type": "ip"
}
}
}
}
}
PUT my_index/_doc/1
{
"ip_addr": "192.168.1.1"
}
GET my_index/_search
{
"query": {
"term": {
"ip_addr": "192.168.0.0/16"
}
}
}
多字段特性 multi-fields
- 允許對(duì)同一個(gè)字段采用不同的配置,比如分詞,常見(jiàn)例子如對(duì)人名實(shí)現(xiàn)拼音搜索,只需要在人名中新增一個(gè)子字段為 pinyin 即可
- 通過(guò)參數(shù) fields 設(shè)置
設(shè)置Mapping

GET my_index/_mapping
# 結(jié)果
{
"my_index": {
"mappings": {
"doc": {
"properties": {
"age": {
"type": "integer"
},
"created": {
"type": "date"
},
"name": {
"type": "text"
},
"title": {
"type": "text"
}
}
}
}
}
}
Mapping參數(shù)
analyzer
- 分詞器,默認(rèn)為standard analyzer,當(dāng)該字段被索引和搜索時(shí)對(duì)字段進(jìn)行分詞處理
boost
- 字段權(quán)重,默認(rèn)為1.0
dynamic
- Mapping中的字段類型一旦設(shè)定后,禁止直接修改,原因是:Lucene實(shí)現(xiàn)的倒排索引生成后不允許修改
- 只能新建一個(gè)索引,然后reindex數(shù)據(jù)
- 默認(rèn)允許新增字段
- 通過(guò)dynamic參數(shù)來(lái)控制字段的新增:
- true(默認(rèn))允許自動(dòng)新增字段
- false 不允許自動(dòng)新增字段,但是文檔可以正常寫(xiě)入,但無(wú)法對(duì)新增字段進(jìn)行查詢等操作
- strict 文檔不能寫(xiě)入,報(bào)錯(cuò)
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": false,
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
},
"social_networks": {
"dynamic": true,
"properties": {}
}
}
}
}
}
}
}
定義后my_index這個(gè)索引下不能自動(dòng)新增字段,但是在user.social_networks下可以自動(dòng)新增子字段
copy_to
- 將該字段復(fù)制到目標(biāo)字段,實(shí)現(xiàn)類似_all的作用
- 不會(huì)出現(xiàn)在_source中,只用來(lái)搜索
DELETE my_index
PUT my_index
{
"mappings": {
"doc": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
}
PUT my_index/doc/1
{
"first_name": "John",
"last_name": "Smith"
}
GET my_index/_search
{
"query": {
"match": {
"full_name": {
"query": "John Smith",
"operator": "and"
}
}
}
}
index
- 控制當(dāng)前字段是否索引,默認(rèn)為true,即記錄索引,false不記錄,即不可搜索
index_options
- index_options參數(shù)控制將哪些信息添加到倒排索引,以用于搜索和突出顯示,可選的值有:docs,freqs,positions,offsets
- docs:只索引 doc id
- freqs:索引 doc id 和詞頻,平分時(shí)可能要用到詞頻
- positions:索引 doc id、詞頻、位置,做 proximity or phrase queries 時(shí)可能要用到位置信息
- offsets:索引doc id、詞頻、位置、開(kāi)始偏移和結(jié)束偏移,高亮功能需要用到offsets
fielddata
- 是否預(yù)加載 fielddata,默認(rèn)為false
- Elasticsearch第一次查詢時(shí)完整加載這個(gè)字段所有 Segment 中的倒排索引到內(nèi)存中
- 如果我們有一些 5 GB 的索引段,并希望加載 10 GB 的 fielddata 到內(nèi)存中,這個(gè)過(guò)程可能會(huì)要數(shù)十秒
- 將 fielddate 設(shè)置為 true ,將載入 fielddata 的代價(jià)轉(zhuǎn)移到索引刷新的時(shí)候,而不是查詢時(shí),從而大大提高了搜索體驗(yàn)
- 參考:預(yù)加載 fielddata
eager_global_ordinals
- 是否預(yù)構(gòu)建全局序號(hào),默認(rèn)false
- 參考:預(yù)構(gòu)建全局序號(hào)(Eager global ordinals)
doc_values
fields
- 該參數(shù)的目的是為了實(shí)現(xiàn) multi-fields
- 一個(gè)字段,多種數(shù)據(jù)類型
- 譬如:一個(gè)字段 city 的數(shù)據(jù)類型為 text ,用于全文索引,可以通過(guò) fields 為該字段定義 keyword 類型,用于排序和聚合
# 設(shè)置 mapping
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"city": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
}
# 插入兩條數(shù)據(jù)
PUT my_index/_doc/1
{
"city": "New York"
}
PUT my_index/_doc/2
{
"city": "York"
}
# 查詢,city用于全文索引 match,city.raw用于排序和聚合
GET my_index/_search
{
"query": {
"match": {
"city": "york"
}
},
"sort": {
"city.raw": "asc"
},
"aggs": {
"Cities": {
"terms": {
"field": "city.raw"
}
}
}
}
format
- 由于JSON沒(méi)有date類型,Elasticsearch預(yù)先通過(guò)format參數(shù)定義時(shí)間格式,將匹配的字符串識(shí)別為date類型,轉(zhuǎn)換為時(shí)間戳(單位:毫秒)
- format默認(rèn)為:
strict_date_optional_time||epoch_millis - Elasticsearch內(nèi)建的時(shí)間格式:
| 名稱 | 格式 |
|---|---|
| epoch_millis | 時(shí)間戳(單位:毫秒) |
| epoch_second | 時(shí)間戳(單位:秒) |
| date_optional_time | |
| basic_date | yyyyMMdd |
| basic_date_time | yyyyMMdd'T'HHmmss.SSSZ |
| basic_date_time_no_millis | yyyyMMdd'T'HHmmssZ |
| basic_ordinal_date | yyyyDDD |
| basic_ordinal_date_time | yyyyDDD'T'HHmmss.SSSZ |
| basic_ordinal_date_time_no_millis | yyyyDDD'T'HHmmssZ |
| basic_time | HHmmss.SSSZ |
| basic_time_no_millis | HHmmssZ |
| basic_t_time | 'T'HHmmss.SSSZ |
| basic_t_time_no_millis | 'T'HHmmssZ |
- 上述名稱加前綴
strict_表示為嚴(yán)格格式 - 更多的查看文檔
properties
- 用于_doc,object和nested類型的字段定義子字段
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"manager": {
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
},
"employees": {
"type": "nested",
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
}
}
}
}
}
PUT my_index/_doc/1
{
"region": "US",
"manager": {
"name": "Alice White",
"age": 30
},
"employees": [
{
"name": "John Smith",
"age": 34
},
{
"name": "Peter Brown",
"age": 26
}
]
}
normalizer
與 analyzer 類似,只不過(guò) analyzer 用于 text 類型字段,分詞產(chǎn)生多個(gè) token,而 normalizer 用于 keyword 類型,只產(chǎn)生一個(gè) token(整個(gè)字段的值作為一個(gè)token,而不是分詞拆分為多個(gè)token)
定義一個(gè)自定義 normalizer,使用大寫(xiě)uppercase過(guò)濾器
PUT test_index_4
{
"settings": {
"analysis": {
"normalizer": {
"my_normalizer": {
"type": "custom",
"char_filter": [],
"filter": ["uppercase", "asciifolding"]
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"foo": {
"type": "keyword",
"normalizer": "my_normalizer"
}
}
}
}
}
# 插入數(shù)據(jù)
POST test_index_4/_doc/1
{
"foo": "hello world"
}
POST test_index_4/_doc/2
{
"foo": "Hello World"
}
POST test_index_4/_doc/3
{
"foo": "hello elasticsearch"
}
# 搜索hello,結(jié)果為空,而不是3條??!
GET test_index_4/_search
{
"query": {
"match": {
"foo": "hello"
}
}
}
# 搜索 hello world,結(jié)果2條,1 和 2
GET test_index_4/_search
{
"query": {
"match": {
"foo": "hello world"
}
}
}
其他字段
- coerce
- 強(qiáng)制類型轉(zhuǎn)換,把json中的值轉(zhuǎn)為ES中字段的數(shù)據(jù)類型,譬如:把字符串"5"轉(zhuǎn)為integer的5
- coerce默認(rèn)為 true
- 如果coerce設(shè)置為 false,當(dāng)json的值與es字段類型不匹配將會(huì) rejected
- 通過(guò) "settings": { "index.mapping.coerce": false } 設(shè)置索引的 coerce
- enabled
- 是否索引,默認(rèn)為 true
- 可以在_doc和字段兩個(gè)粒度進(jìn)行設(shè)置
- ignore_above
- 設(shè)置能被索引的字段的長(zhǎng)度
- 超過(guò)這個(gè)長(zhǎng)度,該字段將不被索引,所以無(wú)法搜索,但聚合的terms可以看到
- null_value
- 該字段定義遇到null值時(shí)的處理策略,默認(rèn)為Null,即空值,此時(shí)ES會(huì)忽略該值
- 通過(guò)設(shè)定該值可以設(shè)定字段為 null 時(shí)的默認(rèn)值
- ignore_malformed
- 當(dāng)數(shù)據(jù)類型不匹配且 coerce 強(qiáng)制轉(zhuǎn)換時(shí),默認(rèn)情況會(huì)拋出異常,并拒絕整個(gè)文檔的插入
- 若設(shè)置該參數(shù)為 true,則忽略該異常,并強(qiáng)制賦值,但是不會(huì)被索引,其他字段則照常
- norms
- norms 存儲(chǔ)各種標(biāo)準(zhǔn)化因子,為后續(xù)查詢計(jì)算文檔對(duì)該查詢的匹配分?jǐn)?shù)提供依據(jù)
- norms 參數(shù)對(duì)評(píng)分很有用,但需要占用大量的磁盤(pán)空間
- 如果不需要計(jì)算字段的評(píng)分,可以取消該字段 norms 的功能
- position_increment_gap
- 與 proximity queries(近似查詢)和 phrase queries(短語(yǔ)查詢)有關(guān)
- 默認(rèn)值 100
- search_analyzer
- 搜索分詞器,查詢時(shí)使用
- 默認(rèn)與 analyzer 一樣
- similarity
- 設(shè)置相關(guān)度算法,ES5.x 和 ES6.x 默認(rèn)的算法為 BM25
- 另外也可選擇 classic 和 boolean
- store
- store 的意思是:是否在 _source 之外在獨(dú)立存儲(chǔ)一份,默認(rèn)值為 false
- es在存儲(chǔ)數(shù)據(jù)的時(shí)候把json對(duì)象存儲(chǔ)到"_source"字段里,"_source"把所有字段保存為一份文檔存儲(chǔ)(讀取需要1次IO),要取出某個(gè)字段則通過(guò) source filtering 過(guò)濾
- 當(dāng)字段比較多或者內(nèi)容比較多,并且不需要取出所有字段的時(shí)候,可以把特定字段的store設(shè)置為true單獨(dú)存儲(chǔ)(讀取需要1次IO),同時(shí)在_source設(shè)置exclude
- 關(guān)于該字段的理解,參考: es設(shè)置mapping store屬性
- term_vector
- 與倒排索引相關(guān)
Dynamic Mapping
ES是依靠JSON文檔的字段類型來(lái)實(shí)現(xiàn)自動(dòng)識(shí)別字段類型,支持的類型如下:
| JSON 類型 | ES 類型 |
|---|---|
| null | 忽略 |
| boolean | boolean |
| 浮點(diǎn)類型 | float |
| 整數(shù) | long |
| object | object |
| array | 由第一個(gè)非 null 值的類型決定 |
| string | 匹配為日期則設(shè)為date類型(默認(rèn)開(kāi)啟); 匹配為數(shù)字則設(shè)置為 float或long類型(默認(rèn)關(guān)閉); 設(shè)為text類型,并附帶keyword的子字段 |
舉栗子
POST my_index/doc
{
"username":"whirly",
"age":22,
"birthday":"1995-01-01"
}
GET my_index/_mapping
# 結(jié)果
{
"my_index": {
"mappings": {
"doc": {
"properties": {
"age": {
"type": "long"
},
"birthday": {
"type": "date"
},
"username": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
日期的自動(dòng)識(shí)別
- dynamic_date_formats 參數(shù)為自動(dòng)識(shí)別的日期格式,默認(rèn)為 [ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
- date_detection可以關(guān)閉日期自動(dòng)識(shí)別機(jī)制
# 自定義日期識(shí)別格式
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
}
# 關(guān)閉日期自動(dòng)識(shí)別機(jī)制
PUT my_index
{
"mappings": {
"_doc": {
"date_detection": false
}
}
}
數(shù)字的自動(dòng)識(shí)別
- 字符串是數(shù)字時(shí),默認(rèn)不會(huì)自動(dòng)識(shí)別為整形,因?yàn)樽址谐霈F(xiàn)數(shù)字完全是合理的
- numeric_detection 參數(shù)可以開(kāi)啟字符串中數(shù)字的自動(dòng)識(shí)別
Dynamic templates
允許根據(jù)ES自動(dòng)識(shí)別的數(shù)據(jù)類型、字段名等來(lái)動(dòng)態(tài)設(shè)定字段類型,可以實(shí)現(xiàn)如下效果:
- 所有字符串類型都設(shè)定為keyword類型,即不分詞
- 所有以message開(kāi)頭的字段都設(shè)定為text類型,即分詞
- 所有以long_開(kāi)頭的字段都設(shè)定為long類型
- 所有自動(dòng)匹配為double類型的都設(shè)定為float類型,以節(jié)省空間
Dynamic templates API
"dynamic_templates": [
{
"my_template_name": {
... match conditions ...
"mapping": { ... }
}
},
...
]
匹配規(guī)則一般有如下幾個(gè)參數(shù):
- match_mapping_type 匹配ES自動(dòng)識(shí)別的字段類型,如boolean,long,string等
- match, unmatch 匹配字段名
- match_pattern 匹配正則表達(dá)式
- path_match, path_unmatch 匹配路徑
# double類型的字段設(shè)定為float以節(jié)省空間
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "double",
"mapping": {
"type": "float"
}
}
}
]
}
}
}
自定義Mapping的建議
- 寫(xiě)入一條文檔到ES的臨時(shí)索引中,獲取ES自動(dòng)生成的Mapping
- 修改步驟1得到的Mapping,自定義相關(guān)配置
- 使用步驟2的Mapping創(chuàng)建實(shí)際所需索引
Index Template 索引模板
- 索引模板,主要用于在新建索引時(shí)自動(dòng)應(yīng)用預(yù)先設(shè)定的配置,簡(jiǎn)化索引創(chuàng)建的操作步驟
- 可以設(shè)定索引的setting和mapping
- 可以有多個(gè)模板,根據(jù)order設(shè)置,order大的覆蓋小的配置
- 索引模板API,endpoint為 _template
# 創(chuàng)建索引模板,匹配 test-index-map 開(kāi)頭的索引
PUT _template/template_1
{
"index_patterns": ["test-index-map*"],
"order": 2,
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "YYYY/MM/dd HH:mm:ss"
}
}
}
}
}
# 插入一個(gè)文檔
POST test-index-map_1/doc
{
"name" : "小旋鋒",
"created_at": "2018/08/16 20:11:11"
}
# 獲取該索引的信息,可以發(fā)現(xiàn) settings 和 mappings 和索引模板里設(shè)置的一樣
GET test-index-map_1
# 刪除
DELETE /_template/template_1
# 查詢
GET /_template/template_1
更多內(nèi)容請(qǐng)?jiān)L問(wèn)我的個(gè)人網(wǎng)站: http://laijianfeng.org
參考文檔:
- elasticsearch 官方文檔
- 慕課網(wǎng) Elastic Stack從入門(mén)到實(shí)踐