index_.asciidoc 6.1 KB

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