ElasticSearch 有两个内部结构_all和_source
_all是所有字段的大杂烩,默认情况下,每个字段的内容,都会拷贝到_all字段.
这样可以忽略字段信息进行搜索,非常方便.
但是这样带来了额外的存储压力和CPU处理压力.
在默认情况下,开启_all和_source
-
{
-
"mappings": {
-
"sod_song_ksc": {
-
"_source": {
-
"enabled": true
-
},
-
"_all": {
-
"enabled": true,
-
"analyzer": "ik"
-
},
-
"properties": {
-
"SongID": {
-
"type": "long",
-
"store": "yes",
-
"index": "not_analyzed"
-
},
-
"Name": {
-
"type": "multi_field",
-
"fields": {
-
"Name": {
-
"type": "string",
-
"store": "yes",
-
"index": "analyzed",
-
"analyzer": "ik"
-
},
-
"raw": {
-
"type": "string",
-
"store": "yes",
-
"index": "not_analyzed"
-
}
-
}
-
},
-
"SingerName": {
-
"type": "string",
-
"store": "yes",
-
"index": "analyzed",
-
"analyzer": "stop"
-
}
-
}
-
}
-
}
-
}
这样索引下来,占用空间很大.

根据我们单位的情况,我觉得可以将需要的字段保存在_all中,然后使用IK分词以备查询,其余的字段,则不存储.
并且禁用_source字段.(也可以通过_source includes或者excludes 指定或者排除字段)
-
{
-
"mappings": {
-
"sod_song_ksc": {
-
"dynamic_templates": [
-
{
-
"all_field": {
-
"mapping": {
-
"index": "no",
-
"store": "yes",
-
"type": "{dynamic_type}",
-
"include_in_all": false
-
},
-
"match": "*"
-
}
-
}
-
],
-
"_source": {
-
"enabled": false
-
},
-
"_all": {
-
"enabled": true,
-
"analyzer": "ik"
-
},
-
"properties": {
-
"SongID": {
-
"type": "long",
-
"store": "yes",
-
"index": "not_analyzed",
-
"include_in_all": true
-
},
-
"Name": {
-
"type": "multi_field",
-
"fields": {
-
"Name": {
-
"type": "string",
-
"store": "yes",
-
"index": "analyzed",
-
"analyzer": "ik"
-
},
-
"raw": {
-
"type": "string",
-
"store": "yes",
-
"index": "not_analyzed",
-
"include_in_all": true
-
}
-
}
-
},
-
"SingerName": {
-
"type": "string",
-
"store": "yes",
-
"index": "analyzed",
-
"analyzer": "stop",
-
"include_in_all": true
-
}
-
}
-
}
-
}
-
}
这里采用了动态映射的功能(dynamic_templates),符合条件的字段,直接采用动态映射中预先的配置.
这里动态映射匹配所有字段,将_all禁用。
然后有需要的字段,再逐个开启.
索引之后,效果非常明显.磁盘占用减少了一半还多.
动态映射 {name}表示文档中原始的字段名称,{dynamic_type}表示原始文档的类型

动态映射在实际使用中,除了匹配字段名称,还可以匹配类型。
"match_mapping_type": "string"
动态映射如果设置在 "_default_" 则表示规则应用在所有类型的索引上.
虽然禁用_source,指定加入_all的字段,可以大幅减少磁盘空间占用,
但是查询会麻烦一些。原来可以直观看到的数据没有了.

而且默认情况下,没有足够的信息显示.

所以查询显得麻烦一些,使用如下的方式:


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2103996/
,如需转载,请注明出处,否则将追究法律责任。