From f47d62cc304b1174dea30ce09b468a97907738ab Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Tue, 13 Nov 2012 14:26:49 -0500 Subject: [PATCH] Date fields shouldn't be returned as longs by Get API --- .../index/get/ShardGetService.java | 5 +- .../test/integration/get/GetActionTests.java | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/src/main/java/org/elasticsearch/index/get/ShardGetService.java index f7c04074844..5e732dc1dc5 100644 --- a/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -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. - *

+ *

* Note: Call must 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); - } } } } diff --git a/src/test/java/org/elasticsearch/test/integration/get/GetActionTests.java b/src/test/java/org/elasticsearch/test/integration/get/GetActionTests.java index 15c3237698b..ef96962f0bc 100644 --- a/src/test/java/org/elasticsearch/test/integration/get/GetActionTests.java +++ b/src/test/java/org/elasticsearch/test/integration/get/GetActionTests.java @@ -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")); + } } \ No newline at end of file