ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中 1._all  

ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 

1.1_all field

_all字段是一个很少用到的字段,它连接所有字段的值构成一个用空格(space)分隔的大string,该string被analyzed和index,但是不被store。当你不知道不清楚document结构的时候,可以用_all。如,有一document:

[java] view plain copy
 
ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 
  1. curl -XPUT 'http://127.0.0.1:9200/myindex/order/0508' -d '{  
  2.     "name": "Scott",  
  3.     "age": "24"  
  4. }'  


用_all字段search:

[java] view plain copy
 
ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 
  1. curl -XGET "http://127.0.0.1:9200/myindex/order/_search?pretty" -d '{  
  2.     "query": {  
  3.         "match": {  
  4.             "_all": "Scott 24"  
  5.         }  
  6.     }  
  7. }'  

注意:_all是按空格(space)分隔的,所以,对于date类型就被analyzed为["year", "month", "day"]。如,一document:

[java] view plain copy
 
ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 
  1. {  
  2.   "first_name":    "John",  
  3.   "last_name":     "Smith",  
  4.   "date_of_birth": "1970-10-24"  
  5. }  
[java] view plain copy
 
ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 
  1. curl -XGET "http://127.0.0.1:9200/myindex/order/_search?pretty" -d '{  
  2.     "query": {  
  3.         "match": {  
  4.             "_all": "john smith 1970"  
  5.         }  
  6.     }  
  7. }'  

_all字段将包含["john", "smith", "1970", "10", "24"]。

所以,_all 字段仅仅是一个经过分析的 string 字段。它使用默认的分析器来分析它的值,而不管这值本来所在的字段指定的分析器。而且像所有 string 类型字段一样,你可以配置 _all 字段使用的分析器:

[java] view plain copy
 
ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 
  1. PUT /myindex/order/_mapping  
  2. {  
  3.     "order": {  
  4.         "_all": { "analyzer": "whitespace" }  
  5.     }  
  6. }  

1.2 Disable _all field

_all字段需要额外的CPU周期和更多的磁盘。所以,如果不需要_all,最好将其禁用!

1.3 Excluding fields from _all

你可能不想把_all禁用,而是希望_all包含某些特定的fields。通过include_in_all选项可以控制字段是否要被包含在_all字段 中,默认值是true。在一个对象上设置include_in_all可以修改这个对象所有字段的默认行为。如,指定_all包含name:

[java] view plain copy
 
ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all
 
  1. PUT /myindex/order/_mapping  
  2. {  
  3.     "order": {  
  4.         "include_in_all": false,  
  5.         "properties": {  
  6.             "name": {  
  7.                 "type": "string",  
  8.                 "include_in_all": true  
  9.             },  
  10.             ...  
  11.         }  
  12.     }  
  13. }