HTTP-索引-创建

1. 创建索引

对比关系型数据库,创建索引就等同于创建数据库。

在 Postman 中,向 ES 服务器发 PUT 请求 : http://127.0.0.1:9200/shopping

请求后,服务器返回响应:

  1. {
  2. "acknowledged": true,//响应结果
  3. "shards_acknowledged": true,//分片结果
  4. "index": "shopping"//索引名称
  5. }

后台日志:

  1. [2021-04-08T13:57:06,954][INFO ][o.e.c.m.MetadataCreateIndexService] [DESKTOP-LNJQ0VF] [shopping] creating index, cause [api], templates [], shards [1]/[1], mappings []

如果重复发 PUT 请求 : http://127.0.0.1:9200/shopping 添加索引,会返回错误信息 :

  1. {
  2. "error": {
  3. "root_cause": [
  4. {
  5. "type": "resource_already_exists_exception",
  6. "reason": "index [shopping/J0WlEhh4R7aDrfIc3AkwWQ] already exists",
  7. "index_uuid": "J0WlEhh4R7aDrfIc3AkwWQ",
  8. "index": "shopping"
  9. }
  10. ],
  11. "type": "resource_already_exists_exception",
  12. "reason": "index [shopping/J0WlEhh4R7aDrfIc3AkwWQ] already exists",
  13. "index_uuid": "J0WlEhh4R7aDrfIc3AkwWQ",
  14. "index": "shopping"
  15. },
  16. "status": 400
  17. }

HTTP-索引-查询 & 删除

1. 查看所有索引

在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下 :

  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  2. yellow open shopping J0WlEhh4R7aDrfIc3AkwWQ 1 1 0 0 208b 208b
表头 含义
health 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
status 索引打开、关闭状态
index 索引名
uuid 索引统一编号
pri 主分片数量
rep 副本数量
docs.count 可用文档数量
docs.deleted 文档删除状态(逻辑删除)
store.size 主分片和副分片整体占空间大小
pri.store.size 主分片占空间大小

2. 查看单个索引

在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping
返回结果如下:

  1. {
  2. "shopping": {//索引名
  3. "aliases": {},//别名
  4. "mappings": {},//映射
  5. "settings": {//设置
  6. "index": {//设置 - 索引
  7. "creation_date": "1617861426847",//设置 - 索引 - 创建时间
  8. "number_of_shards": "1",//设置 - 索引 - 主分片数量
  9. "number_of_replicas": "1",//设置 - 索引 - 主分片数量
  10. "uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//设置 - 索引 - 主分片数量
  11. "version": {//设置 - 索引 - 主分片数量
  12. "created": "7080099"
  13. },
  14. "provided_name": "shopping"//设置 - 索引 - 主分片数量
  15. }
  16. }
  17. }
  18. }

3. 删除索引

在 Postman 中,向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping
返回结果如下:

  1. {
  2. "acknowledged": true
  3. }

再次查看所有索引,GET http://127.0.0.1:9200/_cat/indices?v,返回结果如下:

  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

HTTP-文档-创建(Put & Post)

假设索引已经创建好了,接下来我们来创建文档,并添加数据。这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式

在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc,请求体JSON内容为:

  1. {
  2. "title":"小米手机",
  3. "category":"小米",
  4. "images":"http://www.gulixueyuan.com/xm.jpg",
  5. "price":3999.00
  6. }

注意,此处发送请求的方式必须为 POST,不能是 PUT,否则会发生错误 。

返回结果:

  1. {
  2. "_index": "shopping",//索引
  3. "_type": "_doc",//类型-文档
  4. "_id": "ANQqsHgBaKNfVnMbhZYU",//唯一标识,可以类比为 MySQL 中的主键,随机生成
  5. "_version": 1,//版本
  6. "result": "created",//结果,这里的 create 表示创建成功
  7. "_shards": {//
  8. "total": 2,//分片 - 总数
  9. "successful": 1,//分片 - 总数
  10. "failed": 0//分片 - 总数
  11. },
  12. "_seq_no": 0,
  13. "_primary_term": 1
  14. }

上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。

如果想要自定义唯一性标识,需要在创建时指定: http://127.0.0.1:9200/shopping/_doc/1,请求体JSON内容为:

  1. {
  2. "title":"小米手机",
  3. "category":"小米",
  4. "images":"http://www.gulixueyuan.com/xm.jpg",
  5. "price":3999.00
  6. }

返回结果如下:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",//<------------------自定义唯一性标识
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 1,
  13. "_primary_term": 1
  14. }

此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT。

HTTP-查询-主键查询 & 全查询

查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询

在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1

返回结果如下:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 1,
  6. "_seq_no": 1,
  7. "_primary_term": 1,
  8. "found": true,
  9. "_source": {
  10. "title": "小米手机",
  11. "category": "小米",
  12. "images": "http://www.gulixueyuan.com/xm.jpg",
  13. "price": 3999
  14. }
  15. }

查找不存在的内容,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1001

返回结果如下:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1001",
  5. "found": false
  6. }

查看索引下所有数据,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_search

返回结果如下:

  1. {
  2. "took": 133,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "1",
  33. "_score": 1,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 3999
  39. }
  40. }
  41. ]
  42. }
  43. }

HTTP-全量修改 & 局部修改 & 删除

1. 全量修改

和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖

在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc/1

请求体JSON内容为:

  1. {
  2. "title":"华为手机",
  3. "category":"华为",
  4. "images":"http://www.gulixueyuan.com/hw.jpg",
  5. "price":1999.00
  6. }

修改成功后,服务器响应结果:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 2,
  6. "result": "updated",//<-----------updated 表示数据被更新
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 2,
  13. "_primary_term": 1
  14. }

2. 局部修改

修改数据时,也可以只修改某一给条数据的局部信息

在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_update/1

请求体JSON内容为:

  1. {
  2. "doc": {
  3. "title":"小米手机",
  4. "category":"小米"
  5. }
  6. }

返回结果如下:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 3,
  6. "result": "updated",//<-----------updated 表示数据被更新
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 3,
  13. "_primary_term": 1
  14. }

/
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_doc/1,查看修改内容:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 3,
  6. "_seq_no": 3,
  7. "_primary_term": 1,
  8. "found": true,
  9. "_source": {
  10. "title": "小米手机",
  11. "category": "小米",
  12. "images": "http://www.gulixueyuan.com/hw.jpg",
  13. "price": 1999
  14. }
  15. }

3. 删除

删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。

在 Postman 中,向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping/_doc/1

返回结果:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 4,
  6. "result": "deleted",//<---删除成功
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 4,
  13. "_primary_term": 1
  14. }

在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_doc/1,查看是否删除成功:

  1. {
  2. "_index": "shopping",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "found": false
  6. }

HTTP-条件查询 & 分页查询 & 查询排序

1. 条件查询

假设有以下文档内容,(在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search):

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 1,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. },
  53. {
  54. "_index": "shopping",
  55. "_type": "_doc",
  56. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  57. "_score": 1,
  58. "_source": {
  59. "title": "华为手机",
  60. "category": "华为",
  61. "images": "http://www.gulixueyuan.com/xm.jpg",
  62. "price": 1999
  63. }
  64. },
  65. {
  66. "_index": "shopping",
  67. "_type": "_doc",
  68. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  69. "_score": 1,
  70. "_source": {
  71. "title": "华为手机",
  72. "category": "华为",
  73. "images": "http://www.gulixueyuan.com/xm.jpg",
  74. "price": 1999
  75. }
  76. },
  77. {
  78. "_index": "shopping",
  79. "_type": "_doc",
  80. "_id": "CdR7sHgBaKNfVnMbsJb9",
  81. "_score": 1,
  82. "_source": {
  83. "title": "华为手机",
  84. "category": "华为",
  85. "images": "http://www.gulixueyuan.com/xm.jpg",
  86. "price": 1999
  87. }
  88. }
  89. ]
  90. }
  91. }

2. URL带参查询

查找category为小米的文档,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search?q=category:小米,返回结果如下:

  1. {
  2. "took": 94,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3862942,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1.3862942,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1.3862942,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 1.3862942,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. }
  53. ]
  54. }
  55. }

上述为URL带参数形式查询,这很容易让不善者心怀恶意,或者参数值出现中文会出现乱码情况。为了避免这些情况,我们可用使用带JSON请求体请求进行查询。

3. 请求体带参查询

接下带JSON请求体,还是查找category为小米的文档,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

/

  1. {
  2. "query":{
  3. "match":{
  4. "category":"小米"
  5. }
  6. }
  7. }

返回结果如下:

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3862942,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1.3862942,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1.3862942,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 1.3862942,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. }
  53. ]
  54. }
  55. }

带请求体方式的查找所有内容

查找所有文档内容,也可以这样,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match_all":{}
  4. }
  5. }

则返回所有文档内容:

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 1,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. },
  53. {
  54. "_index": "shopping",
  55. "_type": "_doc",
  56. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  57. "_score": 1,
  58. "_source": {
  59. "title": "华为手机",
  60. "category": "华为",
  61. "images": "http://www.gulixueyuan.com/xm.jpg",
  62. "price": 1999
  63. }
  64. },
  65. {
  66. "_index": "shopping",
  67. "_type": "_doc",
  68. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  69. "_score": 1,
  70. "_source": {
  71. "title": "华为手机",
  72. "category": "华为",
  73. "images": "http://www.gulixueyuan.com/xm.jpg",
  74. "price": 1999
  75. }
  76. },
  77. {
  78. "_index": "shopping",
  79. "_type": "_doc",
  80. "_id": "CdR7sHgBaKNfVnMbsJb9",
  81. "_score": 1,
  82. "_source": {
  83. "title": "华为手机",
  84. "category": "华为",
  85. "images": "http://www.gulixueyuan.com/xm.jpg",
  86. "price": 1999
  87. }
  88. }
  89. ]
  90. }
  91. }

4. 查询指定字段

如果你想查询指定字段,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match_all":{}
  4. },
  5. "_source":["title"]
  6. }

返回结果如下:

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1,
  22. "_source": {
  23. "title": "小米手机"
  24. }
  25. },
  26. {
  27. "_index": "shopping",
  28. "_type": "_doc",
  29. "_id": "A9R5sHgBaKNfVnMb25Ya",
  30. "_score": 1,
  31. "_source": {
  32. "title": "小米手机"
  33. }
  34. },
  35. {
  36. "_index": "shopping",
  37. "_type": "_doc",
  38. "_id": "BNR5sHgBaKNfVnMb7pal",
  39. "_score": 1,
  40. "_source": {
  41. "title": "小米手机"
  42. }
  43. },
  44. {
  45. "_index": "shopping",
  46. "_type": "_doc",
  47. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  48. "_score": 1,
  49. "_source": {
  50. "title": "华为手机"
  51. }
  52. },
  53. {
  54. "_index": "shopping",
  55. "_type": "_doc",
  56. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  57. "_score": 1,
  58. "_source": {
  59. "title": "华为手机"
  60. }
  61. },
  62. {
  63. "_index": "shopping",
  64. "_type": "_doc",
  65. "_id": "CdR7sHgBaKNfVnMbsJb9",
  66. "_score": 1,
  67. "_source": {
  68. "title": "华为手机"
  69. }
  70. }
  71. ]
  72. }
  73. }

5. 分页查询

在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match_all":{}
  4. },
  5. "from":0,
  6. "size":2
  7. }

返回结果如下:

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. }
  41. ]
  42. }
  43. }

6. 查询排序

如果你想通过排序查出价格最高的手机,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match_all":{}
  4. },
  5. "sort":{
  6. "price":{
  7. "order":"desc"
  8. }
  9. }
  10. }

返回结果如下:

  1. {
  2. "took": 96,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": null,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. },
  28. "sort": [
  29. 3999
  30. ]
  31. },
  32. {
  33. "_index": "shopping",
  34. "_type": "_doc",
  35. "_id": "A9R5sHgBaKNfVnMb25Ya",
  36. "_score": null,
  37. "_source": {
  38. "title": "小米手机",
  39. "category": "小米",
  40. "images": "http://www.gulixueyuan.com/xm.jpg",
  41. "price": 1999
  42. },
  43. "sort": [
  44. 1999
  45. ]
  46. },
  47. {
  48. "_index": "shopping",
  49. "_type": "_doc",
  50. "_id": "BNR5sHgBaKNfVnMb7pal",
  51. "_score": null,
  52. "_source": {
  53. "title": "小米手机",
  54. "category": "小米",
  55. "images": "http://www.gulixueyuan.com/xm.jpg",
  56. "price": 1999
  57. },
  58. "sort": [
  59. 1999
  60. ]
  61. },
  62. {
  63. "_index": "shopping",
  64. "_type": "_doc",
  65. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  66. "_score": null,
  67. "_source": {
  68. "title": "华为手机",
  69. "category": "华为",
  70. "images": "http://www.gulixueyuan.com/xm.jpg",
  71. "price": 1999
  72. },
  73. "sort": [
  74. 1999
  75. ]
  76. },
  77. {
  78. "_index": "shopping",
  79. "_type": "_doc",
  80. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  81. "_score": null,
  82. "_source": {
  83. "title": "华为手机",
  84. "category": "华为",
  85. "images": "http://www.gulixueyuan.com/xm.jpg",
  86. "price": 1999
  87. },
  88. "sort": [
  89. 1999
  90. ]
  91. },
  92. {
  93. "_index": "shopping",
  94. "_type": "_doc",
  95. "_id": "CdR7sHgBaKNfVnMbsJb9",
  96. "_score": null,
  97. "_source": {
  98. "title": "华为手机",
  99. "category": "华为",
  100. "images": "http://www.gulixueyuan.com/xm.jpg",
  101. "price": 1999
  102. },
  103. "sort": [
  104. 1999
  105. ]
  106. }
  107. ]
  108. }
  109. }

HTTP-多条件查询 & 范围查询

1. 多条件查询

假设想找出小米牌子,价格为3999元的。(must相当于数据库的&&)
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[{
  5. "match":{
  6. "category":"小米"
  7. }
  8. },{
  9. "match":{
  10. "price":3999.00
  11. }
  12. }]
  13. }
  14. }
  15. }

返回结果如下:

  1. {
  2. "took": 134,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 2.3862944,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 2.3862944,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. }
  29. ]
  30. }
  31. }

假设想找出小米和华为的牌子。(should相当于数据库的||)
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "bool":{
  4. "should":[{
  5. "match":{
  6. "category":"小米"
  7. }
  8. },{
  9. "match":{
  10. "category":"华为"
  11. }
  12. }]
  13. },
  14. "filter":{
  15. "range":{
  16. "price":{
  17. "gt":2000
  18. }
  19. }
  20. }
  21. }
  22. }

返回结果如下:

  1. {
  2. "took": 8,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3862942,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1.3862942,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1.3862942,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 1.3862942,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. },
  53. {
  54. "_index": "shopping",
  55. "_type": "_doc",
  56. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  57. "_score": 1.3862942,
  58. "_source": {
  59. "title": "华为手机",
  60. "category": "华为",
  61. "images": "http://www.gulixueyuan.com/xm.jpg",
  62. "price": 1999
  63. }
  64. },
  65. {
  66. "_index": "shopping",
  67. "_type": "_doc",
  68. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  69. "_score": 1.3862942,
  70. "_source": {
  71. "title": "华为手机",
  72. "category": "华为",
  73. "images": "http://www.gulixueyuan.com/xm.jpg",
  74. "price": 1999
  75. }
  76. },
  77. {
  78. "_index": "shopping",
  79. "_type": "_doc",
  80. "_id": "CdR7sHgBaKNfVnMbsJb9",
  81. "_score": 1.3862942,
  82. "_source": {
  83. "title": "华为手机",
  84. "category": "华为",
  85. "images": "http://www.gulixueyuan.com/xm.jpg",
  86. "price": 1999
  87. }
  88. }
  89. ]
  90. }
  91. }

2.范围查询

假设想找出小米和华为的牌子,价格大于2000元的手机。
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "bool":{
  4. "should":[{
  5. "match":{
  6. "category":"小米"
  7. }
  8. },{
  9. "match":{
  10. "category":"华为"
  11. }
  12. }],
  13. "filter":{
  14. "range":{
  15. "price":{
  16. "gt":2000
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }

返回结果如下:

  1. {
  2. "took": 72,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3862942,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1.3862942,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. }
  29. ]
  30. }
  31. }

HTTP-全文检索 & 完全匹配 & 高亮查询

1. 全文检索

这功能像搜索引擎那样,如品牌输入“小华”,返回结果带回品牌有“小米”和华为的。
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match":{
  4. "category" : "小华"
  5. }
  6. }
  7. }

返回结果:

  1. {
  2. "took": 7,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.6931471,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 0.6931471,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 0.6931471,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 0.6931471,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. },
  53. {
  54. "_index": "shopping",
  55. "_type": "_doc",
  56. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  57. "_score": 0.6931471,
  58. "_source": {
  59. "title": "华为手机",
  60. "category": "华为",
  61. "images": "http://www.gulixueyuan.com/xm.jpg",
  62. "price": 1999
  63. }
  64. },
  65. {
  66. "_index": "shopping",
  67. "_type": "_doc",
  68. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  69. "_score": 0.6931471,
  70. "_source": {
  71. "title": "华为手机",
  72. "category": "华为",
  73. "images": "http://www.gulixueyuan.com/xm.jpg",
  74. "price": 1999
  75. }
  76. },
  77. {
  78. "_index": "shopping",
  79. "_type": "_doc",
  80. "_id": "CdR7sHgBaKNfVnMbsJb9",
  81. "_score": 0.6931471,
  82. "_source": {
  83. "title": "华为手机",
  84. "category": "华为",
  85. "images": "http://www.gulixueyuan.com/xm.jpg",
  86. "price": 1999
  87. }
  88. }
  89. ]
  90. }
  91. }

2. 完全匹配

在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match_phrase":{
  4. "category" : "为"
  5. }
  6. }
  7. }

返回结果如下:

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.6931471,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  21. "_score": 0.6931471,
  22. "_source": {
  23. "title": "华为手机",
  24. "category": "华为",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 1999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  33. "_score": 0.6931471,
  34. "_source": {
  35. "title": "华为手机",
  36. "category": "华为",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "CdR7sHgBaKNfVnMbsJb9",
  45. "_score": 0.6931471,
  46. "_source": {
  47. "title": "华为手机",
  48. "category": "华为",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. }
  53. ]
  54. }
  55. }

3. 高亮查询

在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "query":{
  3. "match_phrase":{
  4. "category" : "为"
  5. }
  6. },
  7. "highlight":{
  8. "fields":{
  9. "category":{}//<----高亮这字段
  10. }
  11. }
  12. }

返回结果如下:

  1. {
  2. "took": 100,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.6931471,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  21. "_score": 0.6931471,
  22. "_source": {
  23. "title": "华为手机",
  24. "category": "华为",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 1999
  27. },
  28. "highlight": {
  29. "category": [
  30. "华<em>为</em>"//<------高亮一个为字。
  31. ]
  32. }
  33. },
  34. {
  35. "_index": "shopping",
  36. "_type": "_doc",
  37. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  38. "_score": 0.6931471,
  39. "_source": {
  40. "title": "华为手机",
  41. "category": "华为",
  42. "images": "http://www.gulixueyuan.com/xm.jpg",
  43. "price": 1999
  44. },
  45. "highlight": {
  46. "category": [
  47. "华<em>为</em>"
  48. ]
  49. }
  50. },
  51. {
  52. "_index": "shopping",
  53. "_type": "_doc",
  54. "_id": "CdR7sHgBaKNfVnMbsJb9",
  55. "_score": 0.6931471,
  56. "_source": {
  57. "title": "华为手机",
  58. "category": "华为",
  59. "images": "http://www.gulixueyuan.com/xm.jpg",
  60. "price": 1999
  61. },
  62. "highlight": {
  63. "category": [
  64. "华<em>为</em>"
  65. ]
  66. }
  67. }
  68. ]
  69. }
  70. }

HTTP-聚合查询

聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值max、平均值avg等等。

接下来按price字段进行分组:

在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "aggs":{//聚合操作
  3. "price_group":{//名称,随意起名
  4. "terms":{//分组
  5. "field":"price"//分组字段
  6. }
  7. }
  8. }
  9. }

返回结果如下:

  1. {
  2. "took": 63,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_type": "_doc",
  20. "_id": "ANQqsHgBaKNfVnMbhZYU",
  21. "_score": 1,
  22. "_source": {
  23. "title": "小米手机",
  24. "category": "小米",
  25. "images": "http://www.gulixueyuan.com/xm.jpg",
  26. "price": 3999
  27. }
  28. },
  29. {
  30. "_index": "shopping",
  31. "_type": "_doc",
  32. "_id": "A9R5sHgBaKNfVnMb25Ya",
  33. "_score": 1,
  34. "_source": {
  35. "title": "小米手机",
  36. "category": "小米",
  37. "images": "http://www.gulixueyuan.com/xm.jpg",
  38. "price": 1999
  39. }
  40. },
  41. {
  42. "_index": "shopping",
  43. "_type": "_doc",
  44. "_id": "BNR5sHgBaKNfVnMb7pal",
  45. "_score": 1,
  46. "_source": {
  47. "title": "小米手机",
  48. "category": "小米",
  49. "images": "http://www.gulixueyuan.com/xm.jpg",
  50. "price": 1999
  51. }
  52. },
  53. {
  54. "_index": "shopping",
  55. "_type": "_doc",
  56. "_id": "BtR6sHgBaKNfVnMbX5Y5",
  57. "_score": 1,
  58. "_source": {
  59. "title": "华为手机",
  60. "category": "华为",
  61. "images": "http://www.gulixueyuan.com/xm.jpg",
  62. "price": 1999
  63. }
  64. },
  65. {
  66. "_index": "shopping",
  67. "_type": "_doc",
  68. "_id": "B9R6sHgBaKNfVnMbZpZ6",
  69. "_score": 1,
  70. "_source": {
  71. "title": "华为手机",
  72. "category": "华为",
  73. "images": "http://www.gulixueyuan.com/xm.jpg",
  74. "price": 1999
  75. }
  76. },
  77. {
  78. "_index": "shopping",
  79. "_type": "_doc",
  80. "_id": "CdR7sHgBaKNfVnMbsJb9",
  81. "_score": 1,
  82. "_source": {
  83. "title": "华为手机",
  84. "category": "华为",
  85. "images": "http://www.gulixueyuan.com/xm.jpg",
  86. "price": 1999
  87. }
  88. }
  89. ]
  90. },
  91. "aggregations": {
  92. "price_group": {
  93. "doc_count_error_upper_bound": 0,
  94. "sum_other_doc_count": 0,
  95. "buckets": [
  96. {
  97. "key": 1999,
  98. "doc_count": 5
  99. },
  100. {
  101. "key": 3999,
  102. "doc_count": 1
  103. }
  104. ]
  105. }
  106. }
  107. }

上面返回结果会附带原始数据的。若不想要不附带原始数据的结果,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "aggs":{
  3. "price_group":{
  4. "terms":{
  5. "field":"price"
  6. }
  7. }
  8. },
  9. "size":0
  10. }

返回结果如下:
/

  1. {
  2. "took": 60,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": []
  17. },
  18. "aggregations": {
  19. "price_group": {
  20. "doc_count_error_upper_bound": 0,
  21. "sum_other_doc_count": 0,
  22. "buckets": [
  23. {
  24. "key": 1999,
  25. "doc_count": 5
  26. },
  27. {
  28. "key": 3999,
  29. "doc_count": 1
  30. }
  31. ]
  32. }
  33. }
  34. }

若想对所有手机价格求平均值。

在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

  1. {
  2. "aggs":{
  3. "price_avg":{//名称,随意起名
  4. "avg":{//求平均
  5. "field":"price"
  6. }
  7. }
  8. },
  9. "size":0
  10. }

/
返回结果如下:

  1. {
  2. "took": 14,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": []
  17. },
  18. "aggregations": {
  19. "price_avg": {
  20. "value": 2332.3333333333335
  21. }
  22. }
  23. }

HTTP-映射关系

有了索引库,等于有了数据库中的 database。

接下来就需要建索引库(index)中的映射了,类似于数据库(database)中的表结构(table)。

创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。

先创建一个索引:

PUT http://127.0.0.1:9200/user

返回结果:

  1. {
  2. "acknowledged": true,
  3. "shards_acknowledged": true,
  4. "index": "user"
  5. }

1. 创建映射

PUT http://127.0.0.1:9200/user/_mapping

  1. {
  2. "properties": {
  3. "name":{
  4. "type": "text",
  5. "index": true
  6. },
  7. "sex":{
  8. "type": "keyword",
  9. "index": true
  10. },
  11. "tel":{
  12. "type": "keyword",
  13. "index": false
  14. }
  15. }
  16. }

返回结果如下:

  1. {
  2. "acknowledged": true
  3. }

2. 查询映射

GET http://127.0.0.1:9200/user/_mapping

返回结果如下:

  1. {
  2. "user": {
  3. "mappings": {
  4. "properties": {
  5. "name": {
  6. "type": "text"
  7. },
  8. "sex": {
  9. "type": "keyword"
  10. },
  11. "tel": {
  12. "type": "keyword",
  13. "index": false
  14. }
  15. }
  16. }
  17. }
  18. }

/

3. 增加数据

PUT http://127.0.0.1:9200/user/_create/1001
//

  1. {
  2. "name":"小米",
  3. "sex":"男的",
  4. "tel":"1111"
  5. }

返回结果如下:

  1. {
  2. "_index": "user",
  3. "_type": "_doc",
  4. "_id": "1001",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 0,
  13. "_primary_term": 1
  14. }

4. 查找name含有”小“数据:

GET http://127.0.0.1:9200/user/_search

  1. {
  2. "query":{
  3. "match":{
  4. "name":"小"
  5. }
  6. }
  7. }

返回结果如下:

  1. {
  2. "took": 495,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.2876821,
  16. "hits": [
  17. {
  18. "_index": "user",
  19. "_type": "_doc",
  20. "_id": "1001",
  21. "_score": 0.2876821,
  22. "_source": {
  23. "name": "小米",
  24. "sex": "男的",
  25. "tel": "1111"
  26. }
  27. }
  28. ]
  29. }
  30. }

5. 查找sex含有”男“数据:

GET http://127.0.0.1:9200/user/_search
//

  1. {
  2. "query":{
  3. "match":{
  4. "sex":"男"
  5. }
  6. }
  7. }

返回结果如下:

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 0,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": []
  17. }
  18. }

得到不想要的结果,只因创建映射时”sex”的类型为”keyword”。

“sex”只能完全为”男的“,才能得出原数据。

GET http://127.0.0.1:9200/user/_search

  1. {
  2. "query":{
  3. "match":{
  4. "sex":"男的"
  5. }
  6. }
  7. }

返回结果如下:

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.2876821,
  16. "hits": [
  17. {
  18. "_index": "user",
  19. "_type": "_doc",
  20. "_id": "1001",
  21. "_score": 0.2876821,
  22. "_source": {
  23. "name": "小米",
  24. "sex": "男的",
  25. "tel": "1111"
  26. }
  27. }
  28. ]
  29. }
  30. }

6. 查询电话

GET http://127.0.0.1:9200/user/_search

  1. {
  2. "query":{
  3. "match":{
  4. "tel":"11"
  5. }
  6. }
  7. }

返回结果如下:

  1. {
  2. "error": {
  3. "root_cause": [
  4. {
  5. "type": "query_shard_exception",
  6. "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
  7. "index_uuid": "ivLnMfQKROS7Skb2MTFOew",
  8. "index": "user"
  9. }
  10. ],
  11. "type": "search_phase_execution_exception",
  12. "reason": "all shards failed",
  13. "phase": "query",
  14. "grouped": true,
  15. "failed_shards": [
  16. {
  17. "shard": 0,
  18. "index": "user",
  19. "node": "4P7dIRfXSbezE5JTiuylew",
  20. "reason": {
  21. "type": "query_shard_exception",
  22. "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
  23. "index_uuid": "ivLnMfQKROS7Skb2MTFOew",
  24. "index": "user",
  25. "caused_by": {
  26. "type": "illegal_argument_exception",
  27. "reason": "Cannot search on field [tel] since it is not indexed."
  28. }
  29. }
  30. }
  31. ]
  32. },
  33. "status": 400
  34. }

报错只因创建映射时”tel”的”index”为false,不允许索引。