Skip to content

Commit cae66fb

Browse files
ubonesskimchy
authored andcommitted
* lucene 4: added missing short support in stream input/output
* lucene 4: added more extensive test for stored fields
1 parent f8842d5 commit cae66fb

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ public Object readGenericValue() throws IOException {
356356
return readBytesReference();
357357
case 15:
358358
return readText();
359+
case 16:
360+
return readShort();
359361
default:
360362
throw new IOException("Can't read unknown type [" + type + "]");
361363
}

src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ public void writeGenericValue(@Nullable Object value) throws IOException {
369369
} else if (value instanceof Text) {
370370
writeByte((byte) 15);
371371
writeText((Text) value);
372+
} else if (value instanceof Short) {
373+
writeByte((byte) 16);
374+
writeShort((Short) value);
372375
} else {
373376
throw new IOException("Can't write type [" + type + "]");
374377
}

src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121

2222
import org.elasticsearch.action.search.SearchResponse;
2323
import org.elasticsearch.client.Client;
24+
import org.elasticsearch.common.Base64;
2425
import org.elasticsearch.common.collect.MapBuilder;
26+
import org.elasticsearch.common.joda.Joda;
2527
import org.elasticsearch.common.xcontent.XContentFactory;
2628
import org.elasticsearch.search.sort.SortOrder;
2729
import org.elasticsearch.test.integration.AbstractNodesTests;
30+
import org.joda.time.DateTime;
31+
import org.joda.time.DateTimeZone;
2832
import org.testng.annotations.AfterClass;
2933
import org.testng.annotations.BeforeClass;
3034
import org.testng.annotations.Test;
@@ -277,4 +281,69 @@ public void testPartialFields() throws Exception {
277281
assertThat(partial2.containsKey("obj1"), equalTo(false));
278282
assertThat(partial2.containsKey("field1"), equalTo(true));
279283
}
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+
}
280349
}

0 commit comments

Comments
 (0)