Meta-Fields
每個文檔都有與之關聯的元字段,例如_index、_type和 _id 元字段。 創建映射類型時,可以自定義其中一些元字段的行爲。
Identity meta-fields(文檔標示元字段)
_index
文檔所屬的索引。
多索引查詢時,有時候只需要在特地索引名上進行查詢,_index字段提供了便利,也就是說可以對索引名進行term查詢、terms查詢、聚合分析、使用腳本和排序。
_index是一個虛擬字段,不會真的加到Lucene索引中,對_index進行term、terms查詢(也包括match、query_string、simple_query_string),但是不支持prefix、wildcard、regexp和fuzzy查詢。
_uid
_type 和_id 的組合
在elasticsearch6及之後棄用。現在,_type類型已被刪除,文檔由_id唯一標識,_uid字段僅作爲查看_id字段以保持向後兼容。
_type
文檔的匹配類型。
在elasticsearch6及之後棄用。
索引中的每一份文檔都和一個 _type和_id唯一關聯。 _type 字段本身的設計目標是爲了索引更快查詢。可被查詢,聚合,排序使用,或者腳本里使用。
_id
文檔的id.
每一個文檔都有一個獨一無二的 _id 標示它, 它被索引存儲一遍我們在調用GET API 或者查詢ids時能夠更快的點位文檔。
在elasticsearch6之前因爲支持多類型的存在,所以我們在一個索引中唯一標示文檔需要同時用到 _type 和 _id,或者用_uid來作爲唯一主鍵,elasticsearch6 及之後_id 就是索引的唯一主鍵。
Document source meta-fields(原文檔元字段)
_source
文檔的原生json字符串文檔內容。
_source字段包含在索引時間傳遞的原始JSON文檔正文。 _source字段本身沒有編入索引(因此不可搜索),但它被存儲,以便在執行獲取請求(如get或search)時可以返回它。
默認_source字段是開啓的,也就是說,默認情況下存儲文檔的原始值。
禁用_source
如果某個字段內容非常多(比如一篇小說),或者查詢業務只需要對該字段進行搜索,返回文檔id,然後通過其他途徑查看文檔原文,則不需要保留_source元字段。可以通過禁用_source元字段,在ElasticSearch 中只存儲倒排索引,不保留字段原始值。
PUT tweets
{
"mappings": {
"_doc": {
"_source": {
"enabled": false
}
}
}
}
包含或者去除 _source 元字段裏面的字段
在文檔存儲之前,我們可以先定義在索引_source存儲哪些信息不存儲哪些信息
定義映射
PUT logs
{
"mappings": {
"_doc": {
"_source": {
"includes": [
"*.count",
"meta.*"
],
"excludes": [
"meta.description",
"meta.other.*"
]
}
}
}
}
插入內容
PUT logs/_doc/1
{
"requests": {
"count": 10,
"foo": "bar"
},
"meta": {
"name": "Some metric",
"description": "Some metric description",
"other": {
"foo": "one",
"baz": "two"
}
}
}
GET logs/_search
{
"query": {
"match": {
"meta.other.foo": "one"
}
}
}
查詢結果如下:
{
"took": 58,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "logs",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"meta": {
"other": {},
"name": "Some metric"
},
"requests": {
"count": 10
}
}
}
]
}
}
_size
mapper-size 插件提供的計算整個_source字段的字節數大小 。
Indexing meta-fields(索引元字段)
_all
_all字段是把其它字段拼接在一起的超級字段,所有的字段用空格分開,_all字段會被解析和索引,但是不存儲。當你只想返回包含某個關鍵字的文檔但是不明確地搜某個字段的時候就需要使用_all字段。默認是關閉的。
在elasticsearch6及之後棄用。
PUT /my_index
{
"mapping": {
"user": {
"_all": {
"enabled": true
}
}
}
}
PUT /my_index/user/1
{
"first_name": "John",
"last_name": "Smith",
"date_of_birth": "1970-10-24"
}
GET /my_index/_search
{
"query": {
"match": {
"_all": "john smith 1970"
}
}
}
上面的例子中_all 將包含這些索引詞:[ "john", "smith", "1970", "10", "24" ]
,索引次的組合順序依賴於分詞器,_all已棄用。
_field_names
_field_names字段包含索引文檔中字段的取值除null以外的任何值每個字段的名稱。 一般使用此字段來查找對於特定字段具有或不具有任何非空值的文檔。
目前,一般來講_field_names字段只包含具有doc_values並且默認關閉屬性的字段名稱。
禁用 _field_names
PUT tweets
{
"mappings": {
"_doc": {
"_field_names": {
"enabled": false
}
}
}
}
_ignored
elasticsearch 6.4之後添加元字段
_ignored 元字段索引和存儲文檔中由於ignore_malformed屬性被ignored 的每個字段的名稱。
Routing meta-field(路由元字段)
_routing
索引中的文檔存儲在特定的分片使用下面的公式決定:
shard_num = hash(_routing) % num_primary_shards
默認是用的 文檔_id元字段的值來路由。
自定義路由模式可以通過指定每個文檔的自定義路由值來實現。例如:
PUT my_index/_doc/1?routing=user1&refresh=true
{
"title": "This is a document"
}
GET my_index/_doc/1?routing=user1
查詢結果
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_routing": "user1",
"found": true,
"_source": {
"title": "This is a document"
}
}
請求體查詢方式如下:
GET my_index/_search
{
"query": {
"terms": {
"_routing": [ "user1" ]
}
}
}
其他meta-field (自定義元字段)
elasticsearch 也支持自定義元數據。