ES8-mapping元字段

1.元字段概述

官方解釋:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-fields.html#_document_source_meta_fieldshtml

mapping元字段是mapping映射中描述文檔自己的字段,大體能夠分爲文檔屬性元數據、文檔元數據、索引元數據、路由元數據和自定義元數據。app

2.主要字段解讀

_index

多索引查詢時,有時候只須要在特意索引名上進行查詢,_index字段提供了便利,也就是說能夠對索引名進行term查詢、terms查詢、聚合分析、使用腳本和排序。elasticsearch

_index是一個虛擬字段,不會真的加到Lucene索引中,對_index進行term、terms查詢(也包括match、query_string、simple_query_string),可是不支持prefix、wildcard、regexp和fuzzy查詢。ide

_type

在6.0.0中棄用,此doc的mapping type名, 自動被索引,可被查詢,聚合,排序使用,或者腳本里訪問ui

_id

doc的id,建索引時候傳入 ,不被索引, 可經過_uid被查詢,腳本里使用,不能參與聚合或排序this

PUT my_index

PUT my_index/my_type/1
{
  "text":"this is a doc"
}

PUT my_index/my_type/2
{
  "text": "Document with ID 2"
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_id": ["1","2"]
    }
  }
}

建立索引,添加文檔,經過_id查詢文檔url

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "2",
        "_score": 1,
        "_source": {
          "text": "Document with ID 2"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 1,
        "_source": {
          "text": "this is a doc"
        }
      }
    ]
  }
}

6.0以前的版本並非這樣的,由於它們支持多種類型,因此_type和_id被合併爲一個名爲_uid的複合主鍵。spa

_uid

在6.0.0中棄用。如今,類型已被刪除,文檔由_id惟一標識,_uid字段僅做爲查看_id字段以保持向後兼容。code

_source

_source字段包含在索引時傳遞的原始JSON文檔正文。 _source字段自己沒有編入索引(所以不可搜索),但它被存儲,以便在執行獲取請求(如get或search)時能夠返回它。 
默認_source字段是開啓的,也就是說,默認狀況下存儲文檔的原始值。regexp

若是某個字段內容很是多(好比一篇小說),或者查詢業務只須要對該字段進行搜索,返回文檔id,而後經過其餘途徑查看文檔原文,則不須要保留_source元字段。能夠經過禁用_source元字段,在ElasticSearch 中只存儲倒排索引,不保留字段原始值。 

_source禁用

DELETE my_index

PUT my_index
{
  "mappings": {
    "my_type":{
      "_source": {"enabled": false}
    }
  }
}

PUT my_index/my_type/1
{
  "text":"this is a doc"
}

經過id查詢文檔

GET my_index/my_type/1

結果中並無_source字段內容

{
  "_index": "my_index",
  "_type": "my_type",
  "_id": "1",
  "_version": 1,
  "found": true
}

_source包含或者排除字段

DELETE my_index

PUT my_index
{
  "mappings": {
    "blog": {
      "_source": {
        "includes": [ "title", "url" ],
        "excludes": [ "content" ]
      },
      "properties": {
        "title": {
          "type": "text"
        },
        "content": {
          "type": "text"
        },
        "url": {
          "type": "text"
        }
      }
    }
  }
}

定義my_index索引blog文檔結構包含三個屬性:title、content、url。設置_source屬性包含title和url不包含content。

PUT my_index/blog/1
{
  "title":"百度搜索",
  "content":"搜索查詢的內容有哪些",
  "url":"http://www.baidu.com"
}

GET my_index/blog/1

查詢結果只能看到title和url兩個字段

{
  "_index": "my_index",
  "_type": "blog",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "title": "百度搜索",
    "url": "http://www.baidu.com"
  }
}

_field_names

_field_names字段索引文檔中每一個字段的名稱,其中包含除null之外的任何值。

 

_routing

使用如下公式將文檔路由到索引中的特定分片。

shard_num = hash(_routing) % num_primary_shards

自定義路由模式能夠經過指定每一個文檔的自定義路由值來實現。

PUT my_index/my_type/3?routing=user1
{
  "title":"this is 3",
  "body":"this is 3 body"
}

GET my_index/my_type/3?routing=user1

查詢結果

{
  "_index": "my_index",
  "_type": "my_type",
  "_id": "3",
  "_version": 2,
  "_routing": "user1",
  "found": true,
  "_source": {
    "title": "this is 3",
    "body": "this is 3 body"
  }
}

查詢全部「user1」路由下的文檔

GET my_index/_search
{
  "query": {
    "term": {
      "_routing": {
        "value": "user1"
      }
    }
  }
}

查詢結果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "3",
        "_score": 0.2876821,
        "_routing": "user1",
        "_source": {
          "title": "this is 3",
          "body": "this is 3 body"
        }
      }
    ]
  }
}
相關文章
相關標籤/搜索