ES 14 - (底層原理) Elasticsearch內部如何處理不一樣type的數據

1 type的做用

在Elasticsearch的索引(index)中, 經過標識元字段_type來區分不一樣的type, 因此咱們能夠把具備相同字段(field)的文檔劃分到同一個type下.json

==> 於是_type也稱做映射類型, 即每一個type都有各自的mapping.數據結構

但即便是相似的數據, 也有可能存在不一樣的field, 好比:app

商品中有電子商品有電壓field;
服裝商品有洗滌方式field;
生鮮商品有養分成分field… 這些不一樣的field要如何處理呢?工具

==> 在以前的博文中有提到過: 同一index的不一樣type中, 同名的field的映射配置必須相同. 這是爲何呢?性能

2 type的底層數據結構

Elasticsearch底層所使用的核心工具庫——Lucene中並無type的說法, 它在創建索引的時候, 會把全部field的值當作opaque bytes(不透明字節)類型來處理:code

在存儲document時, ES會將該document所屬的type做爲一個type字段進行存儲;blog

在搜索document時, ES經過_type來進行過濾和篩選.索引

每一個index中的全部type都是存儲在一塊兒的, 所以:文檔

在Elasticsearch 6.0以前: 同一個index的不一樣type中, 同名的field的映射配置(_type)必須相同.

在Elasticsearch 6.0開始: 一個index中不能擁有多個type.

3 探究type的存儲結構

說明: 從Elasticsearch 6.0開始, 不容許在一個index中建立多個type ——只能建立一個, 不然將發生錯誤:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [website] as the final mapping would have more than 1 type: [manager, writer]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Rejecting mapping update to [website] as the final mapping would have more than 1 type: [manager, writer]"
  },
  "status": 400
}

這裏演示所用的版本是6.6.10, 特此說明.

3.1 建立索引並配置映射

PUT website
{
    "mappings": {      // Elasticsearch 6.0以後的版本中, 只添加這一個type
        "writer": {
            "properties": {
                "id": { "type": "long" },
                "name": { "type": "text" },
                "age": { "type": "integer" },
                "sex": { "type": "text", "index": false }
            }
        }, 
        "manager": {   // 省去此type
            "properties": {
                "id": { "type": "long" },
                "name": { "type": "text" },
                "age": { "type": "integer" },
                "sex": { "type": "text", "index": false }, 
                "authorize": { "type": "text", "index": false}
            }
        }
    }
}

3.2 添加數據

PUT website/writer/1
{
    "id": 1001,
    "name": "tester",
    "age": 18,
    "sex": "female"
}
// Elasticsearch 6.0以後的版本中, 不添加下述文檔:
PUT website/manager/1
{
    "id": 1001,
    "name": "shou feng",
    "age": 20,
    "sex": "male",
    "authorize": "all"
}

3.3 查看存儲結構

// 搜索全部數據
GET website/_search

// 搜索結果以下:
{
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "writer",    // _type是writer
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1001,
          "name" : "tester",
          "age" : 18,
          "sex" : "female"
        }
      },
      {
        "_index": "website",
        "_type": "manager",         // _type爲manager
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1001,
          "name": "shou feng",
          "age": 20,
          "sex": "male",
          "authorize": "all"
        }
      }
    ]
  }
}

4 關於type的最佳實踐

將結構相似的type存放在同一個index下 —— 這些type的大部分field應該是相同的.

若是將兩個field徹底不一樣的type存入同一個index下, 在Lucene底層存儲時, 每一個document中都將有一大部分field是空值, 這將致使嚴重的性能問題, 而且佔用磁盤空間:

例如: 上述website/writer的每一個document中, 都有"authorize"字段, 只是它們的值都爲空.

—— 從這個角度出發, 大概就能猜出 ES限制一個index中只能有一個type 的緣由了吧, 也就是更方便地組織文檔數據、節省磁盤空間😊

版權聲明

做者: 馬瘦風

出處: 博客園 馬瘦風的博客

您的支持是對博主的極大鼓勵, 感謝您的閱讀.

本文版權歸博主全部, 歡迎轉載, 但請保留此段聲明, 並在文章頁面明顯位置給出原文連接, 不然博主保留追究相關人員法律責任的權利.

相關文章
相關標籤/搜索