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

你可能感兴趣的文章
Objective-C实现collatz sequence考拉兹序列算法(附完整源码)
查看>>
Objective-C实现Collatz 序列算法(附完整源码)
查看>>
Objective-C实现comb sort梳状排序算法(附完整源码)
查看>>
Objective-C实现combinationSum组合和算法(附完整源码)
查看>>
Objective-C实现combinations排列组合算法(附完整源码)
查看>>
Objective-C实现combine With Repetitions结合重复算法(附完整源码)
查看>>
Objective-C实现combine Without Repetitions不重复地结合算法(附完整源码)
查看>>
Objective-C实现conjugate gradient共轭梯度算法(附完整源码)
查看>>
Objective-C实现connected components连通分量算法(附完整源码)
查看>>
Objective-C实现Connected Components连通分量算法(附完整源码)
查看>>
Objective-C实现Convex hull凸包问题算法(附完整源码)
查看>>
Objective-C实现convolution neural network卷积神经网络算法(附完整源码)
查看>>
Objective-C实现convolve卷积算法(附完整源码)
查看>>
Objective-C实现coulombs law库仑定律算法(附完整源码)
查看>>
Objective-C实现counting sort计数排序算法(附完整源码)
查看>>
Objective-C实现countSetBits设置位的数量算法(附完整源码)
查看>>
Objective-C实现currency converter货币换算算法(附完整源码)
查看>>
Objective-C实现cycle sort循环排序算法(附完整源码)
查看>>
Objective-C实现data transformations数据转换算法(附完整源码)
查看>>
Objective-C实现datamatrix二维码识别 (附完整源码)
查看>>