update.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. [[java-update-api]]
  2. == Update API
  3. You can either create an `UpdateRequest` and send it to the client:
  4. [source,java]
  5. --------------------------------------------------
  6. UpdateRequest updateRequest = new UpdateRequest();
  7. updateRequest.index("index");
  8. updateRequest.type("type");
  9. updateRequest.id("1");
  10. updateRequest.doc(jsonBuilder()
  11. .startObject()
  12. .field("gender", "male")
  13. .endObject());
  14. client.update(updateRequest).get();
  15. --------------------------------------------------
  16. Or you can use `prepareUpdate()` method:
  17. [source,java]
  18. --------------------------------------------------
  19. client.prepareUpdate("ttl", "doc", "1")
  20. .setScript(new Script("ctx._source.gender = \"male\"" <1> , ScriptService.ScriptType.INLINE, null, null))
  21. .get();
  22. client.prepareUpdate("ttl", "doc", "1")
  23. .setDoc(jsonBuilder() <2>
  24. .startObject()
  25. .field("gender", "male")
  26. .endObject())
  27. .get();
  28. --------------------------------------------------
  29. <1> Your script. It could also be a locally stored script name.
  30. In that case, you'll need to use `ScriptService.ScriptType.FILE`
  31. <2> Document which will be merged to the existing one.
  32. Note that you can't provide both `script` and `doc`.
  33. [[java-update-api-script]]
  34. === Update by script
  35. The update API allows to update a document based on a script provided:
  36. [source,java]
  37. --------------------------------------------------
  38. UpdateRequest updateRequest = new UpdateRequest("ttl", "doc", "1")
  39. .script(new Script("ctx._source.gender = \"male\""));
  40. client.update(updateRequest).get();
  41. --------------------------------------------------
  42. [[java-update-api-merge-docs]]
  43. === Update by merging documents
  44. The update API also support passing a partial document, which will be merged into the existing document (simple
  45. recursive merge, inner merging of objects, replacing core "keys/values" and arrays). For example:
  46. [source,java]
  47. --------------------------------------------------
  48. UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
  49. .doc(jsonBuilder()
  50. .startObject()
  51. .field("gender", "male")
  52. .endObject());
  53. client.update(updateRequest).get();
  54. --------------------------------------------------
  55. [[java-update-api-upsert]]
  56. === Upsert
  57. There is also support for `upsert`. If the document does not already exists, the content of the `upsert`
  58. element will be used to index the fresh doc:
  59. [source,java]
  60. --------------------------------------------------
  61. IndexRequest indexRequest = new IndexRequest("index", "type", "1")
  62. .source(jsonBuilder()
  63. .startObject()
  64. .field("name", "Joe Smith")
  65. .field("gender", "male")
  66. .endObject());
  67. UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
  68. .doc(jsonBuilder()
  69. .startObject()
  70. .field("gender", "male")
  71. .endObject())
  72. .upsert(indexRequest); <1>
  73. client.update(updateRequest).get();
  74. --------------------------------------------------
  75. <1> If the document does not exist, the one in `indexRequest` will be added
  76. If the document `index/type/1` already exists, we will have after this operation a document like:
  77. [source,js]
  78. --------------------------------------------------
  79. {
  80. "name" : "Joe Dalton",
  81. "gender": "male" <1>
  82. }
  83. --------------------------------------------------
  84. <1> This field is added by the update request
  85. If it does not exist, we will have a new document:
  86. [source,js]
  87. --------------------------------------------------
  88. {
  89. "name" : "Joe Smith",
  90. "gender": "male"
  91. }
  92. --------------------------------------------------