弹性搜索索引模板是否可以为字段定义默认类型,并仅允许覆盖特定字段?

Can an elastic search index template define a default type for fields, and allow overriding specific fields only?

我正在映射传递的文档中的所有字段,但有时弹性搜索的动态字段映射类型检测会为包含纯文本的字段选择非常严格的类型(如 long)。这似乎是基于 ES 收到的包含该字段的初始文档。例如:

文档 1:

{
  "a": 1,
  "b": "foo",
  "c": {
    "nested": 5.5
  }
}

文档 2:

{
  "a": "plain text",
  "b": "bar",
  "c": {
    "nested": "plain text again"
  }
}

我能否定义一个索引模板,使所有值默认都被索引为 text,并且仅在我明确想要这样做时才被索引为其他类型?

Elasticsearch 允许自定义动态映射规则。 例如,您可以定义将 long 映射到 text:

的规则
{
  "mappings": {
    "dynamic_templates": [
      {
        "long_to_text": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "text"
          }
        }
      }
    ]
}

可以在文档中找到更多详细信息:https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html