1. IK分词器插件

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

2. 安装步骤

2.1 下载 ik 分词器包

github 地址:https://github.com/medcl/elasticsearch-analysis-ik
github 下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

Tip:版本要和 elasticSearch 对应

2.2 下载后解压

将目录拷贝到 elasticSearch 根目录下的 plugins 目录中

image.png

2.3 重启 es 服务

启动过程中,你可以看到正在加载”analysis-ik”插件的提示信息。 服务启动后,在命令行运行 elasticsearch-plugin list 命令,确认 ik 插件安装成功。

image.png
image.png

2.4 kibana 中测试 ik 分词器

  1. ik_smart # 粗粒度分词,优先匹配最长词(有些词只有一个词)
  2. ik_max_word # 细粒度分词,会穷尽一个语句中所有分词可能

2.4.1 ik_smart 测试(kibana中测试)

  1. GET _analyze
  2. {
  3. "analyzer": "ik_smart",
  4. "text":"中国共产党"
  5. }
  6. ----------------------------
  7. {
  8. "tokens" : [
  9. {
  10. "token" : "中国共产党",
  11. "start_offset" : 0,
  12. "end_offset" : 5,
  13. "type" : "CN_WORD",
  14. "position" : 0
  15. }
  16. ]
  17. }

2.4.2 ik_max_word 测试(kibana中)

  1. GET _analyze
  2. {
  3. "analyzer": "ik_max_word",
  4. "text":"中国共产党"
  5. }
  6. ----------------------------
  7. {
  8. "tokens" : [
  9. {
  10. "token" : "中国共产党",
  11. "start_offset" : 0,
  12. "end_offset" : 5,
  13. "type" : "CN_WORD",
  14. "position" : 0
  15. },
  16. {
  17. "token" : "中国",
  18. "start_offset" : 0,
  19. "end_offset" : 2,
  20. "type" : "CN_WORD",
  21. "position" : 1
  22. },
  23. {
  24. "token" : "国共",
  25. "start_offset" : 1,
  26. "end_offset" : 3,
  27. "type" : "CN_WORD",
  28. "position" : 2
  29. },
  30. {
  31. "token" : "共产党",
  32. "start_offset" : 2,
  33. "end_offset" : 5,
  34. "type" : "CN_WORD",
  35. "position" : 3
  36. },
  37. {
  38. "token" : "共产",
  39. "start_offset" : 2,
  40. "end_offset" : 4,
  41. "type" : "CN_WORD",
  42. "position" : 4
  43. },
  44. {
  45. "token" : "党",
  46. "start_offset" : 4,
  47. "end_offset" : 5,
  48. "type" : "CN_CHAR",
  49. "position" : 5
  50. }
  51. ]
  52. }

2.5 自定义词库

想让系统识别 “赛亚人” 是一个词,需要编辑自定义词库

设置前:

  1. GET _analyze
  2. {
  3. "analyzer": "ik_smart",
  4. "text":"超级赛亚人"
  5. }
  6. ----------------------------
  7. {
  8. "tokens" : [
  9. {
  10. "token" : "超级",
  11. "start_offset" : 0,
  12. "end_offset" : 2,
  13. "type" : "CN_WORD",
  14. "position" : 0
  15. },
  16. {
  17. "token" : "赛",
  18. "start_offset" : 2,
  19. "end_offset" : 3,
  20. "type" : "CN_CHAR",
  21. "position" : 1
  22. },
  23. {
  24. "token" : "亚",
  25. "start_offset" : 3,
  26. "end_offset" : 4,
  27. "type" : "CN_CHAR",
  28. "position" : 2
  29. },
  30. {
  31. "token" : "人",
  32. "start_offset" : 4,
  33. "end_offset" : 5,
  34. "type" : "CN_CHAR",
  35. "position" : 3
  36. }
  37. ]
  38. }

2.5.1 进入elasticsearch/plugins/ik/config目录

ik 目录是之前将 ik 分词器文件放到 es 的 plugins 文件夹中自定义的目录

2.5.2 新建一个 .dic 文件

创建 .dic 文件,内容为需要识别的词

image.png

2.5.3 修改 IKAnalyzer.cfg.xml(在ik/config目录下)

在 ext_dic 下配置自己创建的 .dic 文件的 全名

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
  3. <properties>
  4. <comment>IK Analyzer 扩展配置</comment>
  5. <!--用户可以在这里配置自己的扩展字典 -->
  6. <entry key="ext_dict">my.dic</entry>
  7. <!--用户可以在这里配置自己的扩展停止词字典-->
  8. <entry key="ext_stopwords"></entry>
  9. <!--用户可以在这里配置远程扩展字典 -->
  10. <!-- <entry key="remote_ext_dict">words_location</entry> -->
  11. <!--用户可以在这里配置远程扩展停止词字典-->
  12. <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
  13. </properties>

2.5.4 重启 es 、kibana

下图为 es 启动文件

image.png

设置后:

  1. GET _analyze
  2. {
  3. "analyzer": "ik_smart",
  4. "text":"超级赛亚人"
  5. }
  6. ----------------------------
  7. {
  8. "tokens" : [
  9. {
  10. "token" : "超级",
  11. "start_offset" : 0,
  12. "end_offset" : 2,
  13. "type" : "CN_WORD",
  14. "position" : 0
  15. },
  16. {
  17. "token" : "赛亚人",
  18. "start_offset" : 2,
  19. "end_offset" : 5,
  20. "type" : "CN_WORD",
  21. "position" : 1
  22. }
  23. ]
  24. }

3. Rest 风格说明

一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交 互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

ES 基本Rest命令说明:

method url地址 描述
PUT localhost:9200/索引名称/类型名称/文档id 创建文档(指定文档id)
POST localhost:9200/索引名称/类型名称 创建文档(随机文档id)
POST localhost:9200/索引名称/类型名称/文档id/_update 修改文档
DELETE localhost:9200/索引名称/类型名称/文档id 删除文档
GET localhost:9200/索引名称/类型名称/文档id 查询文档通过文档id
POST localhost:9200/索引名称/类型名称/_search 查询所有数据

3.1 查询索引情况(索引列表)

  1. GET _cat/indices?v
  2. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  3. green open .kibana_task_manager_1 RU2mfr3aTzyux_am3YtrAQ 1 0 2 0 12.4kb 12.4kb
  4. green open .apm-agent-configuration 1OTnQrmCRB-JiX484uY7uA 1 0 0 0 283b 283b
  5. green open .kibana_1 FR3NzGvlQT2zh--70jfL8w 1 0 11 4 29.3kb 29.3kb

3.2 指定索引类型(包括 keyword)

下面的例子中,name 属性不会被分词器拆分,保持整体存入倒排索引中

  1. PUT /test2
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name" : {
  6. "type" : "text",
  7. "fields" : {
  8. "keyword" : {
  9. "type" : "keyword",
  10. "ignore_above" : 256
  11. }
  12. }
  13. },
  14. "age":{
  15. "type": "long"
  16. },
  17. "birthday":{
  18. "type": "date"
  19. }
  20. }
  21. }
  22. }

4. 增删查改命令

4.1 查

4.1.1 GET 查询(通过文档 id)

GET localhost:9200/索引名称/类型名称/文档id { … // 条件 }

  1. // edward:索引名称 user:类型名称 5:文档id
  2. GET /edward/user/5
  3. ------------------
  4. {
  5. "_index" : "edward",
  6. "_type" : "user",
  7. "_id" : "5",
  8. "_version" : 1,
  9. "_seq_no" : 7,
  10. "_primary_term" : 1,
  11. "found" : true,
  12. "_source" : {
  13. "name" : "陈汐",
  14. "age" : 10,
  15. "desc" : "研究技术",
  16. "tages" : [
  17. "show",
  18. "time",
  19. "靓仔"
  20. ]
  21. }
  22. }

4.1.2 条件查询( _search?q= )

GET/POST localhost:9200/索引名称/类型名称/_search?q=name:龙 通过 _serarch?q=name:龙 查询条件是name属性有“龙”的那些数据。

  1. GET /edward/user/_search?q=name:龙
  2. ------------------
  3. {
  4. "took" : 0,
  5. "timed_out" : false,
  6. "_shards" : {
  7. "total" : 1,
  8. "successful" : 1,
  9. "skipped" : 0,
  10. "failed" : 0
  11. },
  12. "hits" : {
  13. "total" : {
  14. "value" : 2, // 一共返回的文档数
  15. "relation" : "eq" // 条件是等于
  16. },
  17. "max_score" : 1.4021543, // 匹配分值:分值越高,匹配的内容越多
  18. "hits" : [
  19. {
  20. "_index" : "edward",
  21. "_type" : "user",
  22. "_id" : "4",
  23. "_score" : 1.4021543,
  24. "_source" : {
  25. "name" : "大龙龙",
  26. "age" : 50,
  27. "desc" : "研究技术",
  28. "tages" : [
  29. "show",
  30. "time",
  31. "靓仔"
  32. ]
  33. }
  34. },
  35. {
  36. "_index" : "edward",
  37. "_type" : "user",
  38. "_id" : "3",
  39. "_score" : 1.4021543,
  40. "_source" : {
  41. "name" : "小龙龙",
  42. "age" : 40,
  43. "desc" : "研究技术",
  44. "tages" : [
  45. "show",
  46. "time",
  47. "靓仔"
  48. ]
  49. }
  50. }
  51. ]
  52. }
  53. }

4.1.3 构建查询( query )

一般的,我们推荐使用构建查询,以后在与程序交互时的查询等也是使用构建查询方式处理查询条件, 因为该方 式可以构建更加复杂的查询条件,也更加一目了然。

4.1.3.1 基本使用

下例,查询条件是一步步构建出来的,将查询条件添加到 match 中即可。

  1. // 语法:
  2. GET /索引名/类型名/_search
  3. {
  4. ... // 构建条件
  5. }
  6. ------------------
  7. ------------------
  8. // 案例:
  9. GET /edward/user/_search
  10. {
  11. "query": {
  12. "match": {
  13. "name":"陈"
  14. }
  15. }
  16. }
  17. ------------------
  18. {
  19. "took" : 0,
  20. "timed_out" : false,
  21. "_shards" : {
  22. "total" : 1,
  23. "successful" : 1,
  24. "skipped" : 0,
  25. "failed" : 0
  26. },
  27. "hits" : {
  28. "total" : {
  29. "value" : 1,
  30. "relation" : "eq"
  31. },
  32. "max_score" : 1.1631508,
  33. "hits" : [
  34. {
  35. "_index" : "edward",
  36. "_type" : "user",
  37. "_id" : "5",
  38. "_score" : 1.1631508,
  39. "_source" : {
  40. "name" : "陈汐",
  41. "age" : 10,
  42. "desc" : "一个热爱研究的技术人",
  43. "tages" : [
  44. "靓仔",
  45. "暖"
  46. ],
  47. "tags" : [
  48. "靓仔",
  49. "帅气"
  50. ]
  51. }
  52. }
  53. ]
  54. }
  55. }

4.1.3.2 查询全部

  1. // 写法1: match_all的值为空,表示没有查询条件
  2. GET /edward/user/_search
  3. {
  4. "query": {
  5. "match_all": {}
  6. }
  7. }
  8. // 写法2:
  9. GET /edward/user/_search

4.1.3.3 指定查询返回字段

在查询中,通过 _source 来控制仅返回 name 和 age 属性。

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "_source": [ // 数组中指定返回的字段名
  7. "name",
  8. "desc"
  9. ]
  10. }
  11. ------------------
  12. {
  13. "took" : 0,
  14. "timed_out" : false,
  15. "_shards" : {
  16. "total" : 1,
  17. "successful" : 1,
  18. "skipped" : 0,
  19. "failed" : 0
  20. },
  21. "hits" : {
  22. "total" : {
  23. "value" : 6,
  24. "relation" : "eq"
  25. },
  26. "max_score" : 1.0,
  27. "hits" : [
  28. {
  29. "_index" : "edward",
  30. "_type" : "user",
  31. "_id" : "1",
  32. "_score" : 1.0,
  33. "_source" : {
  34. "name" : "edwarddamon",
  35. "desc" : "研究技术"
  36. }
  37. },
  38. ......
  39. ]
  40. }
  41. }

4.1.4 排序查询( sort )

在条件查询的基础上,我们又通过 sort 来做排序,排序对象是 age , order 是 desc 降序。

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "age": {
  9. "order": "asc"
  10. }
  11. }
  12. ]
  13. }

注意:在排序的过程中,只能使用可排序的属性进行排序。那么可以排序的属性有哪些呢?

  • 数字
  • 日期
  • ID

其他都不行!

4.1.5 分页查询( from,size )

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "age": {
  9. "order": "asc"
  10. }
  11. }
  12. ],
  13. "from": 0, // 开始的下标(从0开始)
  14. "size": 2 // 每次返回的条数
  15. }

4.1.6 布尔查询(bool)

4.1.6.1 must(and)

如下例,要求 name 属性包含“龙”,并且 age 属性为 40 。

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "name": "龙"
  9. }
  10. },
  11. {
  12. "match": {
  13. "age": 40
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

4.1.6.2 should(or)

如下例,要求 name 属性包含“龙”,或者 age 属性为 40 。

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "match": {
  8. "name": "龙"
  9. }
  10. },
  11. {
  12. "match": {
  13. "age": 40
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

4.1.6.3 must_not(not)

查询 age 属性不是 40 的数据。

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must_not": [
  6. {
  7. "match": {
  8. "age": 40
  9. }
  10. }
  11. ]
  12. }
  13. }
  14. }

4.1.6.4 filter(范围、过虑等)

查询 name 属性包含“龙”,并且 age 属性 大于等于45 并且 小于等于60。

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "name": "龙"
  9. }
  10. }
  11. ],
  12. "filter": {
  13. "range": {
  14. "age": {
  15. "gte": 45,
  16. "lte": 60
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }

这里就用到了 filter 条件过滤查询,过滤条件的范围用 range 表示。
其余操作如下 :

  • gt 表示大于
  • gte 表示大于等于
  • lt 表示小于
  • lte 表示小于等于

4.1.7 短语检索

返回了所有 tages 属性中带“暖”和“靓仔”的记录
只要含有这个 tages 满足一个就给我返回这个数据 (or)

  1. GET /edward/user/_search
  2. {
  3. "query": {
  4. "match": {
  5. "tages":"暖 靓仔"
  6. }
  7. }
  8. }
  • 拓展知识:(and)

match_phrase 称为短语搜索,要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致

  1. GET edward/user/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "name": "大 龙"
  6. }
  7. }
  8. }

4.1.8 精确查找(term)

term 和 match 的区别:

  • term:查询的词不会被分词器解析,直接去倒排索引中查找是否有匹配的标签
  • match:查询的词会被分词器解析,再用解析后的词去倒排索引中查找是否有匹配的标签 ```java // 查询“孤独”这个词被分词器解析成什么 GET _analyze { “analyzer”: “standard”, “text”: “孤独” }

{ “tokens” : [ { “token” : “孤”, “start_offset” : 0, “end_offset” : 1, “type” : ““, “position” : 0 }, { “token” : “独”, “start_offset” : 1, “end_offset” : 2, “type” : ““, “position” : 1 } ] }

  1. - term 进行精确查询
  2. > 此时参数“孤独”不会被分析器解析,则直接用“孤独”去倒排索引中查找;
  3. > 由于之前“孤独”被拆分成了“孤”和“独”存入了倒排索引中,所以这里匹配不到。
  4. ```java
  5. GET /edward/user/_search
  6. {
  7. "query": {
  8. "term": {
  9. "name":"孤独"
  10. }
  11. }
  12. }
  13. ------------
  14. {
  15. "took" : 0,
  16. "timed_out" : false,
  17. "_shards" : {
  18. "total" : 1,
  19. "successful" : 1,
  20. "skipped" : 0,
  21. "failed" : 0
  22. },
  23. "hits" : {
  24. "total" : {
  25. "value" : 0,
  26. "relation" : "eq"
  27. },
  28. "max_score" : null,
  29. "hits" : [ ]
  30. }
  31. }
  • 用 match 进行查询

    这里的参数“孤独”会被拆分成“孤”独”,再分别用“孤”和“独”去倒排索引中进行查找; 由于之前“孤独”被拆分成了“孤”和“独”存入了倒排索引中,所以这里可以匹配到。

  1. // 写法1:对“孤独”进行拆分再去到怕索引中查询
  2. GET /edward/user/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name":"孤独"
  7. }
  8. }
  9. }
  10. // 写法2:“孤”和“独”是或关系,任意一个在倒排索引中查到了,都匹配
  11. GET /edward/user/_search
  12. {
  13. "query": {
  14. "terms": {
  15. "name":["孤","独"]
  16. }
  17. }
  18. }
  19. ------------
  20. {
  21. "took" : 0,
  22. "timed_out" : false,
  23. "_shards" : {
  24. "total" : 1,
  25. "successful" : 1,
  26. "skipped" : 0,
  27. "failed" : 0
  28. },
  29. "hits" : {
  30. "total" : {
  31. "value" : 1,
  32. "relation" : "eq"
  33. },
  34. "max_score" : 3.3479528,
  35. "hits" : [
  36. {
  37. "_index" : "edward",
  38. "_type" : "user",
  39. "_id" : "AH4YhnsBQ4foxyhx5ioD",
  40. "_score" : 3.3479528,
  41. "_source" : {
  42. "name" : "孤独",
  43. "age" : 20,
  44. "desc" : "研究技术",
  45. "tages" : [
  46. "show",
  47. "time",
  48. "靓仔"
  49. ]
  50. }
  51. }
  52. ]
  53. }
  54. }

4.1.9 高亮显示( highlight )

4.1.9.1 默认高亮样式

默认返回的数据中,入参字段会加上 标签

  1. GET edward/user/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "龙"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "name":{}
  11. }
  12. }
  13. }
  14. -----------------------
  15. "hits" : [
  16. {
  17. "_index" : "edward",
  18. "_type" : "user",
  19. "_id" : "4",
  20. "_score" : 1.4021543,
  21. "_source" : {
  22. "name" : "大龙龙",
  23. "age" : 50,
  24. "desc" : "研究技术",
  25. "tages" : [
  26. "show",
  27. "time",
  28. "靓仔"
  29. ]
  30. },
  31. "highlight" : {
  32. "name" : [
  33. "大<em>龙</em><em>龙</em>"
  34. ]
  35. }
  36. },
  37. ......
  38. ]

4.1.9.2 自定义高亮样式

默认返回的数据中,入参字段会加上自定义的 标签

  1. GET edward/user/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "龙"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": "<c style='color:red'>",
  10. "post_tags": "</c>",
  11. "fields": {
  12. "name":{}
  13. }
  14. }
  15. }
  16. ---------------------
  17. "hits" : [
  18. {
  19. "_index" : "edward",
  20. "_type" : "user",
  21. "_id" : "4",
  22. "_score" : 1.4021543,
  23. "_source" : {
  24. "name" : "大龙龙",
  25. "age" : 50,
  26. "desc" : "研究技术",
  27. "tages" : [
  28. "show",
  29. "time",
  30. "靓仔"
  31. ]
  32. },
  33. "highlight" : {
  34. "name" : [
  35. "大<c style='color:red'>龙</c><c style='color:red'>龙</c>"
  36. ]
  37. }
  38. },
  39. ......
  40. ]

4.2 增

4.2.1 PUT 创建(指定文档 id)

PUT localhost:9200/索引名称/类型名称/文档id { … // 文档内容 }

  1. // PUT:创建命令 edward:索引 user:类型 1:文档id
  2. PUT /edward/user/5
  3. {
  4. "name":"陈汐",
  5. "age": 10,
  6. "desc": "研究技术",
  7. "tages": [
  8. "show",
  9. "time",
  10. "靓仔"
  11. ]
  12. }
  13. ------------------
  14. {
  15. "_index" : "edward", // 索引
  16. "_type" : "user", // 类型
  17. "_id" : "5", // id
  18. "_version" : 1, // 版本(每次数据修改都会+1)
  19. "result" : "created", // 操作类型(创建为created、修改为uodated)
  20. "_shards" : { // 分片信息
  21. "total" : 2,
  22. "successful" : 1,
  23. "failed" : 0
  24. },
  25. "_seq_no" : 7,
  26. "_primary_term" : 1
  27. }

4.2.2 POST 创建(随机文档 id)

POST localhost:9200/索引名称/类型名称 { … // 文档内容 }

  1. POST /edward/user
  2. {
  3. "name":"孤独",
  4. "age": 20,
  5. "desc": "研究技术",
  6. "tages": [
  7. "show",
  8. "time",
  9. "靓仔"
  10. ]
  11. }
  12. ------------------
  13. {
  14. "_index" : "edward",
  15. "_type" : "user",
  16. "_id" : "AH4YhnsBQ4foxyhx5ioD", // 随机生成id
  17. "_version" : 1,
  18. "result" : "created",
  19. "_shards" : {
  20. "total" : 2,
  21. "successful" : 1,
  22. "failed" : 0
  23. },
  24. "_seq_no" : 12,
  25. "_primary_term" : 1
  26. }

4.4 改

4.4.1 PUT 修改(新文档内容覆盖旧的)

用法和 PUT 创建相同,只要将需要修改的字段改为新数据便会覆盖旧内容

缺点:原理是数据覆盖,若原数据(原来不需要修改的数据)不写,则会丢失
Tip:不建议使用

4.4.2 POST 修改(指定修改内容)

POST localhost:9200/索引名称/类型名称/文档id/_update { “doc”:{ … // 修改的内容 } }

  1. POST /edward/user/5/_update
  2. {
  3. "doc":{
  4. "desc":"一个热爱研究的技术人",
  5. "tages":[
  6. "靓仔",
  7. "帅气"
  8. ]
  9. }
  10. }
  11. ------------------
  12. {
  13. "_index" : "edward",
  14. "_type" : "user",
  15. "_id" : "5",
  16. "_version" : 5,
  17. "result" : "updated",
  18. "_shards" : {
  19. "total" : 2,
  20. "successful" : 1,
  21. "failed" : 0
  22. },
  23. "_seq_no" : 11,
  24. "_primary_term" : 1
  25. }

4.5 删

4.5.1 DELETE 删除(指定文档id 或 指定索引)

DELETE localhost:9200/索引名称/类型名称/文档id 删除文档 DELETE localhost:9200/索引名称 删除索引及索引中的所有文档

  1. // 删除指定文档案例:
  2. DELETE /edward/user/1
  3. ---------------------
  4. {
  5. "_index" : "edward",
  6. "_type" : "user",
  7. "_id" : "1",
  8. "_version" : 2,
  9. "result" : "deleted",
  10. "_shards" : {
  11. "total" : 2,
  12. "successful" : 1,
  13. "failed" : 0
  14. },
  15. "_seq_no" : 13,
  16. "_primary_term" : 1
  17. }
  18. // 删除索引案例:
  19. DELETE /test
  20. ------------
  21. {
  22. "acknowledged" : true
  23. }

5. 说明

注意 elasticsearch 在第一个版本的开始 每个文档都储存在一个索引中,并分配一个 映射类型,映射类 型用于表示被索引的文档或者实体的类型,这样带来了一些问题, 导致后来在 elasticsearch6.0.0 版本中 一个文档只能包含一个映射类型,而在 7.0.0 中,映 射类型则将被弃用,到了 8.0.0 中则将完全被删 除。
只要记得,一个索引下面只能创建一个类型就行了,其中各字段都具有唯一性,如果在创建映射的时 候,如果没有指定文档类型,那么该索引的默认索引类型是 _doc不指定文档id则会内部帮我们生 成一个id字符串