几个核心概念

文档:对应 RDB 中的记录

类型:很少用。可设置,也可不设置

索引:就是数据库

  • 分片和副本

02-核心概念与 ik 分词器 - 图1image.png

一 个 集群 至 少 有 一 个 节点 , 而 一 个 节点 就 是 一 个 ES 进 程 , 节 点 可 以 有 多 个 索引 默认 的 , 如 果 你 创建 索引 , 那么 索引 将 会 有 个 5 个 分 片 ( primary shard,又 称 主 分 片 ) 构成 的 , 每 一 个 主 分 片 会 有 一 个 副本 ( replica shard ,又 称 复制 分 片 )
02-核心概念与 ik 分词器 - 图3
image.png

  • 倒排索引

了解原理与优势。

  • ES 索引与 Lucence 索引
    在 ES 中,索引被分为多个分片,每份分片是一个 Lucene 的索引。所以一个 ES 索引是由多个 Lucene 索引组成的。

IK分词器

分 词 : 即 把 一 段 中 文 或 者 别 的 划分 成 一 个 个 的 关键 字 , 我们 在 搜索 时 候 会 把 自己 的 信息 进行 分 词 , 会 把 数据 库 中 或 者 索引 库 中 的 数 据 进行 分 词 , 然 后 进行 一 个 匹配 操作 , 默 认 的 中 文 分 词 是 将 每 个 字 看 成 一 个 词 , 比 如 ‘ 我 爱 狂 神 “ 会 被 分 为 “我 “ 爱 “” 狂 “” 神 ”, 这 显 然 是 不 符合 要 求 的 , 所 以 我 们 需要 安装 中 文 分 词 器 ik 来 解决 这 个 问题 。

如果使用中文,建议使用 ik 分词器!

安装

https://github.com/medcl/elasticsearch-analysis-ik

下载,放入 es 安装目录。

版本要与 ES 一致!
02-核心概念与 ik 分词器 - 图5image.png

重启观察 sudo tail -f /var/log/elasticsearch/elasticsearch.log

image.png
02-核心概念与 ik 分词器 - 图8
或者:

image.png

使用 kibana 测试

最少切分 切分结果内容不重复
02-核心概念与 ik 分词器 - 图10

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "中国人民",
  5. "start_offset" : 0,
  6. "end_offset" : 4,
  7. "type" : "CN_WORD",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "站起",
  12. "start_offset" : 4,
  13. "end_offset" : 6,
  14. "type" : "CN_WORD",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "来了",
  19. "start_offset" : 6,
  20. "end_offset" : 8,
  21. "type" : "CN_WORD",
  22. "position" : 2
  23. }
  24. ]
  25. }

最细粒度切分
02-核心概念与 ik 分词器 - 图11

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "中国人民",
  5. "start_offset" : 0,
  6. "end_offset" : 4,
  7. "type" : "CN_WORD",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "中国人",
  12. "start_offset" : 0,
  13. "end_offset" : 3,
  14. "type" : "CN_WORD",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "中国",
  19. "start_offset" : 0,
  20. "end_offset" : 2,
  21. "type" : "CN_WORD",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "国人",
  26. "start_offset" : 1,
  27. "end_offset" : 3,
  28. "type" : "CN_WORD",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "人民",
  33. "start_offset" : 2,
  34. "end_offset" : 4,
  35. "type" : "CN_WORD",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "站起来",
  40. "start_offset" : 4,
  41. "end_offset" : 7,
  42. "type" : "CN_WORD",
  43. "position" : 5
  44. },
  45. {
  46. "token" : "站起",
  47. "start_offset" : 4,
  48. "end_offset" : 6,
  49. "type" : "CN_WORD",
  50. "position" : 6
  51. },
  52. {
  53. "token" : "起来",
  54. "start_offset" : 5,
  55. "end_offset" : 7,
  56. "type" : "CN_WORD",
  57. "position" : 7
  58. },
  59. {
  60. "token" : "来了",
  61. "start_offset" : 6,
  62. "end_offset" : 8,
  63. "type" : "CN_WORD",
  64. "position" : 8
  65. }
  66. ]
  67. }

继续拆分一个复杂的:
02-核心概念与 ik 分词器 - 图12

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "老",
  5. "start_offset" : 0,
  6. "end_offset" : 1,
  7. "type" : "CN_CHAR",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "八",
  12. "start_offset" : 1,
  13. "end_offset" : 2,
  14. "type" : "TYPE_CNUM",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "蜜",
  19. "start_offset" : 2,
  20. "end_offset" : 3,
  21. "type" : "CN_CHAR",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "制",
  26. "start_offset" : 3,
  27. "end_offset" : 4,
  28. "type" : "CN_CHAR",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "小",
  33. "start_offset" : 4,
  34. "end_offset" : 5,
  35. "type" : "CN_CHAR",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "汉堡",
  40. "start_offset" : 5,
  41. "end_offset" : 7,
  42. "type" : "CN_WORD",
  43. "position" : 5
  44. }
  45. ]
  46. }

ES 不认识 “老八” 和 “小汉堡”,怎么办呢?加入到自定义的字典中!
02-核心概念与 ik 分词器 - 图13
image.png
02-核心概念与 ik 分词器 - 图15image.png
image.png

重启 ES,再次尝试,成功!

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "老八",
  5. "start_offset" : 0,
  6. "end_offset" : 2,
  7. "type" : "CN_WORD",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "蜜",
  12. "start_offset" : 2,
  13. "end_offset" : 3,
  14. "type" : "CN_CHAR",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "制",
  19. "start_offset" : 3,
  20. "end_offset" : 4,
  21. "type" : "CN_CHAR",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "小汉堡",
  26. "start_offset" : 4,
  27. "end_offset" : 7,
  28. "type" : "CN_WORD",
  29. "position" : 3
  30. }
  31. ]
  32. }

以后类似场景,我们需要自己将分词放在自定义的 dic 文件中进行配置即可!