2017-04-05 54 views
0

我的索引中有许多字段名称以_count结尾的字段(例如page_count,order_count等),我总是希望它们是long。我试图创建了我认为是默认映射如下:根据通配符设置默认字段类型

{ 
    "mappings": { 
    "_default_": { 
     "_all": { 
     "enabled": false, 
     "norms": { 
      "enabled": false 
     } 
     }, 
     "properties": { 
     "*_count": { 
      "type": "long" 
     } 
     } 
    } 
    }, 
    "settings": { 
    "index.query.default_field": "message", 
    "number_of_replicas": 2, 
    "number_of_shards": 3 
    }, 
    "template": "core-app-*" 
} 

然而,这似乎并没有工作,我现在有字符串字段在我最近的指数:

"page_count":{ 
    "type":"text", 
    "fields":{ 
    "keyword":{ 
     "type":"keyword", 
     "ignore_above":256 
    } 
    } 
} 

是这是基于通配符创建映射的正确方法?我假设不是因为它似乎没有工作... :)

回答

0

您可以通过在elasticsearch中使用dynamic templates功能来实现它。

PUT _template/core-app-template 
{ 
    "template": "core-app-*", 
    "settings": { 
    "index.query.default_field": "message", 
    "number_of_replicas": 2, 
    "number_of_shards": 3 
    }, 
    "mappings": { 
    "_default_": { 
     "_all": { 
     "enabled": false, 
     "norms": { 
     "enabled": false 
     } 
    } 
    }, 
    "my_type": { 
     "dynamic_templates": [ 
     { 
      "_count_as_long": { 
      "match_mapping_type": "*", 
      "match": "*_count", 
      "mapping": { 
       "type": "long" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

注:注意index_type在上面的例子中我把自由将其定义为my_type所以当你创建这个index template使用您的实际index_type代替my_type