Create

自动生成文档id

通过 POST index_name/_doc 系统会自动生成document id

  1. PUT index_name/_doc
  2. {
  3. "xxxx": "xxxx",
  4. .....
  5. }

指定文档id

通过 PUT index_name/_create/doc_id ,例如doc_id为1,则可以指定id创建。如果该id对应的文档已经存在,则操作失败

  1. PUT custome_users/_create/1
  2. {
  3. "username": "xiaoming"
  4. }
  5. ## 返回
  6. {
  7. "_index" : "custome_users",
  8. "_type" : "_doc",
  9. "_id" : "1",
  10. "_version" : 1,
  11. "result" : "created",
  12. "_shards" : {
  13. "total" : 2,
  14. "successful" : 1,
  15. "failed" : 0
  16. },
  17. "_seq_no" : 0,
  18. "_primary_term" : 1
  19. }
  20. ## 再次执行
  21. PUT custome_users/_create/1
  22. {
  23. "username": "xiaoming"
  24. }
  25. ## 结果
  26. {
  27. "error": {
  28. "root_cause": [
  29. {
  30. "type": "version_conflict_engine_exception",
  31. "reason": "[1]: version conflict, document already exists (current version [1])",
  32. "index_uuid": "yT6HwezMQuiW2F_Rv_sw6Q",
  33. "shard": "0",
  34. "index": "custome_users"
  35. }
  36. ],
  37. "type": "version_conflict_engine_exception",
  38. "reason": "[1]: version conflict, document already exists (current version [1])",
  39. "index_uuid": "yT6HwezMQuiW2F_Rv_sw6Q",
  40. "shard": "0",
  41. "index": "custome_users"
  42. },
  43. "status": 409
  44. }

Get

GET custome_users/_doc/1

找到文档,HTTP 200

结果

  1. GET custome_users/_doc/1
  2. {
  3. "_index" : "custome_users",
  4. "_type" : "_doc",
  5. "_id" : "1",
  6. "_version" : 1,
  7. "_seq_no" : 0,
  8. "_primary_term" : 1,
  9. "found" : true,
  10. "_source" : {
  11. "username" : "xiaoming"
  12. }
  13. }

找不到文档,HTTP 404

  1. GET custome_users/_doc/2
  2. {
  3. "_index" : "custome_users",
  4. "_type" : "_doc",
  5. "_id" : "2",
  6. "found" : false
  7. }

Index

Index 和 Create不同的地方:如果文档不存在,则索引新的文档,否则删除现有文档,新的文档被索引,版本+1

  1. PUT custome_users/_doc/1
  2. {
  3. "fullname": "xiaoming_new"
  4. }
  5. # 可以看到"_version"的值为2
  6. {
  7. "_index" : "custome_users",
  8. "_type" : "_doc",
  9. "_id" : "1",
  10. "_version" : 2,
  11. "result" : "updated",
  12. "_shards" : {
  13. "total" : 2,
  14. "successful" : 1,
  15. "failed" : 0
  16. },
  17. "_seq_no" : 1,
  18. "_primary_term" : 1
  19. }
  20. # GET custome_users/_doc/1, 可以看到原来的username字段没有了,说明原文档被删除,增加了新的温昂
  21. {
  22. "_index" : "custome_users",
  23. "_type" : "_doc",
  24. "_id" : "1",
  25. "_version" : 2,
  26. "_seq_no" : 3,
  27. "_primary_term" : 1,
  28. "found" : true,
  29. "_source" : {
  30. "fullname" : "xiaoming"
  31. }
  32. }

Update

  • Update方法不会删除原来的文档,是真正的更新数据
  • POST方法 / Payload需要包含在 doc 中
  • 可以用来新增字段
POST custome_users/_update/1
{
  "doc": {
    "username": "xiaoming_new_v3"
  }
}
# "_version"字段又增加了1
{
  "_index" : "custome_users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
# GET custome_users/_doc/1  可以看到,fullname字段没有丢失,说明是纯更新,没有删掉原文档
{
  "_index" : "custome_users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "fullname" : "xiaoming",
    "username" : "xiaoming_new_v3"
  }
}

Bulk API - 批量操作

mget - 批量读取

官方文档
批量操作,减少网络连接所产生的开销,提高性能

GET _mget
{
  "docs": [
    {
      "_index": "custome_users",
      "_id": 1
    },
    {
      "_index": "custome_users",
      "_id": 2
    }
  ]
}