1. 前言
每個文檔都有與之關(guān)聯(lián)的元數(shù)據(jù)字段(Metadata fields)玫恳,比如_index、_type和_id元數(shù)據(jù)字段挑势。在創(chuàng)建Mapping時淤齐,可以自定義其中一些元數(shù)據(jù)字段的行為。接下來具體介紹一些重要的元數(shù)據(jù)字段涣狗。
元數(shù)據(jù)字段分類
2. 標(biāo)識元數(shù)據(jù)字段
_index
- _index:表示文檔屬于哪個索引谍婉。
- _index字段是虛擬公開的,即它不會作為一個真實字段添加到Lucene索引中镀钓,我們既可以在term查詢(或任何被重寫為term查詢的查詢穗熬,例如match、query_string或simple_query_string查詢)中使用_index字段丁溅,也可以在前綴和通配符查詢中使用唤蔗。但是,它不支持regexp和fuzzy查詢。_index字段也支持在聚合妓柜、排序或腳本中被使用箱季。
- 字段使用示例如下:
PUT my_index_1/_doc/1
{"text":"Document in index 1"}
PUT my_index_2/_doc/2?refresh=true
{"text":"Document in index 2"}
# term查詢、聚合棍掐、排序及腳本中使用_index字段
GET my_index_1,my_index_2/_search
{
"query": {
"terms": {
"_index": [
"my_index_1",
"my_index_2"
]
}
},
"aggs": {
"indices": {
"terms": {
"field": "_index",
"size": 10
}
}
},
"sort": [
{
"_index": {
"order": "asc"
}
}
],
"script_fields": {
"index_name": {
"script": {
"lang": "painless",
"source": "doc['_index']"
}
}
}
}
_id
- _id:文檔的ID藏雏。
- 每個文檔都有一個唯一標(biāo)識它的_id,_id的大小被限制為512字節(jié) 作煌,_id字段會被索引诉稍,并且支持在查詢中使用,在聚合和排序中可以訪問_id字段的值最疆,但不建議這樣做,因為它需要加載大量數(shù)據(jù)到內(nèi)存中蚤告。
- 字段使用示例如下:
PUT my_index_1/_doc/1
{"text":"Document with ID 1"}
PUT my_index_1/_doc/2?refresh=true
{"text":"Document with ID 2"}
# term查詢中使用_id字段
GET my_index_1/_search
{"query":{"terms":{"_id":["1","2"]}}}
3. 文檔源元數(shù)據(jù)字段
_source
- _source:表示文檔主體的原始JSON數(shù)據(jù)努酸。
- _source字段本身沒有索引(因此不能搜索),但是它會被存儲杜恰,以便在執(zhí)行fetch請求(如get或search)時返回获诈。
- 可禁用_source字段。雖然擁有_source有時非常方便心褐,但它確實會導(dǎo)致索引中的存儲開銷舔涎。為此,可將其禁用如下:
# 禁用_source字段
PUT my_index_1
{"mappings":{"_source":{"enabled":false}}}
- 可使用參數(shù)includes/excludes來包含或排除在_source字段中的內(nèi)容逗爹。參數(shù)includes/excludes使用如下:
# 創(chuàng)建索引my_index_1亡嫌,指定_source字段中參數(shù)includes/excludes
PUT my_index_1
{
"mappings": {
"_source": {
"includes": [
"*.count",
"meta.*"
],
"excludes": [
"meta.description",
"meta.other.*"
]
}
}
}
PUT my_index_1/_doc/1
{
"requests": {
"count": 10,
"foo": "bar"
},
"meta": {
"name": "Some metric",
"description": "Some metric description",
"other": {
"foo": "one",
"baz": "two"
}
}
}
# 我們可以通過meta.other.foo字段搜索并匹配到數(shù)據(jù),
# 即使這個字段內(nèi)容在_source中被排除掘而。
GET my_index_1/_search
{"query":{"match":{"meta.other.foo":"one"}}}
_size
- _size:_source字段的字節(jié)大小挟冠,由mapper-size插件提供。
4. 索引元數(shù)據(jù)字段
_field_names
- _field_names:文檔中非空值的所有字段袍睡。
- 在參數(shù)doc_values和參數(shù)norm禁用的情況下知染,exists查詢會使用_field_names字段來查找特定字段具有或不具有任何非空值的文檔,反之斑胜,exists查詢使用其他字段查找控淡。
- 可禁用_field_names字段(通常無此必要),禁用示例如下:
# 禁用_field_names字段
PUT my_index_1
{"mappings":{"_field_names":{"enabled":false}}}
_ignored
- _ignored:索引并存儲文檔中被忽略的每個字段的名稱止潘,這些字段是由于格式錯誤并且ignore_malform開啟而被忽略的掺炭。
- _ignored字段可通過term、terms和exists查詢進(jìn)行搜索覆山,并作為搜索結(jié)果的一部分返回竹伸。
- 字段使用示例如下:
PUT my_index_1
{
"mappings": {
"properties": {
"number_one": {
"type": "integer",
"ignore_malformed": true
},
"number_two": {
"type": "integer",
"ignore_malformed": true
}
}
}
}
# 由于字段number_one設(shè)置了忽略缺陷格式的數(shù)據(jù),保存成功
PUT my_index_1/_doc/1
{"text":"Some text value","number_one":"foo"}
# 由于字段number_two設(shè)置了ignore_malformed=false,保存失敗
PUT my_index_1/_doc/2
{"text":"Some text value","number_two":"foo"}
# 查詢已經(jīng)被忽略缺陷的數(shù)據(jù)
GET my_index_1/_search
{"query":{"exists":{"field":"_ignored"}}}
5. 路由元數(shù)據(jù)字段
_routing
- _routing:一個可自定義的路由值勋篓,用于將文檔路由到特定的分片(shard)吧享。
- _routing字段默認(rèn)值是所在文檔的_id, 當(dāng)前的路由公式:
shard_num = hash(_routing) % num_primary_shards
- 可修改文檔的_routing路由值,操作示例如下:
PUT my_index_1
{
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"title": {
"type" : "text"
}
}
}
}
# 文檔1使用user1替代id作為routing值
PUT my_index_1/_doc/1?routing=user1&refresh=true
{"title":"This is a document"}
# 在term query中使用_routing字段搜索
GET my_index_1/_search
{"query":{"terms":{"_routing":["user1"]}}}
- 可設(shè)置路由必須譬嚣,如果設(shè)置路由必須钢颂,那么所有文檔操作必須提供路由值,設(shè)置路由必須操作如下:
PUT my_index_1
{
"mappings": {
"_routing": {
"required": true
}
}
}
6. 其他元數(shù)據(jù)字段
_meta
- _meta:用于存儲應(yīng)用程序特定的元數(shù)據(jù)信息拜银。
- 通過_meta字段可以自定義與文檔相關(guān)的元數(shù)據(jù)殊鞭。但是這些信息不會被Elasticsearch使用到(即不支持查詢、聚合尼桶、排序等)操灿。自定義元數(shù)據(jù)操作如下:
# 自定義元信息class、version泵督,表示文檔所屬的class和適用的版本范圍
PUT my_index_1
{
"mappings": {
"_meta": {
"class": "MyApp::User",
"version": {
"min": "1.0",
"max": "1.3"
}
}
}
}