ITPub博客

首页 > 应用开发 > Java > ElasticSearch _all和_source

ElasticSearch _all和_source

原创 Java 作者:壹頁書 时间:2016-05-20 21:18:53 0 删除 编辑
ElasticSearch 有两个内部结构_all和_source

_all是所有字段的大杂烩,默认情况下,每个字段的内容,都会拷贝到_all字段.
这样可以忽略字段信息进行搜索,非常方便.
但是这样带来了额外的存储压力和CPU处理压力.

在默认情况下,开启_all和_source
  1. {  
  2.     "mappings": {  
  3.         "sod_song_ksc": {  
  4.             "_source": {  
  5.                 "enabled": true  
  6.             },   
  7.             "_all": {  
  8.                 "enabled": true,   
  9.                 "analyzer": "ik"  
  10.             },   
  11.             "properties": {  
  12.                 "SongID": {  
  13.                     "type": "long",   
  14.                     "store": "yes",   
  15.                     "index": "not_analyzed"  
  16.                 },   
  17.                 "Name": {  
  18.                     "type": "multi_field",   
  19.                     "fields": {  
  20.                         "Name": {  
  21.                             "type": "string",   
  22.                             "store": "yes",   
  23.                             "index": "analyzed",   
  24.                             "analyzer": "ik"  
  25.                         },   
  26.                         "raw": {  
  27.                             "type": "string",   
  28.                             "store": "yes",   
  29.                             "index": "not_analyzed"  
  30.                         }  
  31.                     }  
  32.                 },   
  33.                 "SingerName": {  
  34.                     "type": "string",   
  35.                     "store": "yes",   
  36.                     "index": "analyzed",   
  37.                     "analyzer": "stop"  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42. }  

这样索引下来,占用空间很大.


根据我们单位的情况,我觉得可以将需要的字段保存在_all中,然后使用IK分词以备查询,其余的字段,则不存储.
并且禁用_source字段.(也可以通过_source includes或者excludes 指定或者排除字段)
  1. {  
  2.     "mappings": {  
  3.         "sod_song_ksc": {  
  4.             "dynamic_templates": [  
  5.                 {  
  6.                     "all_field": {  
  7.                         "mapping": {  
  8.                             "index": "no",   
  9.                             "store": "yes",   
  10.                             "type": "{dynamic_type}",   
  11.                             "include_in_all": false  
  12.                         },   
  13.                         "match": "*"  
  14.                     }  
  15.                 }  
  16.             ],   
  17.             "_source": {  
  18.                 "enabled": false  
  19.             },   
  20.             "_all": {  
  21.                 "enabled": true,   
  22.                 "analyzer": "ik"  
  23.             },   
  24.             "properties": {  
  25.                 "SongID": {  
  26.                     "type": "long",   
  27.                     "store": "yes",   
  28.                     "index": "not_analyzed",   
  29.                     "include_in_all": true  
  30.                 },   
  31.                 "Name": {  
  32.                     "type": "multi_field",   
  33.                     "fields": {  
  34.                         "Name": {  
  35.                             "type": "string",   
  36.                             "store": "yes",   
  37.                             "index": "analyzed",   
  38.                             "analyzer": "ik"  
  39.                         },   
  40.                         "raw": {  
  41.                             "type": "string",   
  42.                             "store": "yes",   
  43.                             "index": "not_analyzed",   
  44.                             "include_in_all": true  
  45.                         }  
  46.                     }  
  47.                 },   
  48.                 "SingerName": {  
  49.                     "type": "string",   
  50.                     "store": "yes",   
  51.                     "index": "analyzed",   
  52.                     "analyzer": "stop",   
  53.                     "include_in_all": true  
  54.                 }  
  55.             }  
  56.         }  
  57.     }  
  58. }  

这里采用了动态映射的功能(dynamic_templates),符合条件的字段,直接采用动态映射中预先的配置.
这里动态映射匹配所有字段,将_all禁用。
然后有需要的字段,再逐个开启.
索引之后,效果非常明显.磁盘占用减少了一半还多.

动态映射 {name}表示文档中原始的字段名称,{dynamic_type}表示原始文档的类型



动态映射在实际使用中,除了匹配字段名称,还可以匹配类型。
"match_mapping_type": "string"

动态映射如果设置在 "_default_" 则表示规则应用在所有类型的索引上.

虽然禁用_source,指定加入_all的字段,可以大幅减少磁盘空间占用,
但是查询会麻烦一些。原来可以直观看到的数据没有了.


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



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







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

上一篇: ElasticSearch Java API
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339972