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的大量空值,造成資源浪費。所以,不同類型數據,要放到不同的索引中。在