官方文档参见:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html
_all字段
_all字段是把所有其它字段中的值,以空格为分隔符组成一个大字符串,然后被分析和索引,但是不存储,也就是说它能被查询,但不能被取回显示。
_all能让你在不知道要查找的内容是属于哪个具体字段的情况下进行搜索,例如:
PUT my_index/user/1
{
"first_name": "John",
"last_name": "Smith",
"date_of_birth": "1970-10-24"
}
GET my_index/_search
{
"query": {
"match": {
"_all": "john smith 1970"
}
}
}
_all字段的内容为:”john smith 1970 10 24”
_all字段其实就是一个字符串类型的字段,与其它普通字符串字段有相同的参数,包括:analyzer, term_vectors, index_options, and store。
_all字段在查询时占用更多的CPU和占用更多的磁盘空间,如果确实不需要它可以完全的关闭它或者基于每字段定制。
使用_all字段查询
query_string 和 simple_query_string 查询默认都是查询 _all 字段,除非指定了其它默认字段。
GET _search
{
"query": {
"query_string": {
"query": "john smith 1970"
}
}
}
上面的查询也等同于:
GET _search?q=john+smith+1970
其它查询,象 match and term 查询需要明确的指明_all字段,就象上面第一个查询例子。
Disabling _all字段
_all 字段可以被完全的disabled:
PUT my_index
{
"mappings": {
"type_1": {
"properties": {...}
},
"type_2": {
"_all": {
"enabled": false
},
"properties": {...}
}
}
}
type_1中的_all字段是enabled
type_2中的_all字段是disabled
如果_all字段被disabled,那么query_string 和 simple_query_string就不能使用了,你必须要在配置文件中指定 其它的字段作默认的查询字段:
PUT my_index
{
"mappings": {
"my_type": {
"_all": {
"enabled": false
},
"properties": {
"content": {
"type": "string"
}
}
}
},
"settings": {
"index.query.default_field": "content"
},
}
现在query_string 和 simple_query_string将默认查询content字段 。
从_all排除一些无用字段 include_in_all
通过 include_in_all 参数能控制每个字段是否放入_all字段中,默认是每个字段都会放入_all中,除非index参数设置为no。
例如:
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"title": {
"type": "string"
}
"content": {
"type": "string"
},
"date": {
"type": "date",
"include_in_all": false
},
"memo": {
"type": "string",
"index": "no",
"store": true
}
}
}
}
title,content字段将会放入_all中。
date字段不会被放入_all中。
memo字段也不会被放_all中,因为index=no表示不索引数据,也就不会放入_all中。
include_in_all 参数也可以在type级别设置,并且在 object 或 nested 字段都可以, 在这种情况下所有子字段都将继承该设置。例如:
PUT my_index
{
"mappings": {
"my_type": {
"include_in_all": false,
"properties": {
"title": { "type": "string" },
"author": {
"include_in_all": true,
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" }
}
},
"editor": {
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string", "include_in_all": true }
}
}
}
}
}
}
先在my_type级别排除所有字段进入_all
然后,author.first_name 和 author.last_name 字段会放入_all
editor.first_name继承了my_type属性,不会放入_all
editor.last_name会放入_all
定制多个_all字段
虽然每个索引/type只有一个_all字段,但是可以通过copy_to参数创建多个定制的_all字段,例如下面将first_name,last_name合并放入full_name字段中:
PUT myindex
{
"mappings": {
"mytype": {
"properties": {
"first_name": {
"type": "string",
"copy_to": "full_name"
},
"last_name": {
"type": "string",
"copy_to": "full_name"
},
"full_name": {
"type": "string"
}
}
}
}
}
PUT myindex/mytype/1
{
"first_name": "John",
"last_name": "Smith"
}
GET myindex/_search
{
"query": {
"match": {
"full_name": "John Smith"
}
}
}
可以通过full_name字段查询,但是同_all一样不能被取回,除非设置store=true。
取回显示_all字段内容
_all 字段并没有被持久化放到 _source 字段中 并且默认它也不会被存储,因此它不能被查询后取回显示。
有两种方法可以,一种是store _all字段,另一种就是取原始字段内容。
存储 _all 字段
PUT myindex { "mappings": { "mytype": { "_all": { "store": true } } } } PUT myindex/mytype/1 { "first_name": "John", "last_name": "Smith" } GET _search?fields=_source,first_name,last_name,_all { "query": { "match": { "_all": "John Smith" } } }
取原始字段
PUT myindex { "mappings": { "mytype": { "_all": { "store": false } } } } PUT myindex/mytype/1 { "first_name": "John", "last_name": "Smith" } GET _search?fields=_source,*name,_all { "query": { "match": { "_all": "John Smith" } } }