索引模板: 就是把已经创建好的某个索引的参数设置(settings)和索引映射(mapping)保存下来作为模板, 在创建新索引时, 指定要使用的模板名, 就可以直接重用已经定义好的模板中的设置和映射.
ES 6.0之后
索引模板结构
(1) settings:
指定index的配置信息, 比如分片数、副本数, tranlog同步条件、refresh策略等信息;
1.1 analysis
字符过滤器 char_filter
目前字符过滤器共分为三种:映射字符过滤器(mapping char filter)、HTML过滤器(HTML Strip char filter)、格式替换过滤器(Pattern Replace char filter)。html_strip 字符过滤器去除所有的 HTML 标签。
如下定义一个 mapping 字符过去器。将 & 替换成 and。
"char_filter": {"&_to_and": {"type": "mapping","mappings": [ "&=> and "]}}
格式替换过滤器(Pattern Replace Char Filter),将点 “.” 替换成空格。
"char_filter": {"replace_dot": {"pattern": "\\.","type": "pattern_replace","replacement": " "}}
分词器 tokenizer
常用的分词器有 standard、keyword、whitespace、pattern等。
standard 分词器将字符串分割成单独的字词,删除大部分标点符号。keyword 分词器输出和它接收到的相同的字符串,不做任何分词处理。whitespace 分词器只通过空格俩分割文本。pattern 分词器可以通过正则表达式来分割文本。最常用的一般为 standard 分词器。
更多的分词器详见官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/2.4/analysis-standard-tokenizer.html
标记过滤器 filter
常用的标记过滤器有 lowercase 和 stop 。lowercase 标记过滤器将词转换为小写,stop 标记过滤器用户去除一些自定义停用词或者是语言内定义的停用词。
"filter": {"my_stopwords": {"type": "stop","stopwords": [ "the", "a" ]}}
上述标记过滤器是自定义的过滤掉 “the” 、”a” 两词。除了自定义,还有特定的语言停用词过滤,相应的有:spanish 、english等。具体用法如下:
"filter": {"my_stop": {"type": "stop","stopwords": _spanish_}}
更多的语言停用词参考官方网址:https://www.elastic.co/guide/en/elasticsearch/reference/2.4/analysis-stop-tokenfilter.html/
分析器组合 analyzer
如上定义了字符过滤器、分词器、标记过滤器之后,就该用户自定义分析器。分析器是由三者按顺序组合而成。
将上述组件组合起来便是
"settings": {"index": {"analysis": {"char_filter": {"&_to_and": {"type": "mapping","mappings": [ "&=> and "]},"replace_dot": {"pattern": "\\.","type": "pattern_replace","replacement": " "}},"filter": {"my_stop": {"type": "stop","stopwords": _spanish_},"my_stopwords": {"type": "stop","stopwords": [ "the", "a" ]}},"analyzer": {"my_analyzer": {"type": "custom","char_filter": [ "html_strip", "&_to_and", "replace_dot" ],"tokenizer": "standard","filter": [ "lowercase", "my_stopwords", "my_stop" ]}}},...}}
(2) mappings:
指定index的内部构建信息, 主要有:
①
_all: All Field字段, 如果开启,_all字段就会把所有字段的内容都包含进来,检索的时候可以不用指定字段查询 —— 会检索多个字段, 设置方式:"_all": {"enabled": true}; 在ES 6.0开始,_all字段被禁用了, 作为替换, 可以通过copy_to自定义实现all字段的功能. ②_source: Source Field字段, ES为每个文档都保存一份源数据, 如果不开启, 也就是"_source": {"enabled": false}, 查询的时候就只会返回文档的ID, 其他的文档内容需要通过Fields字段到索引中再次获取, 效率很低. 但若开启, 索引的体积会更大, 此时就可以通过Compress进行压缩, 并通过inclueds、excludes等方式在field上进行限制 —— 指定义允许哪些字段存储到_source中, 哪些不存储;
properties:
最重要的配置, 是对索引结构和文档字段的设置.
PUT _template/shop_template?pretty{"index_patterns": ["shop*", "bar*"], //可以通过"shop*"和"bar*"来适配, template字段已过期"order": 0, // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高"settings": {"index": {"max_result_window": "200000","number_of_shards": "3", // 主分片的个数"number_of_replicas": "1" // 主分片的拷贝分片个数},"analysis": { // 自定义的分析器"analyzer": { // 用户自定义分析器"default": {"type": "custom","tokenizer": "tokenizer_default"}},"tokenizer": { // 用户自定义分词器"tokenizer_default": {"type": "ik_max_word","isNameRecognition": "true","isNumRecognition": "true","isQuantifierRecognition": "true","isRealName": "false","dic": "dic"}},"char_filter": { ... }, // 用户自定义字符过滤器"filter": { ... }, // 用户自定义标记过滤器}},"aliases": {"{index}_alias": {}},"mappings": {// ES 6.0开始只支持一种type, 名称为“_doc”"_doc": {"_source": { // 是否保存字段的原始值"enabled": false},"properties": { // 字段的映射"@timestamp": { // 具体的字段映射"type": "date","format": "yyyy-MM-dd HH:mm:ss"},"createTime": {"type": "date","format": "epoch_second"},"@version": {"doc_values": true,"index": "false", // 设置为false, 不索引"type": "text" // text类型},"logLevel": {"type": "long"}}}}}
常见操作
(1) 查看示例:
GET _template // 查看所有模板
GET _template/temp* // 查看与通配符相匹配的模板
GET _template/temp1,temp2 // 查看多个模板
GET _template/shop_template // 查看指定模板
(2) 判断模板是否存在:
判断示例:
HEAD _template/shop_tem
结果说明:
a) 如果存在, 响应结果是:
200 - OKb) 如果不存在, 响应结果是:404 - Not Found
(3)删除示例:
DELETE _template/shop_template // 删除上述创建的模板
https://my.oschina.net/davidzhang/blog/811511
给 mapping 增加字段
PUT my_index/_mapping/my_type
{
"properties": {
"字段1": {
"type": "float"
},
"字段2": {
"type": "keyword"
},
"字段3": {
"type": "text"
}
}
}
赋值
POST my_index/_update_by_query
{
"script": {
"lang": "painless",
"inline": "ctx._source.new_field_name= '02'"
}
}
参考链接:https://www.jianshu.com/p/1f67e4436c37 作者:xuxiangwork
