Keyword type family

The keyword family includes the following field types:

  • keyword, which is used for structured content such as IDs, email addresses, hostnames, status codes, zip codes, or tags.
  • constant_keyword for keyword fields that always contain the same value.
  • wildcard, which optimizes log lines and similar keyword values for grep-like wildcard queries.

Keyword fields are often used in sorting, aggregations, and term-level queries, such as term.

Avoid using keyword fields for full-text search. Use the text field type instead.

Keyword field type

Below is an example of a mapping for a basic keyword field:

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "tags": {
  6. "type": "keyword"
  7. }
  8. }
  9. }
  10. }

Mapping numeric identifiers

Not all numeric data should be mapped as a numeric field data type. Elasticsearch optimizes numeric fields, such as integer or long, for range queries. However, keyword fields are better for term and other term-level queries.

Identifiers, such as an ISBN or a product ID, are rarely used in range queries. However, they are often retrieved using term-level queries.

Consider mapping a numeric identifier as a keyword if:

  • You don’t plan to search for the identifier data using range queries.
  • Fast retrieval is important. term query searches on keyword fields are often faster than term searches on numeric fields.

If you’re unsure which to use, you can use a multi-field to map the data as both a keyword and a numeric data type.

Parameters for basic keyword fields

The following parameters are accepted by keyword fields:

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.

eager_global_ordinals

Should global ordinals be loaded eagerly on refresh? Accepts true or false (default). Enabling this is a good idea on fields that are frequently used for terms aggregations.

fields

Multi-fields allow the same string value to be indexed in multiple ways for different purposes, such as one field for search and a multi-field for sorting and aggregations.

ignore_above

Do not index any string longer than this value. Defaults to 2147483647 so that all values would be accepted. Please however note that default dynamic mapping rules create a sub keyword field that overrides this default by setting ignore_above: 256.

index

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

index_options

What information should be stored in the index, for scoring purposes. Defaults to docs but can also be set to freqs to take term frequency into account when computing scores.

norms

Whether field-length should be taken into account when scoring queries. Accepts true or false (default).

null_value

Accepts a string value 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).

similarity

Which scoring algorithm or similarity should be used. Defaults to BM25.

normalizer

How to pre-process the keyword prior to indexing. Defaults to null, meaning the keyword is kept as-is.

split_queries_on_whitespace

Whether full text queries should split the input on whitespace when building a query for this field. Accepts true or false (default).

meta

Metadata about the field.

Constant keyword field type

Constant keyword is a specialization of the keyword field for the case that all documents in the index have the same value.

  1. PUT logs-debug
  2. {
  3. "mappings": {
  4. "properties": {
  5. "@timestamp": {
  6. "type": "date"
  7. },
  8. "message": {
  9. "type": "text"
  10. },
  11. "level": {
  12. "type": "constant_keyword",
  13. "value": "debug"
  14. }
  15. }
  16. }
  17. }

constant_keyword supports the same queries and aggregations as keyword fields do, but takes advantage of the fact that all documents have the same value per index to execute queries more efficiently.

It is both allowed to submit documents that don’t have a value for the field or that have a value equal to the value configured in mappings. The two below indexing requests are equivalent:

  1. POST logs-debug/_doc
  2. {
  3. "date": "2019-12-12",
  4. "message": "Starting up Elasticsearch",
  5. "level": "debug"
  6. }
  7. POST logs-debug/_doc
  8. {
  9. "date": "2019-12-12",
  10. "message": "Starting up Elasticsearch"
  11. }

However providing a value that is different from the one configured in the mapping is disallowed.

In case no value is provided in the mappings, the field will automatically configure itself based on the value contained in the first indexed document. While this behavior can be convenient, note that it means that a single poisonous document can cause all other documents to be rejected if it had a wrong value.

Before a value has been provided (either through the mappings or from a document), queries on the field will not match any documents. This includes exists queries.

The value of the field cannot be changed after it has been set.

Parameters for constant keyword fields

The following mapping parameters are accepted:

meta

Metadata about the field.

value

The value to associate with all documents in the index. If this parameter is not provided, it is set based on the first document that gets indexed.

Wildcard field type

A wildcard field stores values optimised for wildcard grep-like queries. Wildcard queries are possible on other field types but suffer from constraints:

  • text fields limit matching of any wildcard expressions to individual tokens rather than the original whole value held in a field
  • keyword fields are untokenized but slow at performing wildcard queries (especially patterns with leading wildcards).

Internally the wildcard field indexes the whole field value using ngrams and stores the full string. The index is used as a rough filter to cut down the number of values that are then checked by retrieving and checking the full values. This field is especially well suited to run grep-like queries on log lines. Storage costs are typically lower than those of keyword fields but search speeds for exact matches on full terms are slower.

You index and search a wildcard field as follows

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "my_wildcard": {
  6. "type": "wildcard"
  7. }
  8. }
  9. }
  10. }
  11. PUT my-index-000001/_doc/1
  12. {
  13. "my_wildcard" : "This string can be quite lengthy"
  14. }
  15. GET my-index-000001/_search
  16. {
  17. "query": {
  18. "wildcard": {
  19. "my_wildcard": {
  20. "value": "*quite*lengthy"
  21. }
  22. }
  23. }
  24. }

Parameters for wildcard fields

The following parameters are accepted by wildcard fields:

ignore_above

Do not index any string longer than this value. Defaults to 2147483647 so that all values would be accepted.

Limitations

  • wildcard fields are untokenized like keyword fields, so do not support queries that rely on word positions such as phrase queries.