index_.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. [[java-docs-index]]
  2. === Index API
  3. The index API allows one to index a typed JSON document into a specific
  4. index and make it searchable.
  5. [[java-docs-index-generate]]
  6. ==== Generate JSON document
  7. There are several different ways of generating a JSON document:
  8. * Manually (aka do it yourself) using native `byte[]` or as a `String`
  9. * Using a `Map` that will be automatically converted to its JSON
  10. equivalent
  11. * Using a third party library to serialize your beans such as
  12. http://wiki.fasterxml.com/JacksonHome[Jackson]
  13. * Using built-in helpers XContentFactory.jsonBuilder()
  14. Internally, each type is converted to `byte[]` (so a String is converted
  15. to a `byte[]`). Therefore, if the object is in this form already, then
  16. use it. The `jsonBuilder` is highly optimized JSON generator that
  17. directly constructs a `byte[]`.
  18. [[java-docs-index-generate-diy]]
  19. ===== Do It Yourself
  20. Nothing really difficult here but note that you will have to encode
  21. dates according to the
  22. {ref}/mapping-date-format.html[Date Format].
  23. [source,java]
  24. --------------------------------------------------
  25. String json = "{" +
  26. "\"user\":\"kimchy\"," +
  27. "\"postDate\":\"2013-01-30\"," +
  28. "\"message\":\"trying out Elasticsearch\"" +
  29. "}";
  30. --------------------------------------------------
  31. [[java-docs-index-generate-using-map]]
  32. ===== Using Map
  33. Map is a key:values pair collection. It represents a JSON structure:
  34. [source,java]
  35. --------------------------------------------------
  36. Map<String, Object> json = new HashMap<String, Object>();
  37. json.put("user","kimchy");
  38. json.put("postDate",new Date());
  39. json.put("message","trying out Elasticsearch");
  40. --------------------------------------------------
  41. [[java-docs-index-generate-beans]]
  42. ===== Serialize your beans
  43. Elasticsearch already uses http://wiki.fasterxml.com/JacksonHome[Jackson].
  44. So you can use it to serialize your beans to JSON:
  45. [source,java]
  46. --------------------------------------------------
  47. import com.fasterxml.jackson.databind.*;
  48. // instance a json mapper
  49. ObjectMapper mapper = new ObjectMapper(); // create once, reuse
  50. // generate json
  51. byte[] json = mapper.writeValueAsBytes(yourbeaninstance);
  52. --------------------------------------------------
  53. [[java-docs-index-generate-helpers]]
  54. ===== Use Elasticsearch helpers
  55. Elasticsearch provides built-in helpers to generate JSON content.
  56. [source,java]
  57. --------------------------------------------------
  58. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  59. XContentBuilder builder = jsonBuilder()
  60. .startObject()
  61. .field("user", "kimchy")
  62. .field("postDate", new Date())
  63. .field("message", "trying out Elasticsearch")
  64. .endObject()
  65. --------------------------------------------------
  66. Note that you can also add arrays with `startArray(String)` and
  67. `endArray()` methods. By the way, the `field` method +
  68. accepts many object types. You can directly pass numbers, dates and even
  69. other XContentBuilder objects.
  70. If you need to see the generated JSON content, you can use the
  71. `string()` method.
  72. [source,java]
  73. --------------------------------------------------
  74. String json = builder.string();
  75. --------------------------------------------------
  76. [[java-docs-index-doc]]
  77. ==== Index document
  78. The following example indexes a JSON document into an index called
  79. twitter, under a type called tweet, with id valued 1:
  80. [source,java]
  81. --------------------------------------------------
  82. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  83. IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
  84. .setSource(jsonBuilder()
  85. .startObject()
  86. .field("user", "kimchy")
  87. .field("postDate", new Date())
  88. .field("message", "trying out Elasticsearch")
  89. .endObject()
  90. )
  91. .get();
  92. --------------------------------------------------
  93. Note that you can also index your documents as JSON String and that you
  94. don't have to give an ID:
  95. [source,java]
  96. --------------------------------------------------
  97. String json = "{" +
  98. "\"user\":\"kimchy\"," +
  99. "\"postDate\":\"2013-01-30\"," +
  100. "\"message\":\"trying out Elasticsearch\"" +
  101. "}";
  102. IndexResponse response = client.prepareIndex("twitter", "tweet")
  103. .setSource(json)
  104. .get();
  105. --------------------------------------------------
  106. `IndexResponse` object will give you a report:
  107. [source,java]
  108. --------------------------------------------------
  109. // Index name
  110. String _index = response.getIndex();
  111. // Type name
  112. String _type = response.getType();
  113. // Document ID (generated or not)
  114. String _id = response.getId();
  115. // Version (if it's the first time you index this document, you will get: 1)
  116. long _version = response.getVersion();
  117. // isCreated() is true if the document is a new one, false if it has been updated
  118. boolean created = response.isCreated();
  119. --------------------------------------------------
  120. For more information on the index operation, check out the REST
  121. {ref}/docs-index_.html[index] docs.
  122. [[java-docs-index-thread]]
  123. ==== Operation Threading
  124. The index API allows one to set the threading model the operation will be
  125. performed when the actual execution of the API is performed on the same
  126. node (the API is executed on a shard that is allocated on the same
  127. server).
  128. The options are to execute the operation on a different thread, or to
  129. execute it on the calling thread (note that the API is still asynchronous). By
  130. default, `operationThreaded` is set to `true` which means the operation
  131. is executed on a different thread.