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

你可能感兴趣的文章
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>