当前位置:   article > 正文

ElasticSearch 的使用-整体方向和调研笔记_index write (api)]

index write (api)]

目录

ElasticSearch 的使用-整体方向和调研笔记

介绍

ElasticSearch

安装

启动

创建索引

插件管理

使用

Kibana

下载

启动

汉化

实际运用的思考

ElasticSearch 架构

总结

ElasticSearch Server端

ElasticSearch Client 端

问题记录

其他相关笔记


ElasticSearch 的使用-整体方向和调研笔记

介绍

官网

中文手册:https://elasticsearch.cn ElasticSearch 搜索引擎及索引建立引擎 kibana 可视化管理工具

ElasticSearch

安装

下载:5.5.3版本 解压即可。

启动

执行bin/elasticsearch 即可。如果想后端运行,那么执行bin/elasticserach -d

###

创建索引

让我们在集群中唯一一个空节点上创建一个叫做blogs的索引。默认情况下,一个索引被分配5个主分片,但是为了演示的目的,我们只分配3个主分片和一个复制分片(每个主分片都有一个复制分片):

  1. PUT /blogs
  2. {
  3. "settings" : {
  4. "number_of_shards" : 3,
  5. "number_of_replicas" : 1
  6. }
  7. }

插件管理

  • 中文分词插件 IK analyzer 插件官网 安装步骤: ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip 参考:Elasticsearch5.x安装IK分词器以及使用

  • 拼音分词器插件 插件官网 安装步骤: ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v5.5.3/elasticsearch-analysis-pinyin-5.5.3.zip

测试: 5.X版本:

  1. POST http://localhost:9200/_analyze?analyzer=pinyin&pretty=true
  2. BOYD {"text":"这里是好记性不如烂笔头感叹号的博客们"}

6.x版本:6.0版本移除了_analyze的analyzer参数支持,analyzer测试需要在Body中关键字指定 参考:6.3 Testing analyzers

  1. POST http://localhost:9200/_analyze
  2. BOYD
  3. {
  4. "analyzer": "pinyin",
  5. "text": "这里是好记性不如烂笔头感叹号的博客们"
  6. }

测试结果:

  1. {
  2. "tokens": [
  3. {
  4. "token": "zhe",
  5. "start_offset": 0,
  6. "end_offset": 0,
  7. "type": "word",
  8. "position": 0
  9. },
  10. {
  11. "token": "zlshjxbrlbtgthdb",
  12. "start_offset": 0,
  13. "end_offset": 0,
  14. "type": "word",
  15. "position": 0
  16. },
  17. {
  18. "token": "li",
  19. "start_offset": 0,
  20. "end_offset": 0,
  21. "type": "word",
  22. "position": 1
  23. },
  24. {
  25. "token": "shi",
  26. "start_offset": 0,
  27. "end_offset": 0,
  28. "type": "word",
  29. "position": 2
  30. },
  31. {
  32. "token": "hao",
  33. "start_offset": 0,
  34. "end_offset": 0,
  35. "type": "word",
  36. "position": 3
  37. },
  38. {
  39. "token": "ji",
  40. "start_offset": 0,
  41. "end_offset": 0,
  42. "type": "word",
  43. "position": 4
  44. },
  45. {
  46. "token": "xing",
  47. "start_offset": 0,
  48. "end_offset": 0,
  49. "type": "word",
  50. "position": 5
  51. },
  52. {
  53. "token": "bu",
  54. "start_offset": 0,
  55. "end_offset": 0,
  56. "type": "word",
  57. "position": 6
  58. },
  59. {
  60. "token": "ru",
  61. "start_offset": 0,
  62. "end_offset": 0,
  63. "type": "word",
  64. "position": 7
  65. },
  66. {
  67. "token": "lan",
  68. "start_offset": 0,
  69. "end_offset": 0,
  70. "type": "word",
  71. "position": 8
  72. },
  73. {
  74. "token": "bi",
  75. "start_offset": 0,
  76. "end_offset": 0,
  77. "type": "word",
  78. "position": 9
  79. },
  80. {
  81. "token": "tou",
  82. "start_offset": 0,
  83. "end_offset": 0,
  84. "type": "word",
  85. "position": 10
  86. },
  87. {
  88. "token": "gan",
  89. "start_offset": 0,
  90. "end_offset": 0,
  91. "type": "word",
  92. "position": 11
  93. },
  94. {
  95. "token": "tan",
  96. "start_offset": 0,
  97. "end_offset": 0,
  98. "type": "word",
  99. "position": 12
  100. },
  101. {
  102. "token": "hao",
  103. "start_offset": 0,
  104. "end_offset": 0,
  105. "type": "word",
  106. "position": 13
  107. },
  108. {
  109. "token": "de",
  110. "start_offset": 0,
  111. "end_offset": 0,
  112. "type": "word",
  113. "position": 14
  114. },
  115. {
  116. "token": "bo",
  117. "start_offset": 0,
  118. "end_offset": 0,
  119. "type": "word",
  120. "position": 15
  121. },
  122. {
  123. "token": "ke",
  124. "start_offset": 0,
  125. "end_offset": 0,
  126. "type": "word",
  127. "position": 16
  128. },
  129. {
  130. "token": "men",
  131. "start_offset": 0,
  132. "end_offset": 0,
  133. "type": "word",
  134. "position": 17
  135. }
  136. ]
  137. }

使用

索引

创建索引

  • 创建默认配置的索引
    1. #请求
    2. PUT twitter
    3. #响应
    4. {
    5. "acknowledged": true,
    6. "shards_acknowledged": true,
    7. "index": "test-base"
    8. }
    9. #默认配置的索引的配置如下
    10. {
    11. "twitter": {
    12. "aliases": {},
    13. "mappings": {},
    14. "settings": {
    15. "index": {
    16. "creation_date": "1539050228177",
    17. "number_of_shards": "5",
    18. "number_of_replicas": "1",
    19. "uuid": "x-yRLPprS_mMqe_yXcoibQ",
    20. "version": {
    21. "created": "6030299"
    22. },
    23. "provided_name": "twitter"
    24. }
    25. }
    26. }
    27. }
  • 使用指定配置创建索引
    1. #请求
    2. PUT twitter
    3. {
    4. "settings" : {
    5. "index" : {
    6. "number_of_shards" : 3,
    7. "number_of_replicas" : 2
    8. }
    9. }
    10. }
    11. #响应
    12. {
    13. "acknowledged": true,
    14. "shards_acknowledged": true,
    15. "index": "twitter"
    16. }
  • 配置可以使用简单配置方式
    1. #请求
    2. PUT twitter
    3. {
    4. "settings" : {
    5. "number_of_shards" : 3,
    6. "number_of_replicas" : 2
    7. }
    8. }
  • Mappings(映射) Mapping为限定在索引中存储的数据的结构,只有符合Mapping的数据才能存储到Index中,在创建索引时也可可以指定索引的映射。
    1. #请求
    2. PUT test
    3. {
    4. "settings" : {
    5. "number_of_shards" : 1
    6. },
    7. "mappings" : {
    8. "type1" : {
    9. "properties" : {
    10. "field1" : { "type" : "text" }
    11. }
    12. }
    13. }
    14. }
    15. #结果
    16. {
    17. "test": {
    18. "aliases": {},
    19. "mappings": {
    20. "type1": {
    21. "properties": {
    22. "field1": {
    23. "type": "text"
    24. }
    25. }
    26. }
    27. },
    28. "settings": {
    29. "index": {
    30. "creation_date": "1539051572751",
    31. "number_of_shards": "1",
    32. "number_of_replicas": "1",
    33. "uuid": "ioXgmR5ISiy76HLrZCjc0w",
    34. "version": {
    35. "created": "6030299"
    36. },
    37. "provided_name": "test"
    38. }
    39. }
    40. }
    41. }

查询索引

  1. #请求
  2. GET twitter
  3. #响应
  4. {
  5. "twitter": {
  6. "aliases": {},
  7. "mappings": {},
  8. "settings": {
  9. "index": {
  10. "creation_date": "1539050228177",
  11. "number_of_shards": "5",
  12. "number_of_replicas": "1",
  13. "uuid": "x-yRLPprS_mMqe_yXcoibQ",
  14. "version": {
  15. "created": "6030299"
  16. },
  17. "provided_name": "twitter"
  18. }
  19. }
  20. }
  21. }

删除索引

  1. #请求
  2. DELETE twitter
  3. #响应
  4. {
  5. "acknowledged": true
  6. }

检测索引是否存在

  1. #请求
  2. HEAD twitter
  3. #响应
  4. ##存在
  5. 200 - OK
  6. ##不存在
  7. 404 - Not Found

关闭/打开索引

  • 打开索引
    1. #请求
    2. POST /my_index/_open
    3. #响应
    4. {
    5. "acknowledged": true,
    6. "shards_acknowledged": true
    7. }
  • 关闭索引
    1. #请求
    2. POST /my_index/_close
    3. #响应
    4. {
    5. "acknowledged": true
    6. }

对于已经关闭的索引在集群上基本没有开销(除了维护元数据),已经关闭的索引不允许进行读写操作。

可以打开或关闭多个索引,如果有索引不存在,则会报错,可以通过使用ignore_unavailable=true参数忽略索引不存在的异常。

  1. #请求
  2. POST /my_index2,my_index/_close?ignore_unavailable=true

修改索引

  • 设置索引为只读
    1. PUT /my_source_index/_settings
    2. {
    3. "settings": {
    4. "index.blocks.write": true
    5. }
    6. }
    7. # 之后的写请求的返回
    8. {
    9. "error": {
    10. "root_cause": [
    11. {
    12. "type": "cluster_block_exception",
    13. "reason": "blocked by: [FORBIDDEN/8/index write (api)];"
    14. }
    15. ],
    16. "type": "cluster_block_exception",
    17. "reason": "blocked by: [FORBIDDEN/8/index write (api)];"
    18. },
    19. "status": 403
    20. }
  • 取消索引为只读
    1. PUT /test/_settings
    2. {
    3. "settings": {
    4. "index.blocks.write": false
    5. }
    6. }
  • 收缩索引 参考:Shrink Index

  • 拆分索引 参考: Split Index

  • 索引自动归档 将别名指定的索引按照序号或日期格式,在满足指定条件时,自动创建新的索引,并将别名关联到新的索引。类似于日志文件自动归档。参考Rollover Index

设置索引的Mappings

只允许新增字段到索引Mappings或者修改已有的字段的搜索设置。

对于Object类型的字段,可以添加新的子字段。

参考: PUT Mappings properties Mapping parameters Field datatypes

索引的配置信息

参考:index-modules 索引的配置分为两部分,一部分为静态的,一部分为动态的。

  • 静态的(static) 在创建时指定,之后就不能修改。
  • 动态的(dynamic) 可以通过更新索引配置API修改。

注:更改已关闭索引上的静态或动态索引设置可能会导致不正确的设置,如果不删除并重新创建索引,则无法纠正这些设置。

  • 静态索引配置:
    • index.number_of_shards
    • index.shard.check_on_startup
    • index.codec
    • index.routing_partition_size
  • 动态索引配置:
    • index.number_of_replicas
    • index.auto_expand_replicas
    • index.refresh_interval
    • index.max_result_window
    • index.max_inner_result_window
    • index.max_rescore_window
    • index.max_docvalue_fields_search
    • index.max_script_fields
    • index.max_ngram_diff
    • index.max_shingle_diff
    • index.blocks.read_only
    • index.blocks.read_only_allow_delete
    • index.blocks.read
    • index.blocks.write
    • index.blocks.metadata
    • index.max_refresh_listeners
    • index.highlight.max_analyzed_offset
    • index.max_terms_count
    • index.routing.allocation.enable
    • index.routing.rebalance.enable
    • index.gc_deletes

Kibana

下载

Kibana官网下载地址 5.5.3版本

启动

执行安装目录下的bin/kibana命令即可。

注意:必须在ElasticSearch 启动之后才能正常访问Kibana,否则提示无法登录。

访问:http://localhost:5601

汉化

  • 先下载Python2.7 官网下载地址
  • 从Github下载汉化项目Kibana_Hanization
  • 在Kibana_Hanization项目的根目录执行命令python main.py Kibana目录
  • 重新打开Kibana

实际运用的思考

ElasticSearch 架构

调研问题列表

  • Mysql 数据迁移到ES 的方案?
  • ES 索引配置多环境同步的处理方案。
    • 手动在Kibana上执行命令。
      • 新增索引
        • 直接使用新增索引API
      • 删除索引
        • 直接使用删除索引API
      • 修改索引
        • 新增字段
        • 修改字段
          • 修改字段数据类型
            • 使用新增索引API建立新的索引
            • 使用修改索引配置API,暂停索引写入操作。
            • 使用Reindex API 将旧数据拷贝到新数据。参考ReIndex API
            • 使用修改索引别名关系API,更改原索引的别名指向新的索引。
        • 修改字段Mapping参数
          • 除了修改搜索参数,其他修改都必须重建索引。
        • 删除字段
          • 必须重建索引。
  • 关键字补全的方案
  • 查询语法
  • 修改索引数据
    • 修改字段值
      • 非嵌套字段
      • 嵌套字段
        • 只能整个字段值全部替换,不能做部分修改。

          目前采用修改前先读取doc的全部数据,再在程序里更新为最终的数据,最后通过新增/替换命令更新整个doc。

  • 删除索引数据
  • 字段数据类型选择的方案
    • 数据类型的选择
      • 整数类型 -> long
      • 小数类型 - >scaled_float 并设置scaling_factor 值为100 即保留2位小数。
      • 文本类型 -> 需要分词使用text,不分词使用keyword
      • 一对多的关系 -> nested
      • 日期类型 -> date
      • 一对一的关系 ->object
    • 数据类型参数如何选择
      • 解析器
        • 字段本身 使用标准解析器分词
        • 字段.raw 不分词
        • 字段.chinese 索引使用中文最大分词,查询使用中文智能分词
      • 多字段
        • 所有的需要被查询的文本字段都必须设置为多字段,并且都要有rawchinese子字段。
  • 数据类型转换问题
    • 禁止数字型接收字符型数字,必须严格要求字段对应的类型。
    • 日期格式支持"yyyy/MM/dd HH:mm:ss.SSSSSS||yyyy/MM/dd||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd HH:mm:ss.SSS||epoch_millis" 格式。
  • 创建索引时的参数问题
    • settings
      • 见下面的推荐的Index配置
    • mappings
      • 见下面的推荐的mappings配置
    • aliases
      • 所有的索引操作都必须强制使用别名访问。

总结

  • 所有的索引都必须使用别名访问。
  • keyword 字段长度通过ignore_above参数控制,超过这个参数值的字符将不会被索引。
  • ReIndex 操作源Index和目标Index的字段类型不是所有类型直接都可以互转。
  • 尽量将ElasticSearch作为一个只读数据,用于解决多关键分词模糊匹配的分页查询效率问题,而不是把他作为一个主数据库使用。
  • ElasticSearch 集群如果只有两个节点,在某个节点崩溃时,会导致数据丢失。

ElasticSearch Server端

调研问题列表

  • ES Server是自己搭建还是购买阿里云ES服务器
  • 阿里云ES服务器如何选择?
    • 开发使用1核2G,2节点,50G云盘
    • 其他使用2核4G,3节点,50G本地SSD
  • 阿里云ES服务器版本?
    • 阿里云ES版本为6.3.1
  • 阿里云ES服务器要怎么管理,包括账号密码、VPC、权限、ES Server本身的配置等
    • 除了开发的ES服务器开启外网访问,其他只允许内网访问。同时Kibana开启IP白名单访问。

ElasticSearch Client 端

  • 客户端最好有程序使用的客户端和手动操作的客户端,其中程序使用的客户端用于在后端应用中使用,手动操作的客户端用于对ES数据进行手动干预,比如索引建立,配置更改,等等手动操作。
    • Java版本客户端,按照官方建议最好使用REST版本,而不是Transport版本。参考客户端访问jest

    • 手动操作使用Kibana

调研问题列表

问题记录

  • 聚合查询报错,错误内容如下:
    Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load
    

问题原因,是indeed的mapping字段没有设置fielddata=true。处理方法如下: 执行以下请求:

  1. PUT megacorp/_mapping/employee
  2. {
  3. "employee": {
  4. "properties": {
  5. "interests": {
  6. "type": "text",
  7. "fielddata": true
  8. }
  9. }
  10. }
  11. }

参考how to set fielddata=true in kibana

  • 在Linux上不允许以root用户启动

  • Linux启动失败的一些问题
  • 6.x版本移除了type(映射类型)
    • 对应6.x版本,每个Index只允许有一个类型,即只允许单一类型,类型名称可以自定义,但只能有一个。推荐的类型名称是_doc,参考删除映射类型
  • 良好的ES性能的关键是将数据去规范化为文档。JOIN字段会带来额外的性能开销。参考parent_join_and_performance

  • Mapping创建好之后不能修改,但可以通过别名方式变相修改。即创建设置Index和Index的别名。操作的时候使用Index的别名,需要修改的时候,建一个新的Index,然后修改别名关联新的Index即可。参考elasticsearch 修改 mapping

其他相关笔记

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/940044
推荐阅读
相关标签
  

闽ICP备14008679号