博客
关于我
Elasticsearch索引模板-转载
阅读量:738 次
发布时间:2019-03-22

本文共 5143 字,大约阅读时间需要 17 分钟。

Elasticsearch索引模板:优化与实际应用

在使用Elasticsearch存储数据时,每个索引或映射类型可能都会有独特的属性。为了更好地管理和优化索引设置,我们可以通过定义索引模板来指定每个索引的分片数量、副本数量、字段是否需要分词以及其他自定义设置。这种模板化的方式能够提高数据的组织效率,同时也为日后扩展和维护提供了方便。

1. 模板的基本概念

索引模板通过指定模板名称来定义特定类型的索引属性。使用通配符(如*business-*)可以对多个索引同时应用相同的模板。例如,模板名称为test-business-*的索引将匹配像business-2017.05.01这样的索引名称。

2. 业务模板(Business Template)

以下是基于业务类型的典型索引模板示例:

{  "order": 0,  "template": "test-business-*",  "settings": {    "index": {      "routing": {        "allocation": {          "require": {            "box_type": "hot"          }        }      },      "refresh_interval": "3s",      "analysis": {        "filter": {          "camel_filter": {            "split_on_numerics": "false",            "type": "word_delimiter",            "stem_english_possessive": "false",            "generate_number_parts": "false",            "protected_words": ["iPhone", "WiFi"]          }        },        "analyzer": {          "camel_analyzer": {            "filter": ["camel_filter", "lowercase"],            "char_filter": ["html_strip"],            "type": "custom",            "tokenizer": "ik_smart"          }        },        "tokenizer": {          "my_tokenizer": {            "type": "whitespace",            "enable_lowercase": "false"          }        }      },      "number_of_shards": "30",      "number_of_replicas": "0"    }  },  "mappings": {    "_default_": {      "dynamic_templates": [        {          "message_field": {            "mapping": {              "analyzer": "camel_analyzer",              "index": "analyzed",              "omit_norms": true,              "type": "string"            },            "match_mapping_type": "string",            "match": "message"          }        },        {          "logid_field": {            "mapping": {              "type": "long"            },            "match_mapping_type": "string",            "match": "logid"          }        },        {          "string_fields": {            "mapping": {              "index": "not_analyzed",              "omit_norms": true,              "type": "string",              "fields": {                "raw": {                  "ignore_above": 256,                  "index": "not_analyzed",                  "type": "string"                }              }            },            "match_mapping_type": "string",            "match": "*"          }        }      ],      "_all": {        "omit_norms": true,        "enabled": true      },      "properties": {        "geoip": {          "dynamic": true,          "type": "object",          "properties": {            "location": {              "type": "geo_point"            }          }        },        "@version": {          "index": "not_analyzed",          "type": "string"        }      }    }  },  "aliases": {}}

3. 网络访问模板(Webaccess Template)

以下是一个适用于网络访问日志的典型索引模板示例:

{  "order": 0,  "template": "test-webaccess-*",  "settings": {    "index": {      "routing": {        "allocation": {          "require": {            "box_type": "hot"          }        }      },      "refresh_interval": "3s",      "analysis": {        "analyzer": {          "ik": {            "type": "custom",            "tokenizer": "ik_smart"          }        }      },      "number_of_shards": "20",      "number_of_replicas": "0"    }  },  "mappings": {    "_default_": {      "dynamic_templates": [        {          "message_field": {            "mapping": {              "analyzer": "ik",              "index": "analyzed",              "omit_norms": true,              "type": "string"            },            "match_mapping_type": "string",            "match": "message"          }        },        {          "bytes_field": {            "mapping": {              "type": "long"            },            "match_mapping_type": "string",            "match": "bytes"          }        },        {          "string_fields": {            "mapping": {              "index": "not_analyzed",              "omit_norms": true,              "type": "string",              "fields": {                "raw": {                  "ignore_above": 256,                  "index": "not_analyzed",                  "type": "string"                }              }            },            "match_mapping_type": "string",            "match": "*"          }        }      ],      "_all": {        "omit_norms": true,        "enabled": true      },      "properties": {        "geoip": {          "dynamic": true,          "type": "object",          "properties": {            "location": {              "type": "geo_point"            }          }        },        "@version": {          "index": "not_analyzed",          "type": "string"        }      }    }  },  "aliases": {}}

4. 模板的优化技巧

在使用索引模板时,可以通过以下方式优化其性能和可读性:

  • 分片和副本的配置:根据具体需求调整number_of_shardsnumber_of_replicas的值。通常情况下,生产环境可以设置较高的副本数量,以确保数据的冗余和高可用性。

  • 分析器和分词器的定制:根据实际业务需求对分析器和分词器进行调整。例如,ik分析器非常适合处理中文文本,能够更好地提取有意义的关键词。

  • 动态映射的优化:通过定义dynamic_templates来为特定的字段设置定制化映射,避免不必要的分析和存储开销。

  • 地理IP信息的处理:在properties中添加geoip字段,能够方便地对日志中的地理位置信息进行存储和检索。

  • 版本控制:通过设置@version字段,可以记录文档的创建时间和更新时间,这对于数据的审计和追溯具有重要意义。

通过合理设计和优化索引模板,可以显著提升Elasticsearch的性能表现,并更好地满足实际业务需求。在实际应用中,建议根据具体场景对模板进行调整和定制,以达到最佳效果。

转载地址:http://wqzwk.baihongyu.com/

你可能感兴趣的文章
npm设置源地址,npm官方地址
查看>>
npm设置镜像如淘宝:http://npm.taobao.org/
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
NPM酷库052:sax,按流解析XML
查看>>
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>
NPOI
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI初级教程
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NPOI在Excel中插入图片
查看>>
NPOI将某个程序段耗时插入Excel
查看>>
NPOI格式设置
查看>>
NPOI设置单元格格式
查看>>
Npp删除选中行的Macro录制方式
查看>>
NR,NF,FNR
查看>>
nrf24l01+arduino
查看>>
nrf开发笔记一开发软件
查看>>
nrm —— 快速切换 NPM 源 (附带测速功能)
查看>>