ElasticSearch _all和_source

壹頁書發表於2016-05-20
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部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2103996/,如需轉載,請註明出處,否則將追究法律責任。

相關文章