Elasticsearch 2.X 自定义字段的Mapping

术语

term - individual word (拆分后的最小单词)

Mapping 简介

Elasticsearch Reference [2.4] » Mapping
Mapping是用来定义文档及包含字段的保存和索引的方式。

Why

接触mapping是因为要收集除了log之外的业务信息。 业务log和系统log不同,很多的自定义字段,并将这些信息推送到单独的index。 最终目的是用过kibana的图形化的展示来统计和分析。当我们要统计比如:用户的访问排名(字段名:user:test@gmail.com)。 当没有设置任何mapping的时候,ES会采用动态mapping(Dynamic Mapping),针对String的字段默认的index方式是:analyzed。这种方式下,test@gmail.com 会被拆分成test和gmail.com(怎么拆分取决于用什么analyzer)。这样不便于统计,这里我们必须显示地去设置mapping。
Mapping parameters » index

通过kibana去选择analyzed的字段去做terms aggregation可以看到对应的warning信息

自定义mapping

可以通过API 去自定义mapping。 (这个最好在数据开始index之前,因为数据index的时候会动态设置mapping,再去修改会出现一些冲突)新增加的字段可以继续通过修改mapping来增加。 ES 支持一个index多个type,mapping可以针对单个type也可以针对index。
示例:

curl -XPUT http://localhost:9200/business-index-*/_mapping/biz -d '
{
 "properties" : {
    "uri" : {"type": "string","index" : "not_analyzed"},
    "user" : {"type": "string", "index" : "not_analyzed"},
    "keyword" : {"type": "string", "index" : "not_analyzed"},
    "responseStatus" : { "type" : "integer" },
    "responseTime" : { "type" : "long" }
 }
}';

自定义template

对于确定的index,通过mapping的方式就可以达到我们的目的。 比如: 商品的索引,这个index不会变,里面的数据document会增删改查,但是index始终在那里。
但是对于类似log和数据分析的数据,这些数据会惊人的速度增加,如果放在一个index就不现实。 所以ELK就有了 “time-based index pattern” , 通过这种方式可以每天或者每月生成一个index文件。比如logstash的日志: logstash-2016.08.20 针对这种场景,就需要引入更高一层的配置: Index Template
设定自己的template的示例如下:

curl -XPUT http://localhost:9200/_template/business -d '
{
    "template": "business*",
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "_default_": {
            "properties": {
                "uri": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "user": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "keyword": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "responseStatus": {
                    "type": "integer"
                },
                "responseTime": {
                    "type": "long"
                }
            }
        }
    }
}
';

The settings and mappings will be applied to any index name that matches the business* template

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值