原文地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/constant-keyword.html, 原文档版权归 www.elastic.co 所有

Constant keyword datatypeedit

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 fieldsedit

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.