Token count field type

A field of type token_count is really an integer field which accepts string values, analyzes them, then indexes the number of tokens in the string.

For instance:

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name": {
  6. "type": "text",
  7. "fields": {
  8. "length": {
  9. "type": "token_count",
  10. "analyzer": "standard"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
  17. PUT my-index-000001/_doc/1
  18. { "name": "John Smith" }
  19. PUT my-index-000001/_doc/2
  20. { "name": "Rachel Alice Williams" }
  21. GET my-index-000001/_search
  22. {
  23. "query": {
  24. "term": {
  25. "name.length": 3
  26. }
  27. }
  28. }

The name field is a text field which uses the default standard analyzer.

The name.length field is a token_count multi-field which will index the number of tokens in the name field.

This query matches only the document containing Rachel Alice Williams, as it contains three tokens.

Parameters for token_count fields

The following parameters are accepted by token_count fields:

analyzer

The analyzer which should be used to analyze the string value. Required. For best performance, use an analyzer without token filters.

enable_position_increments

Indicates if position increments should be counted. Set to false if you don’t want to count tokens removed by analyzer filters (like stop). Defaults to true.

boost

Mapping field-level query time boosting. Accepts a floating point number, defaults to 1.0.

doc_values

Should the field be stored on disk in a column-stride fashion, so that it can later be used for sorting, aggregations, or scripting? Accepts true (default) or false.

index

Should the field be searchable? Accepts true (default) and false.

null_value

Accepts a numeric value of the same type as the field which is substituted for any explicit null values. Defaults to null, which means the field is treated as missing.

store

Whether the field value should be stored and retrievable separately from the _source field. Accepts true or false (default).