API容許你向索引(index)添加文檔類型(type),或者向文檔類型(type)中添加字段(field)。app
PUT secisland { "mappings": { "log": { "properties": { "message": { "type": "string" } } } } }
添加索引名爲secisland,文檔類型爲log,其中包含字段message,字段類型是字符串。curl
PUT secisland/_mapping/user
url
{ "properties": { "name": { "type": "string" } } }
向已經存在的索引secisland添加文檔類型爲user,包含字段name,字段類型是字符串。spa
PUT secisland/_mapping/log
code
{ "properties": { "user_name": { "type": "string" } } }
已經存在的索引secisland,文檔類型爲log,添加新的字段user_name,字段類型是字符串。
對象
多個索引設置映射,能夠一次向多個索引添加文檔類型索引
PUT /{index}/_mapping/{type} { body }
{index}能夠有多種方式,逗號分隔,好比test1,test2,test3接口
_all,表示全部索引,通配符,*表示全部,test*表示以test開頭ci
{type}須要添加或更新的文檔類型文檔
{body}須要添加的字段或字段類型
在通常狀況下,對現有字段的映射不會更新。對這個規則有一些例外。例如:
新的屬性被添加到對象數據類型的字段。
新的多域字段被添加到現有的字段。
doc_values能夠被禁用。
增長了ignore_above參數。
例如:
請求:PUT my_index
參數:
{ "mappings": { "user": { "properties": { "name": { "properties": { "first": { "type": "string" } } }, "user_id": { "type": "string", "index": "not_analyzed" } } } } }
user的第一個name屬性是對象數據類型(Object datatype)字段,對上個索引進行修改:
請求:PUT my_index/_mapping/user
參數:
{ "properties": { "name": { "properties": { "last": { "type": "string" } } }, "user_id": { "type": "string", "index": "not_analyzed", "ignore_above": 100 } } }
修改映射,對第一個對象數據類型增長了一個熟悉是last。修改了user_id, 經過設置ignore_above使默認的更新爲0。
在同一個索引的不一樣類型(type)中,相同名稱的字段中必須有相同的映射,由於他們內部是在同一個領域內,若是試圖在這種狀況下更新映射參數,系統將會拋出異常。除非在更新的時候指定 update_all_types參數。在這種狀況下它將更新在相同的指標參數在全部同名的字段。
例如:
請求:PUT my_index
參數:
{ "mappings": { "type_one": { "properties": { "text": { "type": "string", "analyzer": "standard" } } }, "type_two": { "properties": { "text": { "type": "string", "analyzer": "standard" } } } } }
修改映射
請求:PUT my_index/_mapping/type_one
參數:
{ "properties": { "text": { "type": "string", "analyzer": "standard", "search_analyzer": "whitespace" } } }
這個時候會拋出異常,而後增長參數,update_all_types,這個時候會同時更新兩個類型。
請求:PUT my_index/_mapping/type_one?update_all_types
{ "properties": { "text": { "type": "string", "analyzer": "standard", "search_analyzer": "whitespace" } } }
獲取文檔映射接口容許經過索引或者索引和類型來檢索。
curl -XGET 'http://localhost:9200/twitter/_mapping/tweet'
系統同時支持獲取多個索引和類型的語法:
獲取文檔映射接口一次能夠獲取多個索引或文檔映射類型。該接口一般是以下格式:
host:port/{index}/_mapping/{type},{index}和{type}能夠接受逗號(,)分隔符,也可使用_all來表示所有索引。以下所示:
curl -XGET 'http://localhost:9200/_mapping/twitter,kimchy'
curl -XGET 'http://localhost:9200/_all/_mapping/tweet,book'
第一個省略_all,第二個使用_all都是表示所有索引。也就是說,下面兩個是等價的:
curl -XGET 'http://localhost:9200/_all/_mapping'
curl -XGET 'http://localhost:9200/_mapping'
獲取文檔字段接口容許你檢索一個或多個字段。這個用來檢索想要檢索的字段,而不是某個索引或者文檔類型的所有內容。
這段請求只返回字段爲text的內容:
curl -XGET 'http://localhost:9200/twitter/_mapping/tweet/field/text'
響應結果以下(假定text爲String類型)
{ "twitter": { "tweet": { "text": { "full_name": "text", "mapping": { "text": { "type": "string" } } } } } }
獲取多索引和類型的字段映射。
獲取文檔字段映射接口一次能夠獲取多個索引或文檔映射類型。該接口一般是以下格式:host:port/{index}/{type}/_mapping/field/{field}
{index},{type},{field}可使用逗號(,)分隔,也可使用*做爲通配符{type},{field}可使用逗號(,)分隔。
其中{index}可使用_all表示所有索引,示例以下:
curl -XGET 'http://localhost:9200/twitter,kimchy/_mapping/field/message'
curl -XGET 'http://localhost:9200/_all/_mapping/tweet,book/field/message,user.id'
curl -XGET 'http://localhost:9200/_all/_mapping/tw*/field/*.id'
指定字段
獲取文檔字段接口,可使用逗號(,)分隔符或者通配符(*)。
以下文檔示例,若是隻使用字段名id會產生歧義。
{ "article": { "properties": { "id": { "type": "string" }, "title": { "type": "string"}, "abstract": { "type": "string"}, "author": { "properties": { "id": { "type": "string" }, "name": { "type": "string" } } } } } }
若是想要表示author中的id,name,使用author.id,author.name。請求以下:
curl -XGET "http://localhost:9200/publications/_mapping/article/field/
author.id,abstract,author.name"
返回結果以下:
{ "publications": { "article": { "abstract": { "full_name": "abstract", "mapping": { "abstract": { "type": "string" } } }, "author.id": { "full_name": "author.id", "mapping": { "id": { "type": "string" } } }, "author.name": { "full_name": "author.name", "mapping": { "name": { "type": "string" } } } } } }
檢查索引或文檔類型是否存在
curl -XHEAD -i 'http://localhost:9200/twitter/tweet'
存在返回200,不存在返回404。
賽克藍德(secisland)後續會逐步對Elasticsearch的最新版本的各項功能進行分析,近請期待。也歡迎加入secisland公衆號進行關注。