_all field

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-all-field.html

译文链接 : http://www.apache.wiki/display/Elasticsearch/_all+field

贡献者 : 朱彦安ApacheCNApache中文网

_all 字段是一个特殊的全能字段,它将所有其他字段的值连接成一个大字符串,使用空格作为分隔符,然后对其进行分词和索引,但不存储。 这意味着它可以被检索,但不能取回。

_all 字段允许您在文档中搜索,并不需要知道哪个字段包含该值。 这使得它成为开始使用新数据集时的有用选项。 例如 :

  1. curl -XPUT 'localhost:9200/my_index/user/1?pretty' -H 'Content-Type: application/json' -d' # 1
  2. {
  3. "first_name": "John",
  4. "last_name": "Smith",
  5. "date_of_birth": "1970-10-24"
  6. }
  7. '
  8. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  9. {
  10. "query": {
  11. "match": {
  12. "_all": "john smith 1970"
  13. }
  14. }
  15. }
  16. '

| 1 | 该 _all 字段包含项: [ “john”, “smith”, “1970”, “10”, “24” ] |

注意 :

所有值被视为字符串

上述示例中的 date_of_birth 字段被视为 date 字段,因此将索引表示 1970-10-24 00:00:00 UTC 的单个项。 然而,_all 字段将所有值视为字符串,因此日期值被索引成三个字符串项 : “1970”,“24”,“10”。 注意,_all 字段将每个字段的原始值作为字符串组合。 它不会组合来自每个字段的项。

_all 字段是一个文本字段,并接受与其他字符串字段相同的参数,包括 analyzerterm_vectorsindex_options store

_all 字段是有用的,特别是当使用简单的过滤来探索新的数据时。 但是,通过将字段值连接成一个大字符串,_all 字段将丢失短字段(更相关)和长字段(较不相关)之间的区别。 对于搜索相关性很重要的用例,最好专门查询各个字段。

_all 字段并不轻量 : 它需要额外的 CPU 并使用更多的磁盘空间。 如果不需要,它可以完全禁用或个别字段定制。

在查询中使用 _all 字段

query_stringsimple_query_string 查询默认查询 _all 字段,除非指定了另一个字段 :

  1. curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "query": {
  4. "query_string": {
  5. "query": "john smith 1970"
  6. }
  7. }
  8. }
  9. '

同样的在URI搜索请求中 ?q= parameter(内部改写为query_string):

  1. GET _search?q=john+smith+1970

其他查询(如 matchterm 查询)要求您按照 第一个示例 显式指定 _all 字段。

禁用 _all 字段

通过将 enabled 设置为 false,可以对 _all 字段完全禁用:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "type_1": { # 1
  5. "properties": {...}
  6. },
  7. "type_2": { # 2
  8. "_all": {
  9. "enabled": false
  10. },
  11. "properties": {...}
  12. }
  13. }
  14. }
  15. '

| 1 | type_1 中的 _all 字段已启用。 | | 2 | type_2 中的 _all 字段已完全禁用. |

如果 _all 字段被禁用,则 URI 搜索请求和 query_string simple_query_string 查询将无法将其用于查询(请参阅在查询中使用 _all 字段)。 您可以将它们配置为使用与 index.query.default_field 设置不同的字段 :

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "_all": {
  6. "enabled": false # 1
  7. },
  8. "properties": {
  9. "content": {
  10. "type": "text"
  11. }
  12. }
  13. }
  14. },
  15. "settings": {
  16. "index.query.default_field": "content" # 2
  17. }
  18. }
  19. '

| 1 | _all字段对于my_type类型是禁用的。 | | 2 | query_string查询将默认查询此索引中的content字段。 |

排除 _all 的字段

可以使用 include_in_all 设置从 _all 字段中包含或排除各个字段。

索引提升和 _all 字段

使用 boost 参数,索引时可以提升个别字段。 _all 字段考虑到这些提升 :

  1. curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "mytype": {
  5. "properties": {
  6. "title": { # 1
  7. "type": "text",
  8. "boost": 2
  9. },
  10. "content": { # 2
  11. "type": "text"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. '

| 1 2 | 当查询 _all 字段时,源于 title 字段的字符是源于 content 字段的字符的两倍相关。 |

警告 :

使用 _all 字段的在索引时 boost 对查询性能有显着的影响。 通常更好的解决方案是单独查询字段,可在查询时 boost

自定义 _all 字段

虽然每个索引只有一个 _all 字段,但 copy_to 参数允许创建多个自定义 _all 字段。 例如,first_name last_name 字段可以组合到 full_name 字段中 :

  1. curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "mytype": {
  5. "properties": {
  6. "first_name": {
  7. "type": "text",
  8. "copy_to": "full_name" # 1
  9. },
  10. "last_name": {
  11. "type": "text",
  12. "copy_to": "full_name" # 2
  13. },
  14. "full_name": {
  15. "type": "text"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. '
  22. curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
  23. {
  24. "first_name": "John",
  25. "last_name": "Smith"
  26. }
  27. '
  28. curl -XGET 'localhost:9200/myindex/_search?pretty' -H 'Content-Type: application/json' -d'
  29. {
  30. "query": {
  31. "match": {
  32. "full_name": "John Smith"
  33. }
  34. }
  35. }
  36. '

| 1 2 | first_namelast_name 值将复制到 full_name 字段。 |

高亮显示和 _all 字段

如果原始字符串值可用,则字段只能用于 高亮 显示,从 _source 字段或作为存储字段。

_all 字段不在 _source 字段中,默认情况下不存储,因此无法高亮显示。 有两个选项。存储 _all 字段或高亮显示 原始字段

存储 _all 字段

如果存储设置为 true,则可以检索原始字段值,并且可以高亮显示 :

  1. curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "mytype": {
  5. "_all": {
  6. "store": true
  7. }
  8. }
  9. }
  10. }
  11. '
  12. curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
  13. {
  14. "first_name": "John",
  15. "last_name": "Smith"
  16. }
  17. '
  18. curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
  19. {
  20. "query": {
  21. "match": {
  22. "_all": "John Smith"
  23. }
  24. },
  25. "highlight": {
  26. "fields": {
  27. "_all": {}
  28. }
  29. }
  30. }
  31. '

当然,存储 _all 字段将使用更多的磁盘空间,因为由于它是其他字段的组合,所以可能会导致奇怪的高亮显示结果。 _all 字段也接受 term_vector index_options 参数,允许使用快速的矢量荧光笔和贴片荧光笔。

高亮显示原始字段

您可以查询 _all 字段,但使用原始字段进行高亮显示,如下所示 :

  1. curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "mytype": {
  5. "_all": {}
  6. }
  7. }
  8. }
  9. '
  10. curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
  11. {
  12. "first_name": "John",
  13. "last_name": "Smith"
  14. }
  15. '
  16. curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
  17. {
  18. "query": {
  19. "match": {
  20. "_all": "John Smith" # 1
  21. }
  22. },
  23. "highlight": {
  24. "fields": {
  25. "*_name": { # 2
  26. "require_field_match": false # 3
  27. }
  28. }
  29. }
  30. }
  31. '

| 1 | 查询检查 _all 字段以查找匹配的文档。 | | 2 | 在 _source 可以使用的两个名称字段上执行高亮显示。 | | 3 | 该查询未针对 name 字段运行,因此将 require_field_match 设置为 false。 |