本文共 5143 字,大约阅读时间需要 17 分钟。
Elasticsearch索引模板:优化与实际应用
在使用Elasticsearch存储数据时,每个索引或映射类型可能都会有独特的属性。为了更好地管理和优化索引设置,我们可以通过定义索引模板来指定每个索引的分片数量、副本数量、字段是否需要分词以及其他自定义设置。这种模板化的方式能够提高数据的组织效率,同时也为日后扩展和维护提供了方便。
索引模板通过指定模板名称来定义特定类型的索引属性。使用通配符(如*或business-*)可以对多个索引同时应用相同的模板。例如,模板名称为test-business-*的索引将匹配像business-2017.05.01这样的索引名称。
以下是基于业务类型的典型索引模板示例:
{ "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": {}} 以下是一个适用于网络访问日志的典型索引模板示例:
{ "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": {}} 在使用索引模板时,可以通过以下方式优化其性能和可读性:
分片和副本的配置:根据具体需求调整number_of_shards和number_of_replicas的值。通常情况下,生产环境可以设置较高的副本数量,以确保数据的冗余和高可用性。
分析器和分词器的定制:根据实际业务需求对分析器和分词器进行调整。例如,ik分析器非常适合处理中文文本,能够更好地提取有意义的关键词。
动态映射的优化:通过定义dynamic_templates来为特定的字段设置定制化映射,避免不必要的分析和存储开销。
地理IP信息的处理:在properties中添加geoip字段,能够方便地对日志中的地理位置信息进行存储和检索。
版本控制:通过设置@version字段,可以记录文档的创建时间和更新时间,这对于数据的审计和追溯具有重要意义。
通过合理设计和优化索引模板,可以显著提升Elasticsearch的性能表现,并更好地满足实际业务需求。在实际应用中,建议根据具体场景对模板进行调整和定制,以达到最佳效果。
转载地址:http://wqzwk.baihongyu.com/