# Token数

原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html)

译文链接 :[http://www.apache.wiki/pages/viewpage.action?pageId=10030674](http://www.apache.wiki/pages/viewpage.action?pageId=10030674)

贡献者 : [李亚运](/display/~liyayun)，[ApacheCN](/display/~apachecn)，[Apache中文网](/display/~apachechina)

## 简述

类型为`token_count`的字段是一个接受字符串值的[`integer`](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html)字段，对它们进行分析，然后对字符串中的token数进行索引。

## 示例

例如：

```
PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "name": { 
          "type": "text",
          "fields": {
            "length": { 
              "type":     "token_count",
              "analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

PUT my_index/my_type/1
{ "name": "John Smith" }

PUT my_index/my_type/2
{ "name": "Rachel Alice Williams" }

GET my_index/_search
{
  "query": {
    "term": {
      "name.length": 3 
    }
  }
}
```

| [![](img/24cb6cd12df1a0cbdb39ab06379bd3de.jpg)](https://translate.googleusercontent.com/translate_c?depth=1&hl=zh-CN&rurl=translate.google.com&sl=en&sp=nmt4&tl=zh-CN&u=https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html&usg=ALkJrhj-xw1n9gZS9tQ_tK5CUYOeTr55wA#CO186-1) | `name`字段是使用默认`standard`分析器的分析字符串字段。 |
| [![](img/c7d178c0e490b3b3fa632f5c43fa27ff.jpg)](https://translate.googleusercontent.com/translate_c?depth=1&hl=zh-CN&rurl=translate.google.com&sl=en&sp=nmt4&tl=zh-CN&u=https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html&usg=ALkJrhj-xw1n9gZS9tQ_tK5CUYOeTr55wA#CO186-2) | `name.length`字段是一个`token_count` [多字段](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html) ，它将在`name`字段中索引token的数量。 |
| [![](img/4eebc14898339f6b58322f85335942c7.jpg)](https://translate.googleusercontent.com/translate_c?depth=1&hl=zh-CN&rurl=translate.google.com&sl=en&sp=nmt4&tl=zh-CN&u=https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html&usg=ALkJrhj-xw1n9gZS9tQ_tK5CUYOeTr55wA#CO186-3) | 此查询仅匹配包含`Rachel Alice Williams`的文档，因为它包含三个token。 |

![注意](img/66451d8bfad3b8dc5776cc1eafdf57b6.jpg)

在技术上， `token_count`类型对位置增量进行求和，而不是对token计数。 这意味着即使分析仪滤除停止词，它们也包括在计数中。

## 参数

`token_count`字段接受以下参数：

| [`analyzer`](https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html) | 应该用于分析字符串值的[分析器](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html) 。必须值。 为获得最佳性能，请使用不带token过滤器的分析器。 |
| [`boost`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-boost.html) | 映射字段级查询时间提升。 接受一个浮点数，默认为`1.0` 。 |
| [`doc_values`](https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html) | 该字段是否应该以多列的方式存储在磁盘上，以便以后可以将其用于排序，聚合或脚本化？接受`true` （默认）或`false` 。 |
| [`index`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index.html) | 应该可以搜索该字段吗？ 接受`not_analyzed` （默认）和`no` 。 |
| [`include_in_all`](https://www.elastic.co/guide/en/elasticsearch/reference/current/include-in-all.html) | 字段值是否应包含在[`_all`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html)字段中？ 接受`true`或`false` 默认为`false` 。 注意：如果为`true` ，则将字符串值添加到_all，而不是计算的token数量。 |
| [`null_value`](https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html) | 接受与替换任何显式`null`值的字段相同`type`的数值。 默认为`null` ，这意味着该字段被视为丢失。 |
| [`store`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html) | 字段值是否应与[`_source`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html)字段分开存储和检索。 接受`true`或`false` （默认）。 |