diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java index 862684f256500..9096f50d47333 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java @@ -13,14 +13,20 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Booleans; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.support.XContentMapValues; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Map; import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.either; +import static org.hamcrest.Matchers.equalTo; /** * Basic test that indexed documents survive the rolling restart. See @@ -137,18 +143,7 @@ public void testAutoIdWithOpTypeCreate() throws IOException { waitForGreen.addParameter("wait_for_nodes", "3"); client().performRequest(waitForGreen); - Version minNodeVersion = null; - Map response = entityAsMap(client().performRequest(new Request("GET", "_nodes"))); - Map nodes = (Map) response.get("nodes"); - for (Map.Entry node : nodes.entrySet()) { - Map nodeInfo = (Map) node.getValue(); - Version nodeVersion = Version.fromString(nodeInfo.get("version").toString()); - if (minNodeVersion == null) { - minNodeVersion = nodeVersion; - } else if (nodeVersion.before(minNodeVersion)) { - minNodeVersion = nodeVersion; - } - } + Version minNodeVersion = minNodeVersion(); if (minNodeVersion.before(Version.V_7_5_0)) { ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(bulk)); @@ -171,6 +166,62 @@ public void testAutoIdWithOpTypeCreate() throws IOException { } } + public void testDateNanosFormatUpgrade() throws IOException { + final String indexName = "test_date_nanos"; + switch (CLUSTER_TYPE) { + case OLD: + Request createIndex = new Request("PUT", "/" + indexName); + XContentBuilder mappings = XContentBuilder.builder(XContentType.JSON.xContent()) + .startObject() + .startObject("mappings") + .startObject("properties") + .startObject("date") + .field("type", "date") + .endObject() + .startObject("date_nanos") + .field("type", "date_nanos") + .endObject() + .endObject() + .endObject() + .endObject(); + createIndex.setJsonEntity(Strings.toString(mappings)); + client().performRequest(createIndex); + + Request index = new Request("POST", "/" + indexName + "/_doc/"); + XContentBuilder doc = XContentBuilder.builder(XContentType.JSON.xContent()) + .startObject() + .field("date", "2015-01-01T12:10:30.123456789Z") + .field("date_nanos", "2015-01-01T12:10:30.123456789Z") + .endObject(); + index.addParameter("refresh", "true"); + index.setJsonEntity(Strings.toString(doc)); + client().performRequest(index); + break; + + case UPGRADED: + Request search = new Request("POST", "/" + indexName + "/_search"); + XContentBuilder query = XContentBuilder.builder(XContentType.JSON.xContent()) + .startObject() + .array("fields", new String[] { "date", "date_nanos" }) + .endObject(); + search.setJsonEntity(Strings.toString(query)); + Map response = entityAsMap(client().performRequest(search)); + + Map bestHit = (Map) ((List) (XContentMapValues.extractValue("hits.hits", response))).get(0); + List date = (List) XContentMapValues.extractValue("fields.date", bestHit); + assertThat(date.size(), equalTo(1)); + assertThat(date.get(0), equalTo("2015-01-01T12:10:30.123Z")); + + List dateNanos = (List) XContentMapValues.extractValue("fields.date_nanos", bestHit); + assertThat(dateNanos.size(), equalTo(1)); + assertThat(dateNanos.get(0), equalTo("2015-01-01T12:10:30.123456789Z")); + break; + + default: + break; + } + } + private void bulk(String index, String valueSuffix, int count) throws IOException { StringBuilder b = new StringBuilder(); for (int i = 0; i < count; i++) { @@ -191,4 +242,20 @@ private void assertCount(String index, int count) throws IOException { assertEquals("{\"hits\":{\"total\":" + count + "}}", EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8)); } + + private Version minNodeVersion() throws IOException { + Map response = entityAsMap(client().performRequest(new Request("GET", "_nodes"))); + Map nodes = (Map) response.get("nodes"); + Version minNodeVersion = null; + for (Map.Entry node : nodes.entrySet()) { + Map nodeInfo = (Map) node.getValue(); + Version nodeVersion = Version.fromString(nodeInfo.get("version").toString()); + if (minNodeVersion == null) { + minNodeVersion = nodeVersion; + } else if (nodeVersion.before(minNodeVersion)) { + minNodeVersion = nodeVersion; + } + } + return minNodeVersion; + } } diff --git a/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/old_cluster/10_basic.yml b/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/old_cluster/10_basic.yml index d114fa8d30f11..9d7e3e77a7739 100644 --- a/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/old_cluster/10_basic.yml +++ b/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/old_cluster/10_basic.yml @@ -256,24 +256,3 @@ - do: indices.get_index_template: name: my-it - ---- -"Verify date_nanos default format": - - do: - indices.create: - index: index_nanos - body: - mappings: - properties: - date: - type: date - date_nanos: - type: date_nanos - - - do: - index: - index: index_nanos - id: 0 - body: - date: "2015-01-01T12:10:30.123456789Z" - date_nanos: "2015-01-01T12:10:30.123456789Z" diff --git a/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml b/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml index c5ed28394a95b..33e3e2ede5157 100644 --- a/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml +++ b/qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml @@ -186,16 +186,3 @@ - match: {component_templates.0.component_template.template.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}} - is_true: component_templates.0.component_template.template.mappings - match: {component_templates.0.component_template.template.aliases: {aliasname: {}}} - ---- -"Verify date_nanos default format": - - do: - search: - index: index_nanos - body: - fields: ["date_nanos", "date"] - - - match: { hits.total.value: 1 } - - match: { hits.hits.0._id: "0" } - - match: { hits.hits.0.fields.date: [ "2015-01-01T12:10:30.123Z" ] } - - match: { hits.hits.0.fields.date_nanos: [ "2015-01-01T12:10:30.123456789Z" ] }