1
0

index_.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. You can use http://wiki.fasterxml.com/JacksonHome[Jackson] to serialize
  44. your beans to JSON. Please add http://search.maven.org/#search%7Cga%7C1%7Cjackson-databind[Jackson Databind]
  45. to your project. Then you can use `ObjectMapper` to serialize your beans:
  46. [source,java]
  47. --------------------------------------------------
  48. import com.fasterxml.jackson.databind.*;
  49. // instance a json mapper
  50. ObjectMapper mapper = new ObjectMapper(); // create once, reuse
  51. // generate json
  52. byte[] json = mapper.writeValueAsBytes(yourbeaninstance);
  53. --------------------------------------------------
  54. [[java-docs-index-generate-helpers]]
  55. ===== Use Elasticsearch helpers
  56. Elasticsearch provides built-in helpers to generate JSON content.
  57. [source,java]
  58. --------------------------------------------------
  59. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  60. XContentBuilder builder = jsonBuilder()
  61. .startObject()
  62. .field("user", "kimchy")
  63. .field("postDate", new Date())
  64. .field("message", "trying out Elasticsearch")
  65. .endObject()
  66. --------------------------------------------------
  67. Note that you can also add arrays with `startArray(String)` and
  68. `endArray()` methods. By the way, the `field` method +
  69. accepts many object types. You can directly pass numbers, dates and even
  70. other XContentBuilder objects.
  71. If you need to see the generated JSON content, you can use the
  72. `string()` method.
  73. [source,java]
  74. --------------------------------------------------
  75. String json = builder.string();
  76. --------------------------------------------------
  77. [[java-docs-index-doc]]
  78. ==== Index document
  79. The following example indexes a JSON document into an index called
  80. twitter, under a type called tweet, with id valued 1:
  81. [source,java]
  82. --------------------------------------------------
  83. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  84. IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
  85. .setSource(jsonBuilder()
  86. .startObject()
  87. .field("user", "kimchy")
  88. .field("postDate", new Date())
  89. .field("message", "trying out Elasticsearch")
  90. .endObject()
  91. )
  92. .get();
  93. --------------------------------------------------
  94. Note that you can also index your documents as JSON String and that you
  95. don't have to give an ID:
  96. [source,java]
  97. --------------------------------------------------
  98. String json = "{" +
  99. "\"user\":\"kimchy\"," +
  100. "\"postDate\":\"2013-01-30\"," +
  101. "\"message\":\"trying out Elasticsearch\"" +
  102. "}";
  103. IndexResponse response = client.prepareIndex("twitter", "tweet")
  104.       .setSource(json, XContentType.JSON)
  105. .get();
  106. --------------------------------------------------
  107. `IndexResponse` object will give you a report:
  108. [source,java]
  109. --------------------------------------------------
  110. // Index name
  111. String _index = response.getIndex();
  112. // Type name
  113. String _type = response.getType();
  114. // Document ID (generated or not)
  115. String _id = response.getId();
  116. // Version (if it's the first time you index this document, you will get: 1)
  117. long _version = response.getVersion();
  118. // status has stored current instance statement.
  119. RestStatus status = response.status();
  120. --------------------------------------------------
  121. For more information on the index operation, check out the REST
  122. {ref}/docs-index_.html[index] docs.
  123. [[java-docs-index-thread]]
  124. ==== Operation Threading
  125. The index API allows one to set the threading model the operation will be
  126. performed when the actual execution of the API is performed on the same
  127. node (the API is executed on a shard that is allocated on the same
  128. server).
  129. The options are to execute the operation on a different thread, or to
  130. execute it on the calling thread (note that the API is still asynchronous). By
  131. default, `operationThreaded` is set to `true` which means the operation
  132. is executed on a different thread.