索引模板: 就是把已经创建好的某个索引的参数设置(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。

  1. "char_filter": {
  2. "&_to_and": {
  3. "type": "mapping",
  4. "mappings": [ "&=> and "]
  5. }
  6. }

格式替换过滤器(Pattern Replace Char Filter),将点 “.” 替换成空格。

  1. "char_filter": {
  2. "replace_dot": {
  3. "pattern": "\\.",
  4. "type": "pattern_replace",
  5. "replacement": " "
  6. }
  7. }

分词器 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 标记过滤器用户去除一些自定义停用词或者是语言内定义的停用词。

  1. "filter": {
  2. "my_stopwords": {
  3. "type": "stop",
  4. "stopwords": [ "the", "a" ]
  5. }
  6. }

上述标记过滤器是自定义的过滤掉 “the” 、”a” 两词。除了自定义,还有特定的语言停用词过滤,相应的有:spanishenglish等。具体用法如下:

  1. "filter": {
  2. "my_stop": {
  3. "type": "stop",
  4. "stopwords": _spanish_
  5. }
  6. }

更多的语言停用词参考官方网址:https://www.elastic.co/guide/en/elasticsearch/reference/2.4/analysis-stop-tokenfilter.html/

分析器组合 analyzer

如上定义了字符过滤器、分词器、标记过滤器之后,就该用户自定义分析器。分析器是由三者按顺序组合而成。
将上述组件组合起来便是

  1. "settings": {
  2. "index": {
  3. "analysis": {
  4. "char_filter": {
  5. "&_to_and": {
  6. "type": "mapping",
  7. "mappings": [ "&=> and "]
  8. },
  9. "replace_dot": {
  10. "pattern": "\\.",
  11. "type": "pattern_replace",
  12. "replacement": " "
  13. }
  14. },
  15. "filter": {
  16. "my_stop": {
  17. "type": "stop",
  18. "stopwords": _spanish_
  19. },
  20. "my_stopwords": {
  21. "type": "stop",
  22. "stopwords": [ "the", "a" ]
  23. }
  24. },
  25. "analyzer": {
  26. "my_analyzer": {
  27. "type": "custom",
  28. "char_filter": [ "html_strip", "&_to_and", "replace_dot" ],
  29. "tokenizer": "standard",
  30. "filter": [ "lowercase", "my_stopwords", "my_stop" ]
  31. }
  32. }
  33. },
  34. ...
  35. }
  36. }

(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进行压缩, 并通过incluedsexcludes等方式在field上进行限制 —— 指定义允许哪些字段存储到_source中, 哪些不存储;

properties:

最重要的配置, 是对索引结构和文档字段的设置.

  1. PUT _template/shop_template?pretty
  2. {
  3. "index_patterns": ["shop*", "bar*"], //可以通过"shop*""bar*"来适配, template字段已过期
  4. "order": 0, // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高
  5. "settings": {
  6. "index": {
  7. "max_result_window": "200000",
  8. "number_of_shards": "3", // 主分片的个数
  9. "number_of_replicas": "1" // 主分片的拷贝分片个数
  10. },
  11. "analysis": { // 自定义的分析器
  12. "analyzer": { // 用户自定义分析器
  13. "default": {
  14. "type": "custom",
  15. "tokenizer": "tokenizer_default"
  16. }
  17. },
  18. "tokenizer": { // 用户自定义分词器
  19. "tokenizer_default": {
  20. "type": "ik_max_word",
  21. "isNameRecognition": "true",
  22. "isNumRecognition": "true",
  23. "isQuantifierRecognition": "true",
  24. "isRealName": "false",
  25. "dic": "dic"
  26. }
  27. },
  28. "char_filter": { ... }, // 用户自定义字符过滤器
  29. "filter": { ... }, // 用户自定义标记过滤器
  30. }
  31. },
  32. "aliases": {
  33. "{index}_alias": {}
  34. },
  35. "mappings": {
  36. // ES 6.0开始只支持一种type, 名称为“_doc
  37. "_doc": {
  38. "_source": { // 是否保存字段的原始值
  39. "enabled": false
  40. },
  41. "properties": { // 字段的映射
  42. "@timestamp": { // 具体的字段映射
  43. "type": "date",
  44. "format": "yyyy-MM-dd HH:mm:ss"
  45. },
  46. "createTime": {
  47. "type": "date",
  48. "format": "epoch_second"
  49. },
  50. "@version": {
  51. "doc_values": true,
  52. "index": "false", // 设置为false, 不索引
  53. "type": "text" // text类型
  54. },
  55. "logLevel": {
  56. "type": "long"
  57. }
  58. }
  59. }
  60. }
  61. }

常见操作

(1) 查看示例:

GET _template                // 查看所有模板
GET _template/temp*          // 查看与通配符相匹配的模板
GET _template/temp1,temp2    // 查看多个模板
GET _template/shop_template  // 查看指定模板

(2) 判断模板是否存在:
判断示例:

HEAD _template/shop_tem

结果说明:

a) 如果存在, 响应结果是: 200 - OK b) 如果不存在, 响应结果是: 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