您的位置:首页 > 理论基础 > 数据结构算法

ElasticSearch51:索引管理_内核级知识点_深入探秘type底层数据结构

2018-01-08 17:32 429 查看
1.每个index对应的type底层结构是怎样的?

type,是一个index中用来区分类似的数据的数据结构,但是可能有不同的field,而且有不同的属性来控制索引建立、分词器

field的value,在底层的lucense中建立索引的时候,全部是opaque bytes类型,不区分类型。

lucense是没有type的概念,在document中,实际上是将type作为一个document的field来存储,即_type。

es通过_type来进行type的过滤和筛选

一个index中的多个type,实际上是放在一起存储的,因此一个index下,不能有多个type重名,而类型或者其他设置不同的,因为那样是无法处理的。

例子:我们创建一个index,并加入两个type
{
"ecommerce":{
"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"
}
}
}

}
}
}

 


其实,index的type底层结构是这样的

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


如果我们加入两条数据

{

    "name":"geli kongtiao",

    "price":1999,

    "service_period":"one year"

}

{

    "name":"aozhou dalongxia",

    "price":999,

    "eat_period":"one week"

}

那么其实底层存储的是这样的两条数据,会将其他的字段也加入,每个document包含该index中所有的type的field

{

    "name":"geli kongtiao",

    "price":1999,

    "eat_period":"",

    "service_period":"one year"

}

{

    "name":"aozhou dalongxia",

    "price":999,

    "eat_period":"one week",

    "service_period":"one year"

}

2.最佳实践

将类似的结构的type放在一个index下,这些type应该有多个field是相同的

加入说,你将两个field完全不同的type放到同一个index下,那么每条数据都至少有一半的field在底层的lucense中时空值,会有严重的性能问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch