如何在 ElasticSearch 中创建自定义可重用类型?

How to create a custom reusable type in ElasticSearch?

我的 ElasticSearch 架构的 json 如下所示:-

{
  "mappings": {
    "properties": {
      "DESCRIPTION_FR": {
        "type": "text",
        "analyzer": "french"
      },
      "FEEDBACK_FR": {
        "type": "text",
        "analyzer": "french"
      },
      "SOURCE_FR": {
        "type": "text",
        "analyzer": "french"
      }
    }
  }
}

有 100 个这样的属性。使用这种方法在所有属性中复制更改是多余且错误的。

ElasticSearch 7.2 中有没有一种方法可以编写自定义数据类型并在 属性 映射中重用它。

{
  "settings": {
  //definition of custom type "text_fr"
 },
  "mappings": {
    "properties": {
      "DESCRIPTION_FR": {
        "type": "text_fr"
      },
      "FEEDBACK_FR": {
        "type": "text_fr"
      },
      "SOURCE_FR": {
        "type": "text_fr"
      }
    }
  }
}

是的!你要找的是 dynamic mapping templates. More specifically the match feature.

  1. 定义带有前导通配符的目标字段名称:
PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "is_french_text": {
          "match_mapping_type": "*",
          "match": "*_FR",
          "mapping": {
            "type": "text",
            "analyzer": "french"
          }
        }
      }
    ]
  }
}
  1. 插入文档:
POST my_index/_doc
{
  "DESCRIPTION_FR": "je",
  "FEEDBACK_FR": "oui",
  "SOURCE_FR": "je ne sais quoi"
}
  1. 验证动态生成的映射:
GET my_index/_mapping