elasticsearch mapping简单介绍

 这两天一直在看elasticsearch相关的内容,看到mapping这一块,就折腾了下。

一般情况下,我们不需要对elasticsearch的mapping进行设置,但如果希望对索引使用自定义的管理,那么就需要了解这一块的内容了。这里是我在Logstash中对elasticsearch的设置.

这里是Logstash的配置:

output {

  if [type] == "test" {
    elasticsearch {
      hosts => ["10.1.0.12:9200"]
        index => "test"
      document_type => "%{[@metadata][type]}"
      manage_template => true
      template_overwrite => true
      template_name => "json_php_mapping"
      template => "/usr/local/services/logstash/templates/test.json"
}
}
}

这里注意:index,template_name,template

    index:  在elasticsearch上索引的名字

    template_name: 使用elasticsearch上_template下的模板(与template关联不大)

    template:在_template下生成模板。

下面是test.json:

{
  "template": "test",
  "settings":{
  "index.number_of_shards": 5,
  "number_of_replicas": 1,
  "index.refresh_interval":"60s"
  },
  "mappings" : {
    "_default_": {
    "_all": {"enabled": false},
    "dynamic": true,
    "date_detection": true,
    "dynamic_date_formats": [
    "date_optional_time||epoch_millis||epoch_second",
    "yyyy-MM-dd'T'HH:mm:ssZZ.SSS||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm:ss.SSS"
    ],
    "dynamic_templates": [{
      "date":{
        "match_mapping_type": "date",
        "match": "date_*",
        "mapping":{
          "type": "date"
          }
        }
     },{
      "strings": {
        "match_mapping_type": "string",
        "match": "s_*",
        "mapping":{
        "type": "keyword",
        "ignore_above": 256
         }
        }
     },{
      "long": {
        "match_mapping_type":"*",
        "match": "l_*",
        "mapping":{
          "type": "integer"
           }
          }
      },
      {
      "double": {
        "match_mapping_type": "*",
        "match": "d_*",
        "mapping": {
        "type": "double"
        }
      }
     },
      {
        "analyzer": {
          "match": "t_*",
          "mapping": {
          "type": "text",
            "index": true,
            "analyzer": "english"
            }
          }
      },
      {
        "ip": {
          "match_mapping_type": "*",
          "match": "ip_*",
          "mapping": {
            "type": "ip"
            }
           }
        }
       ]
      }
    }
}

参数解释:

  template: 这里是模板的名字,可以在kibana中使用:GET _template 来查看es下的所有模板

  settings: 一般设置分片。 index.refresh_interval 一般是用户将segments刷新到磁盘缓存的时间间隔,默认是1s

  _default_:  index中的type中使用默认的这个mapping.注意:在6.0版本后特性被移除

  dynamic: 开启动态模板

  date_detection: 打开时间格式的识别

  dynamic_date_formats: 可以识别的时间格式

  dynamic_templates下定义: 

  "dynamic_templates": [
    {
      "my_template_name": { 
        ...  match conditions ... 
        "mapping": { ... } 
      }
    },
    ...
  ]

  my_template_name:就是给下面match取得名字,没什么作用。

  match conditions: (https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html)

    1. match_mapping_type :匹配字段的类型。通过类型匹配字段

    2. match: 匹配字段,通过正则匹配字段

    3. match_pattern: 一般与上面的match使用,用于正则

 还有一些插入时间es自动生成的,如@timestamp,_type,version等等,差不多就这样了。

  官网关于这一块介绍的很容易理解,建议看官网,下面的连接是本文中最关键的:https://www.elastic.co/guide/en/elasticsearch/reference

 

   

原文地址:https://www.cnblogs.com/si-jie/p/8137712.html