The token_count data type is a specialized text mapping that stores how many tokens an analyzer produces for a field. A book title mapped this way can be searched by token count, such as finding titles longer than three words. section 4.5.1
A practical token_count mapping follows this pattern:
- Create an index whose title field has type token_count. section 4.5.1
- Provide an analyzer; the example uses the standard analyzer to produce the counted tokens. section 4.5.1
- Index titles such as “Elasticsearch in Action,” “Elasticsearch for Java Developers,” and “Elastic Stack in Action.” section 4.5.1
- Run a range query to select counts greater than three and less than or equal to five; four-token titles match while a three-token title does not. section 4.5.1
Count titles directly or alongside searchable text
A field can be mapped only as token_count, or a text field can expose an additional word_count subfield with token_count. The latter preserves normal text searching while enabling an exact term query such as title.word_count = 4. section 4.5.1
Listing 4.9 Index with a token_count data type for the title field
PUT tech_books
{
"mappings": {
"properties": {
"title": { #1
"type": "token_count", #2
"analyzer": "standard" #3
}
}
}
}
Inspect the runnable mapping that assigns token_count and the standard analyzer to the title field. section 4.5.1
