|
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.search.SearchResponse; |
23 | 23 | import org.elasticsearch.client.Client; |
| 24 | +import org.elasticsearch.common.Base64; |
24 | 25 | import org.elasticsearch.common.collect.MapBuilder; |
| 26 | +import org.elasticsearch.common.joda.Joda; |
25 | 27 | import org.elasticsearch.common.xcontent.XContentFactory; |
26 | 28 | import org.elasticsearch.search.sort.SortOrder; |
27 | 29 | import org.elasticsearch.test.integration.AbstractNodesTests; |
| 30 | +import org.joda.time.DateTime; |
| 31 | +import org.joda.time.DateTimeZone; |
28 | 32 | import org.testng.annotations.AfterClass; |
29 | 33 | import org.testng.annotations.BeforeClass; |
30 | 34 | import org.testng.annotations.Test; |
@@ -277,4 +281,69 @@ public void testPartialFields() throws Exception { |
277 | 281 | assertThat(partial2.containsKey("obj1"), equalTo(false)); |
278 | 282 | assertThat(partial2.containsKey("field1"), equalTo(true)); |
279 | 283 | } |
| 284 | + |
| 285 | + @Test |
| 286 | + public void testStoredFieldsWithoutSource() throws Exception { |
| 287 | + client.admin().indices().prepareDelete().execute().actionGet(); |
| 288 | + client.admin().indices().prepareCreate("test").execute().actionGet(); |
| 289 | + client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(); |
| 290 | + |
| 291 | + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") |
| 292 | + .startObject("_source").field("enabled", false).endObject() |
| 293 | + .startObject("byte_field").field("type", "byte").field("store", "yes").endObject() |
| 294 | + .startObject("short_field").field("type", "short").field("store", "yes").endObject() |
| 295 | + .startObject("integer_field").field("type", "integer").field("store", "yes").endObject() |
| 296 | + .startObject("long_field").field("type", "long").field("store", "yes").endObject() |
| 297 | + .startObject("float_field").field("type", "float").field("store", "yes").endObject() |
| 298 | + .startObject("double_field").field("type", "double").field("store", "yes").endObject() |
| 299 | + .startObject("date_field").field("type", "date").field("store", "yes").endObject() |
| 300 | + .startObject("boolean_field").field("type", "boolean").field("store", "yes").endObject() |
| 301 | + .startObject("binary_field").field("type", "binary").field("store", "yes").endObject() |
| 302 | + .endObject().endObject().endObject().string(); |
| 303 | + |
| 304 | + client.admin().indices().preparePutMapping().setType("type1").setSource(mapping).execute().actionGet(); |
| 305 | + |
| 306 | + client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() |
| 307 | + .field("byte_field", (byte) 1) |
| 308 | + .field("short_field", (short) 2) |
| 309 | + .field("integer_field", 3) |
| 310 | + .field("long_field", 4l) |
| 311 | + .field("float_field", 5.0f) |
| 312 | + .field("double_field", 6.0d) |
| 313 | + .field("date_field", Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC))) |
| 314 | + .field("boolean_field", true) |
| 315 | + .field("binary_field", Base64.encodeBytes("testing text".getBytes("UTF8"))) |
| 316 | + .endObject()).execute().actionGet(); |
| 317 | + |
| 318 | + client.admin().indices().prepareRefresh().execute().actionGet(); |
| 319 | + |
| 320 | + SearchResponse searchResponse = client.prepareSearch().setQuery(matchAllQuery()) |
| 321 | + .addField("byte_field") |
| 322 | + .addField("short_field") |
| 323 | + .addField("integer_field") |
| 324 | + .addField("long_field") |
| 325 | + .addField("float_field") |
| 326 | + .addField("double_field") |
| 327 | + .addField("date_field") |
| 328 | + .addField("boolean_field") |
| 329 | + .addField("binary_field") |
| 330 | + .execute().actionGet(); |
| 331 | + |
| 332 | + assertThat(searchResponse.hits().getTotalHits(), equalTo(1l)); |
| 333 | + assertThat(searchResponse.hits().hits().length, equalTo(1)); |
| 334 | + assertThat(searchResponse.hits().getAt(0).fields().size(), equalTo(9)); |
| 335 | + |
| 336 | + |
| 337 | + assertThat(searchResponse.hits().getAt(0).fields().get("byte_field").value().toString(), equalTo("1")); |
| 338 | + assertThat(searchResponse.hits().getAt(0).fields().get("short_field").value().toString(), equalTo("2")); |
| 339 | + assertThat(searchResponse.hits().getAt(0).fields().get("integer_field").value(), equalTo((Object) 3)); |
| 340 | + assertThat(searchResponse.hits().getAt(0).fields().get("long_field").value(), equalTo((Object) 4l)); |
| 341 | + assertThat(searchResponse.hits().getAt(0).fields().get("float_field").value(), equalTo((Object) 5.0f)); |
| 342 | + assertThat(searchResponse.hits().getAt(0).fields().get("double_field").value(), equalTo((Object) 6.0d)); |
| 343 | + String dateTime = Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC)); |
| 344 | + assertThat(searchResponse.hits().getAt(0).fields().get("date_field").value(), equalTo((Object) dateTime)); |
| 345 | + assertThat(searchResponse.hits().getAt(0).fields().get("boolean_field").value(), equalTo((Object) "true")); |
| 346 | + assertThat(searchResponse.hits().getAt(0).fields().get("binary_field").value().toString(), equalTo(Base64.encodeBytes("testing text".getBytes("UTF8")))); |
| 347 | + |
| 348 | + } |
280 | 349 | } |
0 commit comments