63_索引管理_核心級知識點:深入探秘type底層資料結構

5765809發表於2024-10-02

type,是一個index中用來區分類似的資料的,類似的資料,但是可能有不同的fields,而且有不同的屬性來控制索引建立、分詞器
field的value,在底層的lucene中建立索引的時候,全部是opaque bytes型別,不區分型別的
lucene是沒有type的概念的,在document中,實際上將type作為一個document的field來儲存,即_type,es透過_type來進行type的過濾和篩選
一個index中的多個type,實際上是放在一起儲存的,因此一個index下,不能有多個type重名,而型別或者其他設定不同的,因為那樣是無法處理的

{
"ecommerce": {
"mappings": {
"elactronic_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"
}
}
}
}
}
}

{
"name": "geli kongtiao",
"price": 1999.0,
"service_period": "one year"
}

{
"name": "aozhou dalongxia",
"price": 199.0,
"eat_period": "one week"
}

在底層的儲存是這樣子的。。。。

{
"ecommerce": {
"mappings": {
"_type": {
"type": "string",
"index": "not_analyzed"
},
"name": {
"type": "string"
}
"price": {
"type": "double"
}
"service_period": {
"type": "string"
}
"eat_period": {
"type": "string"
}
}
}
}

{
"_type": "elactronic_goods",
"name": "geli kongtiao",
"price": 1999.0,
"service_period": "one year",
"eat_period": ""
}

{
"_type": "fresh_goods",
"name": "aozhou dalongxia",
"price": 199.0,
"service_period": "",
"eat_period": "one week"
}

最佳實踐,將類似結構的type放在一個index下,這些type應該有多個field是相同的
假如說,你將兩個type的field完全不同,放在一個index下,那麼就每條資料都至少有一半的field在底層的lucene中是空值,會有嚴重的效能問題

相關文章