Dynamic templates support default types?

懵懂的女人 提交于 2020-07-22 06:03:32

问题


Dynamic templates allow you to define custom mappings that can be applied to dynamically added fields based on:

  • the datatype detected by Elasticsearch, with match_mapping_type.
  • the name of the field, with match and unmatch or match_pattern.
  • the full dotted path to the field, with path_match and path_unmatch.

I was trying to have a default type keyword for all fields while some special fields with specific *Suffix or prefix* could have specified types as follows, but it turned out all fields will be keyword in the end unexpectedly.

{
  "order": 99,
  "index_patterns": [
    "xxxx_stats_*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "6",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "_doc": {
      "dynamic": true,
      "_source": {
        "enabled": true
      },
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "*",
            "unmatch": [
              "*Time",
              "*At",
              "is*"
            ],
            "mapping": {
              "ignore_above": 256,
              "null_value": "NULL",
              "type": "keyword"
            }
          }
        },
        {
          "timeSuffix": {
            "match_mapping_type": "*",
            "match": [
              "*Time",
              "*At"
            ],
            "mapping": {
              "type": "long"
            }
          }
        },
        {
          "isPrefix": {
            "match_mapping_type": "*",
            "match": "is*",
            "mapping": {
              "type": "boolean"
            }
          }
        }
      ],
      "date_detection": false,
      "numeric_detection": true
    }
  },
  "aliases": {
    "{index}-alias": {
      
    }
  }
}

回答1:


AFAIK match and unmatch cannot be arrays, only strings or regexes. So try this:

{
  "dynamic_templates":[
    {
      "timeSuffix":{
        "match_mapping_type":"*",
        "match_pattern":"regex",
        "match":"^(.*Time)|(.*At)$",
        "mapping":{
          "type":"long"
        }
      }
    },
    {
      "isPrefix":{
        "match_mapping_type":"*",
        "match":"is*",
        "mapping":{
          "type":"boolean"
        }
      }
    },
    {
      "strings":{
        "match_mapping_type":"*",
        "mapping":{
          "ignore_above":256,
          "null_value":"NULL",
          "type":"keyword"
        }
      }
    }
  ]
}

I also find that when you move strings to the bottom, the 2 mappings above will be resolved first. Otherwise, since every segment includes match_mapping_type":"*", the first matching segment will apply. This issue may be related.



来源:https://stackoverflow.com/questions/62127673/dynamic-templates-support-default-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!