1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
[[index_]]
== Index API
The index API allows one to index a typed JSON document into a specific
index and make it searchable.
[[generate]]
=== Generate JSON document
There are different way of generating JSON document:
* Manually (aka do it yourself) using native `byte[]` or as a `String`
* Using `Map` that will be automatically converted to its JSON
equivalent
* Using a third party library to serialize your beans such as
http://wiki.fasterxml.com/JacksonHome[Jackson]
* Using built-in helpers XContentFactory.jsonBuilder()
Internally, each type is converted to `byte[]` (so a String is converted
to a `byte[]`). Therefore, if the object is in this form already, then
use it. The `jsonBuilder` is highly optimized JSON generator that
directly constructs a `byte[]`.
==== Do It Yourself
Nothing really difficult here but note that you will have to encode
dates regarding to the
{ref}/mapping-date-format.html[Date Format].
[source,java]
--------------------------------------------------
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
--------------------------------------------------
[[using-map]]
==== Using Map
Map is a key:values pair collection. It represents very well a JSON
structure:
[source,java]
--------------------------------------------------
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
--------------------------------------------------
[[beans]]
==== Serialize your beans
Elasticsearch already use Jackson but shade it under
`org.elasticsearch.common.jackson` package. +
So, you can add your own Jackson version in your `pom.xml` file or in
your classpath. See http://wiki.fasterxml.com/JacksonDownload[Jackson
Download Page].
For example:
[source,xml]
--------------------------------------------------
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.3</version>
</dependency>
--------------------------------------------------
Then, you can start serializing your beans to JSON:
[source,java]
--------------------------------------------------
import com.fasterxml.jackson.databind.*;
// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
// generate json
String json = mapper.writeValueAsString(yourbeaninstance);
--------------------------------------------------
[[helpers]]
==== Use Elasticsearch helpers
Elasticsearch provides built-in helpers to generate JSON content.
[source,java]
--------------------------------------------------
import static org.elasticsearch.common.xcontent.XContentFactory.*;
XContentBuilder builder = jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
--------------------------------------------------
Note that you can also add arrays with `startArray(String)` and
`endArray()` methods. By the way, `field` method +
accept many object types. You can pass directly numbers, dates and even
other XContentBuilder objects.
If you need to see the generated JSON content, you can use the
`string()` method.
[source,java]
--------------------------------------------------
String json = builder.string();
--------------------------------------------------
[[index-doc]]
=== Index document
The following example indexes a JSON document into an index called
twitter, under a type called tweet, with id valued 1:
[source,java]
--------------------------------------------------
import static org.elasticsearch.common.xcontent.XContentFactory.*;
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.execute()
.actionGet();
--------------------------------------------------
Note that you can also index your documents as JSON String and that you
don't have to give an ID:
[source,java]
--------------------------------------------------
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
IndexResponse response = client.prepareIndex("twitter", "tweet")
.setSource(json)
.execute()
.actionGet();
--------------------------------------------------
`IndexResponse` object will give you report:
[source,java]
--------------------------------------------------
// Index name
String _index = response.index();
// Type name
String _type = response.type();
// Document ID (generated or not)
String _id = response.id();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.version();
--------------------------------------------------
If you use percolation while indexing, `IndexResponse` object will give
you percolator that have matched:
[source,java]
--------------------------------------------------
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(json)
.execute()
.actionGet();
List<String> matches = response.matches();
--------------------------------------------------
For more information on the index operation, check out the REST
{ref}/docs-index_.html[index] docs.
=== Operation Threading
The index API allows to set the threading model the operation will be
performed when the actual execution of the API is performed on the same
node (the API is executed on a shard that is allocated on the same
server).
The options are to execute the operation on a different thread, or to
execute it on the calling thread (note that the API is still async). By
default, `operationThreaded` is set to `true` which means the operation
is executed on a different thread.
|