Elasticsearch-2.4.x > Mapping > Meta-fields > _all

_all字段

_all字段是一个特殊的catch-all字段.它把其他字段的值连接成一个大的string,使用空格作为分隔符.然后进行分析和索引,但是不被存储.这就意味着它可以被搜索,但是不能取出,高亮.
_all字段允许查询文档值但不知道哪个字段包含的值.这对开始使用新的数据集的时候是有用的.例如:

PUT my_index/user/1                                                           (1)
{
  "first_name":    "John",
  "last_name":     "Smith",
  "date_of_birth": "1970-10-24"
}

GET my_index/_search
{
  "query": {
    "match": {
      "_all": "john smith 1970"
    }
  }
}

(1) 这个_all字段包含的term[“john”,”smith,”1970”,”10”,”24”]

note : 所有值都被视为string
date_of_birth 字段在上述的例子中被指定为date类型的字段并且索引为一个表示1970-10-24 00:00:00 UTC的单一term.因为_all视所有值为string,所以该字段值会被索引为三个term [“1970”,”24”,”10”]
更要注意的是:_all字段将每个字段的原始值组合成为string,而没有把每个字段的term合并起来.

_all字段是一个string field,并接受其他string字段接受的相同参数. 包含 : analzyer , term_vectors , index_options and store.
_all字段需要额外的cpu周期和更多的磁盘空间.如果不需要的话,可以完全禁用或者自定义_all字段

使用_all字段查询

query_string and simple_query_string查询默认都是针对_all字段.除非指定了另一个字段

GET _search
{
  "query": {
    "query_string": {
      "query": "john smith 1970"
    }
  }
}

其他的查询,比如 match 和 term 查询你需要显示的指定_all字段,根据上述第一个例子

禁用_all字段

每个类型通过设置enabled:false将_all字段完全禁用

PUT my_index
{
  "mappings": {
    "type_1": {                                                                    (1)
      "properties": {...}
    },
    "type_2": {                                                                    (2)
      "_all": {
        "enabled": false
      },
      "properties": {...}
    }
  }
}

(1) type_1的_all是启用的
(2) type_2的_all是完全禁用的

如果_all字段是禁用的,query_string and simple_query_string将不能用于查询.你可以将它们配置为使用不同的字段 index.query.default_field setting:

PUT my_index
{
  "mappings": {
    "my_type": {
      "_all": {
        "enabled": false                                                                 (1)
      },
      "properties": {
        "content": {
          "type": "string"
        }
      }
    }
  },
  "settings": {
    "index.query.default_field": "content"                                               (2)
  },
}

(1) my_type类型_all字段是禁用的
(2) query_string默认查询content字段

从_all字段中排除字段

可以使用include_in_all设置从_all字段包含或者排除的单独字段

index boosting和_all字段

单个字段可以在索引的时候提高权重,使用boost参数._all字段考虑了这些提升.

PUT myindex
{
  "mappings": {
    "mytype": {
      "properties": {
        "title": {                                                                        (1)
          "type": "string",
          "boost": 2
        },
        "content": {                                                                      (2)
          "type": "string"
        }
      }
    }
  }
}

(1) 当查询_all字段的时候,title字段的词的相关性会是后者的两倍
(2) content字段的词

warning: 在_all字段中使用 index-time boosting对查询性能有显著的影响.通常更好的解决方案是单独查询字段.

自定义_all字段

虽然一个索引只有一个all字段,但是copy_to参数允许创建多个自定义_all字段.例如:first_name and last_name合并到full_name

PUT myindex
{
  "mappings": {
    "mytype": {
      "properties": {
        "first_name": {
          "type":    "string",
          "copy_to": "full_name"                                     (1)
        },
        "last_name": {
          "type":    "string",
          "copy_to": "full_name"                                     (2)
        },
        "full_name": {
          "type":    "string"
        }
      }
    }
  }
}

PUT myindex/mytype/1
{
  "first_name": "John",
  "last_name": "Smith"
}

GET myindex/_search
{
  "query": {
    "match": {
      "full_name": "John Smith"
    }
  }
}

(1) first_name 和 last_name值被copy到full_name字段.
(2)

高亮和_all字段

如果一个string值是可以获得的,那么这个字段就可以被高亮显示.要么来自_source字段,要么是一个存储字段.
_all字段不在_source字段中,默认情况下不存储.所以不能用于高亮显示.有两个选项:要么存储_all字段,要么高亮原始字段.

存储_all字段

如果store设置为true,则可以取出_all原始字段并高亮显示.

PUT myindex
{
  "mappings": {
    "mytype": {
      "_all": {
        "store": true
      }
    }
  }
}

PUT myindex/mytype/1
{
  "first_name": "John",
  "last_name": "Smith"
}

GET _search
{
  "query": {
    "match": {
      "_all": "John Smith"
    }
  },
  "highlight": {
    "fields": {
      "_all": {}
    }
  }
}

当然,存储_all字段将会占用更多的磁盘空间.因为它是其他字段的组合,所以可能会导致奇怪的高亮结果.
_all字段也接受term_vector和index_options参数,允许使用fast vector highlighter和postings highlighter.

高亮原始字段

你可以查询_all字段,但是使用原始字段高亮:

PUT myindex
{
  "mappings": {
    "mytype": {
      "_all": {}
    }
  }
}

PUT myindex/mytype/1
{
  "first_name": "John",
  "last_name": "Smith"
}

GET _search
{
  "query": {
    "match": {
      "_all": "John Smith"                                                                      (1)
    }
  },
  "highlight": {
    "fields": {
      "*_name": {                                                                               (2)
        "require_field_match": "false"                                                          (3)
      }
    }
  }
}

(1) 查询检查_all字段找到匹配的文档
(2) 高亮是在两个name字段上执行的,它们可以从_source字段获取
(3) 查询没有针对name字段进行,所以将require_field_match设置为false.

官网 : https://www.elastic.co/guide/en/elasticsearch/reference/2.4/mapping-all-field.html#highlighting-all-field

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值