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_keywordfor keyword fields that always contain the same value. -
wildcardfor unstructured machine-generated content. Thewildcardtype is optimized for fields with large values or high cardinality.
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:
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
properties: {
tags: {
type: 'keyword'
}
}
}
}
)
puts response
res, err := es.Indices.Create(
"my-index-000001",
es.Indices.Create.WithBody(strings.NewReader(`{
"mappings": {
"properties": {
"tags": {
"type": "keyword"
}
}
}
}`)),
)
fmt.Println(res, err)
PUT my-index-000001
{
"mappings": {
"properties": {
"tags": {
"type": "keyword"
}
}
}
}
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
rangequeries. -
Fast retrieval is important.
termquery searches onkeywordfields are often faster thantermsearches 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:
-
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) orfalse. -
eager_global_ordinals -
Should global ordinals be loaded eagerly on refresh? Accepts
trueorfalse(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
2147483647so that all values would be accepted. Please however note that default dynamic mapping rules create a subkeywordfield that overrides this default by settingignore_above: 256. -
index -
Should the field be quickly searchable? Accepts
true(default) andfalse.keywordfields that only havedoc_valuesenabled can still be queried, albeit slower. -
index_options -
What information should be stored in the index, for scoring purposes.
Defaults to
docsbut can also be set tofreqsto take term frequency into account when computing scores. -
meta - Metadata about the field.
-
norms -
Whether field-length should be taken into account when scoring queries.
Accepts
trueorfalse(default). -
null_value -
Accepts a string value which is substituted for any explicit
nullvalues. Defaults tonull, which means the field is treated as missing. Note that this cannot be set if thescriptvalue is used. -
on_script_error -
Defines what to do if the script defined by the
scriptparameter throws an error at indexing time. Acceptsfail(default), which will cause the entire document to be rejected, andcontinue, which will register the field in the document’s_ignoredmetadata field and continue indexing. This parameter can only be set if thescriptfield is also set. -
script -
If this parameter is set, then the field will index values generated
by this script, rather than reading the values directly from the
source. If a value is set for this field on the input document, then
the document will be rejected with an error.
Scripts are in the same format as their
runtime equivalent. Values emitted by the
script are normalized as usual, and will be ignored if they are longer
that the value set on
ignore_above. -
store -
Whether the field value should be stored and retrievable separately from
the
_sourcefield. Acceptstrueorfalse(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
trueorfalse(default). -
time_series_dimension -
[preview] This functionality is in technical preview and may be changed or removed in a future release. Elastic will apply best effort to fix any issues, but features in technical preview are not subject to the support SLA of official GA features. (Optional, Boolean)
For internal use by Elastic only.
Marks the field as a time series dimension. Defaults to
false.The
index.mapping.dimension_fields.limitindex setting limits the number of dimensions in an index.Dimension fields have the following constraints:
-
The
doc_valuesandindexmapping parameters must betrue. - Field values cannot be an array or multi-value.
- Field values cannot be larger than 1024 bytes.
-
The field cannot use a
normalizer.
-
The
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.
PUT logs-debug
{
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"message": {
"type": "text"
},
"level": {
"type": "constant_keyword",
"value": "debug"
}
}
}
}
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:
POST logs-debug/_doc
{
"date": "2019-12-12",
"message": "Starting up Elasticsearch",
"level": "debug"
}
POST logs-debug/_doc
{
"date": "2019-12-12",
"message": "Starting up Elasticsearch"
}
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:
|
Metadata about the field. |
|
|
|
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
The wildcard field type is a specialized keyword field for unstructured
machine-generated content you plan to search using grep-like
wildcard and regexp
queries. The wildcard type is optimized for fields with large values or high
cardinality.
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. If the
field values share many prefixes, such as URLs for the same website, storage
costs for a wildcard field may be higher than an equivalent keyword field.
You index and search a wildcard field as follows
PUT my-index-000001
{
"mappings": {
"properties": {
"my_wildcard": {
"type": "wildcard"
}
}
}
}
PUT my-index-000001/_doc/1
{
"my_wildcard" : "This string can be quite lengthy"
}
GET my-index-000001/_search
{
"query": {
"wildcard": {
"my_wildcard": {
"value": "*quite*lengthy"
}
}
}
}
Parameters for wildcard fields
The following parameters are accepted by wildcard fields:
|
Accepts a string value which is substituted for any explicit |
|
|
Do not index any string longer than this value. Defaults to |
Limitations
-
wildcardfields are untokenized like keyword fields, so do not support queries that rely on word positions such as phrase queries. -
When running
wildcardqueries anyrewriteparameter is ignored. The scoring is always a constant score.