1. _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 字段允许您在文档中搜索,并不需要知道哪个字段包含该值。 这使得它成为开始使用新数据集时的有用选项。 例如 :

curl -XPUT 'localhost:9200/my_index/user/1?pretty' -H 'Content-Type: application/json' -d' # 1
{
  "first_name":    "John",
  "last_name":     "Smith",
  "date_of_birth": "1970-10-24"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "john smith 1970"
    }
  }
}
'

| 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 并使用更多的磁盘空间。 如果不需要,它可以完全禁用或个别字段定制。

1.1.1. 在查询中使用 _all 字段

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

 curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "john smith 1970"
    }
  }
}
'

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

GET _search?q=john+smith+1970

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

1.1.2. 禁用 _all 字段

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

 curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "type_1": { # 1
      "properties": {...}
    },
    "type_2": { # 2
      "_all": {
        "enabled": false
      },
      "properties": {...}
    }
  }
}
'

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

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

 curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "_all": {
        "enabled": false # 1
      },
      "properties": {
        "content": {
          "type": "text"
        }
      }
    }
  },
  "settings": {
    "index.query.default_field": "content" # 2
  }
}
'

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

1.1.3. 排除 _all 的字段

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

1.1.4. 索引提升和 _all 字段

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

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "properties": {
        "title": { # 1
          "type": "text",
          "boost": 2
        },
        "content": { # 2
          "type": "text"
        }
      }
    }
  }
}
'

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

警告 :

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

1.1.5. 自定义 _all 字段

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

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "properties": {
        "first_name": {
          "type":    "text",
          "copy_to": "full_name" # 1
        },
        "last_name": {
          "type":    "text",
          "copy_to": "full_name" # 2
        },
        "full_name": {
          "type":    "text"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/myindex/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "full_name": "John Smith"
    }
  }
}
'

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

1.1.6. 高亮显示和 _all 字段

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

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

1.1.7. 存储 _all 字段

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

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "_all": {
        "store": true
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "John Smith"
    }
  },
  "highlight": {
    "fields": {
      "_all": {}
    }
  }
}
'

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

1.1.8. 高亮显示原始字段

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

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "_all": {}
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "John Smith" # 1
    }
  },
  "highlight": {
    "fields": {
      "*_name": { # 2
        "require_field_match": false # 3
      }
    }
  }
}
'

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

Copyright © Kilvn 2021. all right reserved,powered by Gitbook最后更新时间: 2021-06-08 20:22:42

results matching ""

    No results matching ""