博客
关于我
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/

你可能感兴趣的文章
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
Nuget~管理自己的包包
查看>>
nullnullHuge Pages
查看>>
NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
查看>>
numpy 用法
查看>>
Numpy如何使用np.umprod重写range函数中i的python
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>
OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>
OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
查看>>
OAuth2.0_授权服务配置_客户端详情配置_Spring Security OAuth2.0认证授权---springcloud工作笔记142
查看>>
OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
查看>>
OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
查看>>
OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
查看>>
OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
查看>>
oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
查看>>
OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
查看>>