一、新增文档

请求

  1. POST http://127.0.0.1:9200/user5/prople

参数

  1. {
  2. "name": "Lina",
  3. "age": "25",
  4. "sex": "N"
  5. }

说明

  • user5:添加到哪个索引
  • prople:添加到哪个type下面

响应

  1. {
  2. "_index": "user5",
  3. "_type": "prople",
  4. "_id": "AW1DSm9smMVd_1XNEVJN",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "created": true
  13. }

说明

  • _id:自动生成的id,可以自己指定(修改文档)
  • _version:版本号,修改一次加一
  • result:created表示新增,updated表示更新 ,deleted 表示删除

二、修改文档

请求

  1. PUT http://127.0.0.1:9200/user5/prople/AW1DSm9smMVd_1XNEVJN

参数

  1. {
  2. "name": "Lina1",
  3. "age": "26",
  4. "sex": "Y"
  5. }

说明

  • user5:添加到哪个索引
  • prople:添加到哪个type下面
  • AW1DSm9smMVd_1XNEVJN:要修改文档的id,如果id不存在,就新增

响应

  1. {
  2. "_index": "user5",
  3. "_type": "prople",
  4. "_id": "AW1DSm9smMVd_1XNEVJN",
  5. "_version": 2,
  6. "result": "updated",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "created": false
  13. }

三、删除文档

请求

  1. DELETE http://127.0.0.1:9200/user5/prople/2

响应

  1. {
  2. "found": true,
  3. "_index": "user5",
  4. "_type": "prople",
  5. "_id": "2",
  6. "_version": 2,
  7. "result": "deleted",
  8. "_shards": {
  9. "total": 2,
  10. "successful": 1,
  11. "failed": 0
  12. }
  13. }

四、查看文档

请求

  1. GET http://127.0.0.1:9200/user5/prople/AW1DSm9smMVd_1XNEVJN

说明

  • user5:添加到哪个索引
  • prople:添加到哪个type下面
  • AW1DSm9smMVd_1XNEVJN:要查看文档的id

    响应

  1. {
  2. "_index": "user5",
  3. "_type": "prople",
  4. "_id": "AW1DSm9smMVd_1XNEVJN",
  5. "_version": 2,
  6. "found": true,
  7. "_source": {
  8. "name": "Lina1",
  9. "age": "26",
  10. "sex": "Y"
  11. }
  12. }

说明

  • found:是否查找到
  • _source:原始信息,找到时候才有

    五、查看全部文档

    请求

  1. GET http://127.0.0.1:9200/user5/prople/_search

响应

  1. {
  2. "took": 9,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 1.0,
  13. "hits": [
  14. {
  15. "_index": "user5",
  16. "_type": "prople",
  17. "_id": "AW1DSm9smMVd_1XNEVJN",
  18. "_score": 1.0,
  19. "_source": {
  20. "name": "Lina1",
  21. "age": "26",
  22. "sex": "Y"
  23. }
  24. }
  25. ]
  26. }
  27. }

说明

  • took:该操作的耗时(单位为毫秒)
  • timed_out:是否超时
  • hits:命中的全部记录
    • total:返回记录数
    • max_score:最高的匹配程度
    • hits:返回的记录组成的数组

六、只查看原始数据

请求

  1. GET http://127.0.0.1:9200/user5/prople/AW1DSm9smMVd_1XNEVJN/_source

响应

  1. {
  2. "name": "Lina1",
  3. "age": "26",
  4. "sex": "Y"
  5. }