Date fields shouldn't be returned as longs by Get API

This commit is contained in:
Igor Motov 2012-11-13 14:26:49 -05:00 committed by Shay Banon
parent d1281d283b
commit f47d62cc30
2 changed files with 57 additions and 4 deletions

View File

@ -108,7 +108,7 @@ public class ShardGetService extends AbstractIndexShardComponent {
* Returns {@link GetResult} based on the specified {@link Engine.GetResult} argument.
* This method basically loads specified fields for the associated document in the engineGetResult.
* This method load the fields from the Lucene index and not from transaction log and therefore isn't realtime.
* <p>
* <p/>
* Note: Call <b>must</b> release engine searcher associated with engineGetResult!
*/
public GetResult get(Engine.GetResult engineGetResult, String id, String type, String[] fields) {
@ -243,9 +243,6 @@ public class ShardGetService extends AbstractIndexShardComponent {
// only if the field is stored or source is enabled we should add it..
if (docMapper.sourceMapper().enabled() || x == null || x.stored()) {
value = searchLookup.source().extractValue(field);
if (x != null && value instanceof String) {
value = x.valueFromString((String) value);
}
}
}
}

View File

@ -222,4 +222,60 @@ public class GetActionTests extends AbstractNodesTests {
assertThat(getResponse.exists(), equalTo(true));
assertThat(getResponse.sourceAsMap().get("field").toString(), equalTo(fieldValue));
}
@Test
public void getFieldsWithDifferentTypes() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
.addMapping("type1", jsonBuilder().startObject().startObject("type").startObject("_source").field("enabled", true).endObject().endObject().endObject())
.addMapping("type2", jsonBuilder().startObject().startObject("type")
.startObject("_source").field("enabled", false).endObject()
.startObject("properties")
.startObject("str").field("type", "string").field("store", "yes").endObject()
.startObject("int").field("type", "integer").field("store", "yes").endObject()
.startObject("date").field("type", "date").field("store", "yes").endObject()
.endObject()
.endObject().endObject())
.execute().actionGet();
ClusterHealthResponse clusterHealth = client.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
assertThat(clusterHealth.timedOut(), equalTo(false));
assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN));
client.prepareIndex("test", "type1", "1").setSource("str", "test", "int", 42, "date", "2012-11-13T15:26:14.000Z").execute().actionGet();
client.prepareIndex("test", "type2", "1").setSource("str", "test", "int", 42, "date", "2012-11-13T15:26:14.000Z").execute().actionGet();
// realtime get with stored source
logger.info("--> realtime get (from source)");
GetResponse getResponse = client.prepareGet("test", "type1", "1").setFields("str", "int", "date").execute().actionGet();
assertThat(getResponse.exists(), equalTo(true));
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
logger.info("--> realtime get (from stored fields)");
getResponse = client.prepareGet("test", "type2", "1").setFields("str", "int", "date").execute().actionGet();
assertThat(getResponse.exists(), equalTo(true));
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
logger.info("--> flush the index, so we load it from it");
client.admin().indices().prepareFlush().execute().actionGet();
logger.info("--> non realtime get (from source)");
getResponse = client.prepareGet("test", "type1", "1").setFields("str", "int", "date").execute().actionGet();
assertThat(getResponse.exists(), equalTo(true));
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
logger.info("--> non realtime get (from stored fields)");
getResponse = client.prepareGet("test", "type2", "1").setFields("str", "int", "date").execute().actionGet();
assertThat(getResponse.exists(), equalTo(true));
assertThat((String) getResponse.field("str").getValue(), equalTo("test"));
assertThat((Integer) getResponse.field("int").getValue(), equalTo(42));
assertThat((String) getResponse.field("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
}
}