1. 程式人生 > 其它 >ElasticSearch7.3 學習之type底層結構及棄用原因

ElasticSearch7.3 學習之type底層結構及棄用原因

1、type是什麼

type,是一個index中用來區分類似的資料的。類似的資料,但是可能有不同的fields,而且有不同的屬性來控制索引建立、分詞器、field的value。

在底層的lucene中建立索引的時候,全部是opaque bytes型別,不區分型別的。

lucene是沒有type的概念的,在document中,實際上將type作為一個document的field來儲存,即type。

ElasticSearch通過type來進行type的過濾和篩選。

2、es中不同type儲存機制

一個index中的多個type,實際上是放在一起儲存的,因此一個index下,不能有多個type重名,而型別或者其他設定不同的,因為那樣是無法處理的。注意:下面語句博主沒有實驗過,只是瀏覽了一遍,加深自己的理解。

{
   "goods": {
      "mappings": {
         "electronic_goods": {
            "properties": {
               "name": {
                  "type": "string",
               },
               "price": {
                  "type": "double"
               },
               "service_period": {
                  "type": "string"
                   }            
                }
         },
         "fresh_goods": {
            "properties": {
               "name": {
                  "type": "string",
               },
               "price": {
                  "type": "double"
               },
               "eat_period": {
                    "type": "string"
               }
                }
         }
      }
   }
}
PUT /goods/electronic_goods/1
{
  "name": "小米空調",
  "price": 1999.0,
  "service_period": "one year"
}
PUT /goods/fresh_goods/1
{
  "name": "澳洲龍蝦",
  "price": 199.0,
  "eat_period": "one week"
}

es文件在底層的儲存是這樣子的

{
   "goods": {
      "mappings": {
        "_type": {
          "type": "string",
          "index": "false"
        },
        "name": {
          "type": "string"
        }
        "price": {
          "type": "double"
        }
        "service_period": {
          "type": "string"
        },
        "eat_period": {
          "type": "string"
        }
      }
   }
}

底層資料儲存格式

{
  "_type": "electronic_goods",
  "name": "小米空調",
  "price": 1999.0,
  "service_period": "one year",
  "eat_period": ""
}
{
  "_type": "fresh_goods",
  "name": "澳洲龍蝦",
  "price": 199.0,
  "service_period": "",
  "eat_period": "one week"
}

3、type棄用

同一索引下,不同type的資料也會儲存其他type的field的大量空值,造成資源浪費。所以,不同型別資料,要放到不同的索引中。在es9中,將會徹底刪除type。