index_.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. [[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. [[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. ==== Do It Yourself
  19. Nothing really difficult here but note that you will have to encode
  20. dates according to the
  21. {ref}/mapping-date-format.html[Date Format].
  22. [source,java]
  23. --------------------------------------------------
  24. String json = "{" +
  25. "\"user\":\"kimchy\"," +
  26. "\"postDate\":\"2013-01-30\"," +
  27. "\"message\":\"trying out Elasticsearch\"" +
  28. "}";
  29. --------------------------------------------------
  30. [[using-map]]
  31. ==== Using Map
  32. Map is a key:values pair collection. It represents a JSON structure:
  33. [source,java]
  34. --------------------------------------------------
  35. Map<String, Object> json = new HashMap<String, Object>();
  36. json.put("user","kimchy");
  37. json.put("postDate",new Date());
  38. json.put("message","trying out Elasticsearch");
  39. --------------------------------------------------
  40. [[beans]]
  41. ==== Serialize your beans
  42. Elasticsearch already uses Jackson but shades it under
  43. `org.elasticsearch.common.jackson` package. +
  44. So, you can add your own Jackson version in your `pom.xml` file or in
  45. your classpath. See http://wiki.fasterxml.com/JacksonDownload[Jackson
  46. Download Page].
  47. For example:
  48. [source,xml]
  49. --------------------------------------------------
  50. <dependency>
  51. <groupId>com.fasterxml.jackson.core</groupId>
  52. <artifactId>jackson-databind</artifactId>
  53. <version>2.1.3</version>
  54. </dependency>
  55. --------------------------------------------------
  56. Then, you can start serializing your beans to JSON:
  57. [source,java]
  58. --------------------------------------------------
  59. import com.fasterxml.jackson.databind.*;
  60. // instance a json mapper
  61. ObjectMapper mapper = new ObjectMapper(); // create once, reuse
  62. // generate json
  63. byte[] json = mapper.writeValueAsBytes(yourbeaninstance);
  64. --------------------------------------------------
  65. [[helpers]]
  66. ==== Use Elasticsearch helpers
  67. Elasticsearch provides built-in helpers to generate JSON content.
  68. [source,java]
  69. --------------------------------------------------
  70. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  71. XContentBuilder builder = jsonBuilder()
  72. .startObject()
  73. .field("user", "kimchy")
  74. .field("postDate", new Date())
  75. .field("message", "trying out Elasticsearch")
  76. .endObject()
  77. --------------------------------------------------
  78. Note that you can also add arrays with `startArray(String)` and
  79. `endArray()` methods. By the way, the `field` method +
  80. accepts many object types. You can directly pass numbers, dates and even
  81. other XContentBuilder objects.
  82. If you need to see the generated JSON content, you can use the
  83. `string()` method.
  84. [source,java]
  85. --------------------------------------------------
  86. String json = builder.string();
  87. --------------------------------------------------
  88. [[index-doc]]
  89. === Index document
  90. The following example indexes a JSON document into an index called
  91. twitter, under a type called tweet, with id valued 1:
  92. [source,java]
  93. --------------------------------------------------
  94. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  95. IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
  96. .setSource(jsonBuilder()
  97. .startObject()
  98. .field("user", "kimchy")
  99. .field("postDate", new Date())
  100. .field("message", "trying out Elasticsearch")
  101. .endObject()
  102. )
  103. .execute()
  104. .actionGet();
  105. --------------------------------------------------
  106. Note that you can also index your documents as JSON String and that you
  107. don't have to give an ID:
  108. [source,java]
  109. --------------------------------------------------
  110. String json = "{" +
  111. "\"user\":\"kimchy\"," +
  112. "\"postDate\":\"2013-01-30\"," +
  113. "\"message\":\"trying out Elasticsearch\"" +
  114. "}";
  115. IndexResponse response = client.prepareIndex("twitter", "tweet")
  116. .setSource(json)
  117. .execute()
  118. .actionGet();
  119. --------------------------------------------------
  120. `IndexResponse` object will give you a report:
  121. [source,java]
  122. --------------------------------------------------
  123. // Index name
  124. String _index = response.getIndex();
  125. // Type name
  126. String _type = response.getType();
  127. // Document ID (generated or not)
  128. String _id = response.getId();
  129. // Version (if it's the first time you index this document, you will get: 1)
  130. long _version = response.getVersion();
  131. // isCreated() is true if the document is a new one, false if it has been updated
  132. boolean created = response.isCreated();
  133. --------------------------------------------------
  134. For more information on the index operation, check out the REST
  135. {ref}/docs-index_.html[index] docs.
  136. === Operation Threading
  137. The index API allows one to set the threading model the operation will be
  138. performed when the actual execution of the API is performed on the same
  139. node (the API is executed on a shard that is allocated on the same
  140. server).
  141. The options are to execute the operation on a different thread, or to
  142. execute it on the calling thread (note that the API is still asynchronous). By
  143. default, `operationThreaded` is set to `true` which means the operation
  144. is executed on a different thread.