Skip to content

Commit

Permalink
Show human readable Elasticsearch version that created index and date…
Browse files Browse the repository at this point in the history
… when index was created

By setting human parameter to true, it's now possible to see human readable versions of Elasticsearch that created and updated the index as well as the date when the index was created.

Closes elastic#11484
  • Loading branch information
imotov committed Jun 5, 2015
1 parent 5f4c6b0 commit 683c775
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 1 deletion.
9 changes: 9 additions & 0 deletions rest-api-spec/api/indices.get.json
Expand Up @@ -34,6 +34,15 @@
"options" : ["open","closed","none","all"],
"default" : "open",
"description":"Whether wildcard expressions should get expanded to open or closed indices (default: open)"
},
"flat_settings": {
"type": "boolean",
"description": "Return settings in flat format (default: false)"
},
"human": {
"type": "boolean",
"description": "Whether to return version and creation date values in human-readable format.",
"default": false
}
}
},
Expand Down
5 changes: 5 additions & 0 deletions rest-api-spec/api/indices.get_settings.json
Expand Up @@ -37,6 +37,11 @@
"local": {
"type": "boolean",
"description": "Return local information, do not retrieve the state from master node (default: false)"
},
"human": {
"type": "boolean",
"description": "Whether to return version and creation date values in human-readable format.",
"default": false
}
}
},
Expand Down
23 changes: 23 additions & 0 deletions rest-api-spec/test/indices.get/10_basic.yaml
Expand Up @@ -115,6 +115,29 @@ setup:
- is_false: test_index.aliases
- is_false: test_index.warmers

---
"Get index infos with human settings should return index creation date and version in readable format":

- do:
indices.get:
index: test_index
feature: _settings
human: true

- is_true: test_index.settings.index.creation_date_string
- is_true: test_index.settings.index.version.created_string

---
"Get index infos by default shouldn't return index creation date and version in readable format":

- do:
indices.get:
index: test_index
feature: _settings

- is_false: test_index.settings.index.creation_date_string
- is_false: test_index.settings.index.version.created_string

---
"Missing index should throw an Error":

Expand Down
Expand Up @@ -100,6 +100,7 @@ public static Feature[] convertToFeatures(String... featureNames) {

private static final Feature[] DEFAULT_FEATURES = new Feature[] { Feature.ALIASES, Feature.MAPPINGS, Feature.SETTINGS, Feature.WARMERS };
private Feature[] features = DEFAULT_FEATURES;
private boolean humanReadable = false;

public GetIndexRequest features(Feature... features) {
if (features == null) {
Expand Down Expand Up @@ -135,6 +136,15 @@ public ActionRequestValidationException validate() {
return null;
}

public GetIndexRequest humanReadable(boolean humanReadable) {
this.humanReadable = humanReadable;
return this;
}

public boolean humanReadable() {
return humanReadable;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
Expand All @@ -143,6 +153,7 @@ public void readFrom(StreamInput in) throws IOException {
for (int i = 0; i < size; i++) {
features[i] = Feature.fromId(in.readByte());
}
humanReadable = in.readBoolean();
}

@Override
Expand All @@ -152,6 +163,7 @@ public void writeTo(StreamOutput out) throws IOException {
for (Feature feature : features) {
out.writeByte(feature.id);
}
out.writeBoolean(humanReadable);
}

}
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
Expand Down Expand Up @@ -102,7 +103,11 @@ protected void doMasterOperation(final GetIndexRequest request, String[] concret
if (!doneSettings) {
ImmutableOpenMap.Builder<String, Settings> settingsMapBuilder = ImmutableOpenMap.builder();
for (String index : concreteIndices) {
settingsMapBuilder.put(index, state.metaData().index(index).getSettings());
Settings indexSettings = state.metaData().index(index).getSettings();
if (request.humanReadable()) {
indexSettings = IndexMetaData.addHumanReadableSettings(indexSettings);
}
settingsMapBuilder.put(index, indexSettings);
}
settings = settingsMapBuilder.build();
doneSettings = true;
Expand Down
Expand Up @@ -37,6 +37,7 @@ public class GetSettingsRequest extends MasterNodeReadRequest<GetSettingsRequest
private String[] indices = Strings.EMPTY_ARRAY;
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, true, true, true);
private String[] names = Strings.EMPTY_ARRAY;
private boolean humanReadable = false;

@Override
public GetSettingsRequest indices(String... indices) {
Expand Down Expand Up @@ -68,6 +69,15 @@ public GetSettingsRequest names(String... names) {
return this;
}

public boolean humanReadable() {
return humanReadable;
}

public GetSettingsRequest humanReadable(boolean humanReadable) {
this.humanReadable = humanReadable;
return this;
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
Expand All @@ -83,6 +93,7 @@ public void readFrom(StreamInput in) throws IOException {
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
names = in.readStringArray();
humanReadable = in.readBoolean();
}

@Override
Expand All @@ -91,5 +102,6 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
out.writeStringArray(names);
out.writeBoolean(humanReadable);
}
}
Expand Up @@ -79,6 +79,9 @@ protected void masterOperation(GetSettingsRequest request, ClusterState state, A
}

Settings settings = SettingsFilter.filterSettings(settingsFilter.getPatterns(), indexMetaData.settings());
if (request.humanReadable()) {
settings = IndexMetaData.addHumanReadableSettings(settings);
}
if (!CollectionUtils.isEmpty(request.names())) {
Settings.Builder settingsBuilder = Settings.builder();
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.warmer.IndexWarmersMetaData;
import org.joda.time.DateTime;

import java.io.IOException;
import java.text.ParseException;
Expand Down Expand Up @@ -160,9 +161,12 @@ public static State fromString(String state) {
public static final String SETTING_BLOCKS_WRITE = "index.blocks.write";
public static final String SETTING_BLOCKS_METADATA = "index.blocks.metadata";
public static final String SETTING_VERSION_CREATED = "index.version.created";
public static final String SETTING_VERSION_CREATED_STRING = "index.version.created_string";
public static final String SETTING_VERSION_UPGRADED = "index.version.upgraded";
public static final String SETTING_VERSION_UPGRADED_STRING = "index.version.upgraded_string";
public static final String SETTING_VERSION_MINIMUM_COMPATIBLE = "index.version.minimum_compatible";
public static final String SETTING_CREATION_DATE = "index.creation_date";
public static final String SETTING_CREATION_DATE_STRING = "index.creation_date_string";
public static final String SETTING_UUID = "index.uuid";
public static final String SETTING_LEGACY_ROUTING_HASH_FUNCTION = "index.legacy.routing.hash.type";
public static final String SETTING_LEGACY_ROUTING_USE_TYPE = "index.legacy.routing.use_type";
Expand Down Expand Up @@ -920,4 +924,26 @@ public static boolean isIndexUsingShadowReplicas(Settings settings) {
return settings.getAsBoolean(SETTING_SHADOW_REPLICAS, false);
}

/**
* Adds human readable version and creation date settings.
* This method is used to display the settings in a human readable format in REST API
*/
public static Settings addHumanReadableSettings(Settings settings) {
Settings.Builder builder = Settings.builder().put(settings);
Version version = settings.getAsVersion(SETTING_VERSION_CREATED, null);
if (version != null) {
builder.put(SETTING_VERSION_CREATED_STRING, version.toString());
}
Version versionUpgraded = settings.getAsVersion(SETTING_VERSION_UPGRADED, null);
if (versionUpgraded != null) {
builder.put(SETTING_VERSION_UPGRADED_STRING, versionUpgraded.toString());
}
Long creationDate = settings.getAsLong(SETTING_CREATION_DATE, null);
if (creationDate != null) {
DateTime creationDateTime = new DateTime(creationDate);
builder.put(SETTING_CREATION_DATE_STRING, creationDateTime.toString());
}
return builder.build();
}

}
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
Expand Down Expand Up @@ -78,6 +79,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
}
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local()));
getIndexRequest.humanReadable(request.paramAsBoolean("human", false));
client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<GetIndexResponse>(channel) {

@Override
Expand Down
Expand Up @@ -51,6 +51,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
GetSettingsRequest getSettingsRequest = new GetSettingsRequest()
.indices(Strings.splitStringByCommaToArray(request.param("index")))
.indicesOptions(IndicesOptions.fromRequest(request, IndicesOptions.strictExpandOpen()))
.humanReadable(request.hasParam("human"))
.names(names);
getSettingsRequest.local(request.paramAsBoolean("local", getSettingsRequest.local()));

Expand Down
@@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.Version;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.joda.time.DateTime;
import org.junit.Test;

import static com.google.common.collect.Sets.newHashSet;
import static org.elasticsearch.test.VersionUtils.randomVersion;

public class HumanReadableIndexSettingsTests extends ElasticsearchTestCase {

@Test
public void testHumanReadableSettings() {
Version versionCreated = randomVersion(random());
Version versionUpgraded = randomVersion(random());
long created = System.currentTimeMillis();
Settings testSettings = Settings.builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, versionCreated)
.put(IndexMetaData.SETTING_VERSION_UPGRADED, versionUpgraded)
.put(IndexMetaData.SETTING_CREATION_DATE, created)
.build();

Settings humanSettings = IndexMetaData.addHumanReadableSettings(testSettings);

assertEquals(versionCreated.toString(), humanSettings.get(IndexMetaData.SETTING_VERSION_CREATED_STRING, null));
assertEquals(versionUpgraded.toString(), humanSettings.get(IndexMetaData.SETTING_VERSION_UPGRADED_STRING, null));
assertEquals(new DateTime(created).toString(), humanSettings.get(IndexMetaData.SETTING_CREATION_DATE_STRING, null));
}
}

0 comments on commit 683c775

Please sign in to comment.