這兩天一直在看elasticsearch相關的內容,看到mapping這一塊,就折騰了下。php
通常狀況下,咱們不須要對elasticsearch的mapping進行設置,但若是但願對索引使用自定義的管理,那麼就須要瞭解這一塊的內容了。這裏是我在Logstash中對elasticsearch的設置.html
這裏是Logstash的配置:json
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"
}
}
}app
這裏注意:index,template_name,templateelasticsearch
index: 在elasticsearch上索引的名字ide
template_name: 使用elasticsearch上_template下的模板(與template關聯不大)ui
template:在_template下生成模板。spa
下面是test.json:orm
{
"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