index_.asciidoc 6.1 KB

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