Skip to content

Commit f47d62c

Browse files
imotovkimchy
authored andcommitted
Date fields shouldn't be returned as longs by Get API
1 parent d1281d2 commit f47d62c

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/main/java/org/elasticsearch/index/get/ShardGetService.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public GetResult get(String type, String id, String[] gFields, boolean realtime)
108108
* Returns {@link GetResult} based on the specified {@link Engine.GetResult} argument.
109109
* This method basically loads specified fields for the associated document in the engineGetResult.
110110
* This method load the fields from the Lucene index and not from transaction log and therefore isn't realtime.
111-
* <p>
111+
* <p/>
112112
* Note: Call <b>must</b> release engine searcher associated with engineGetResult!
113113
*/
114114
public GetResult get(Engine.GetResult engineGetResult, String id, String type, String[] fields) {
@@ -243,9 +243,6 @@ public GetResult innerGet(String type, String id, String[] gFields, boolean real
243243
// only if the field is stored or source is enabled we should add it..
244244
if (docMapper.sourceMapper().enabled() || x == null || x.stored()) {
245245
value = searchLookup.source().extractValue(field);
246-
if (x != null && value instanceof String) {
247-
value = x.valueFromString((String) value);
248-
}
249246
}
250247
}
251248
}

src/test/java/org/elasticsearch/test/integration/get/GetActionTests.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,60 @@ public void realtimeGetWithCompress() throws Exception {
222222
assertThat(getResponse.exists(), equalTo(true));
223223
assertThat(getResponse.sourceAsMap().get("field").toString(), equalTo(fieldValue));
224224
}
225+
226+
@Test
227+
public void getFieldsWithDifferentTypes() throws Exception {
228+
client.admin().indices().prepareDelete().execute().actionGet();
229+
230+
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
231+
.addMapping("type1", jsonBuilder().startObject().startObject("type").startObject("_source").field("enabled", true).endObject().endObject().endObject())
232+
.addMapping("type2", jsonBuilder().startObject().startObject("type")
233+
.startObject("_source").field("enabled", false).endObject()
234+
.startObject("properties")
235+
.startObject("str").field("type", "string").field("store", "yes").endObject()
236+
.startObject("int").field("type", "integer").field("store", "yes").endObject()
237+
.startObject("date").field("type", "date").field("store", "yes").endObject()
238+
.endObject()
239+
.endObject().endObject())
240+
.execute().actionGet();
241+
242+
ClusterHealthResponse clusterHealth = client.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
243+
assertThat(clusterHealth.timedOut(), equalTo(false));
244+
assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN));
245+
246+
client.prepareIndex("test", "type1", "1").setSource("str", "test", "int", 42, "date", "2012-11-13T15:26:14.000Z").execute().actionGet();
247+
client.prepareIndex("test", "type2", "1").setSource("str", "test", "int", 42, "date", "2012-11-13T15:26:14.000Z").execute().actionGet();
248+
249+
// realtime get with stored source
250+
logger.info("--> realtime get (from source)");
251+
GetResponse getResponse = client.prepareGet("test", "type1", "1").setFields("str", "int", "date").execute().actionGet();
252+
assertThat(getResponse.exists(), equalTo(true));
253+
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
254+
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
255+
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
256+
257+
logger.info("--> realtime get (from stored fields)");
258+
getResponse = client.prepareGet("test", "type2", "1").setFields("str", "int", "date").execute().actionGet();
259+
assertThat(getResponse.exists(), equalTo(true));
260+
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
261+
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
262+
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
263+
264+
logger.info("--> flush the index, so we load it from it");
265+
client.admin().indices().prepareFlush().execute().actionGet();
266+
267+
logger.info("--> non realtime get (from source)");
268+
getResponse = client.prepareGet("test", "type1", "1").setFields("str", "int", "date").execute().actionGet();
269+
assertThat(getResponse.exists(), equalTo(true));
270+
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
271+
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
272+
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
273+
274+
logger.info("--> non realtime get (from stored fields)");
275+
getResponse = client.prepareGet("test", "type2", "1").setFields("str", "int", "date").execute().actionGet();
276+
assertThat(getResponse.exists(), equalTo(true));
277+
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
278+
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
279+
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
280+
}
225281
}

0 commit comments

Comments
 (0)