Skip to content

Commit

Permalink
Merge branch 'master' into ccr
Browse files Browse the repository at this point in the history
* elastic-master:
  Watcher: Mark watcher as started only after loading watches (#30403)
  Pass the task to broadcast actions (#29672)
  Disable REST default settings testing until #29229 is back-ported
  Correct wording in log message (#30336)
  Do not fail snapshot when deleting a missing snapshotted file (#30332)
  AwaitsFix testCreateShrinkIndexToN
  DOCS: Correct mapping tags in put-template api
  DOCS: Fix broken link in the put index template api
  Add put index template api to high level rest client (#30400)
  Relax testAckedIndexing to allow document updating
  [Docs] Add snippets for POS stop tags default value
  Move respect accept header on no handler to 6.3.1
  Respect accept header on no handler (#30383)
  [Test] Add analysis-nori plugin to the vagrant tests
  [Docs] Fix bad link
  [Docs] Fix end of section in the korean plugin docs
  Expose the Lucene Korean analyzer module in a plugin (#30397)
  Docs: remove transport_client from CCS role example (#30263)
  [Rollup] Validate timezone in range queries (#30338)
  Use readFully() to read bytes from CipherInputStream (#28515)
  Fix  docs Recently merged #29229 had a doc bug that broke the doc build. This commit fixes.
  Test: remove cluster permission from CCS user (#30262)
  Add Get Settings API support to java high-level rest client (#29229)
  Watcher: Remove unneeded index deletion in tests
  • Loading branch information
dnhatn committed May 8, 2018
2 parents ad499fc + 71ebed6 commit d9b9d7d
Show file tree
Hide file tree
Showing 71 changed files with 3,855 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -265,6 +269,28 @@ public void flushAsync(FlushRequest flushRequest, ActionListener<FlushResponse>
listener, emptySet(), headers);
}

/**
* Retrieve the settings of one or more indices
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html">
* Indices Get Settings API on elastic.co</a>
*/
public GetSettingsResponse getSettings(GetSettingsRequest getSettingsRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(getSettingsRequest, RequestConverters::getSettings,
GetSettingsResponse::fromXContent, emptySet(), headers);
}

/**
* Asynchronously retrieve the settings of one or more indices
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html">
* Indices Get Settings API on elastic.co</a>
*/
public void getSettingsAsync(GetSettingsRequest getSettingsRequest, ActionListener<GetSettingsResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(getSettingsRequest, RequestConverters::getSettings,
GetSettingsResponse::fromXContent, listener, emptySet(), headers);
}

/**
* Force merge one or more indices using the Force Merge API
* <p>
Expand Down Expand Up @@ -432,4 +458,26 @@ public void putSettingsAsync(UpdateSettingsRequest updateSettingsRequest, Action
UpdateSettingsResponse::fromXContent, listener, emptySet(), headers);
}

/**
* Puts an index template using the Index Templates API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
* on elastic.co</a>
*/
public PutIndexTemplateResponse putTemplate(PutIndexTemplateRequest putIndexTemplateRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(putIndexTemplateRequest, RequestConverters::putTemplate,
PutIndexTemplateResponse::fromXContent, emptySet(), headers);
}

/**
* Asynchronously puts an index template using the Index Templates API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
* on elastic.co</a>
*/
public void putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequest,
ActionListener<PutIndexTemplateResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, RequestConverters::putTemplate,
PutIndexTemplateResponse::fromXContent, listener, emptySet(), headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest;
Expand Down Expand Up @@ -76,7 +78,6 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.rankeval.RankEvalRequest;
import org.elasticsearch.rest.action.RestFieldCapabilitiesAction;
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

Expand All @@ -85,10 +86,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.StringJoiner;

final class RequestConverters {
Expand Down Expand Up @@ -600,6 +598,22 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
return request;
}

static Request getSettings(GetSettingsRequest getSettingsRequest) throws IOException {
String[] indices = getSettingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.indices();
String[] names = getSettingsRequest.names() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.names();

String endpoint = endpoint(indices, "_settings", names);
Request request = new Request(HttpGet.METHOD_NAME, endpoint);

Params params = new Params(request);
params.withIndicesOptions(getSettingsRequest.indicesOptions());
params.withLocal(getSettingsRequest.local());
params.withIncludeDefaults(getSettingsRequest.includeDefaults());
params.withMasterTimeout(getSettingsRequest.masterNodeTimeout());

return request;
}

static Request indicesExist(GetIndexRequest getIndexRequest) {
// this can be called with no indices as argument by transport client, not via REST though
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {
Expand Down Expand Up @@ -630,6 +644,21 @@ static Request indexPutSettings(UpdateSettingsRequest updateSettingsRequest) thr
return request;
}

static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
String endpoint = new EndpointBuilder().addPathPartAsIs("_template").addPathPart(putIndexTemplateRequest.name()).build();
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
Params params = new Params(request);
params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
if (putIndexTemplateRequest.create()) {
params.putParam("create", Boolean.TRUE.toString());
}
if (Strings.hasText(putIndexTemplateRequest.cause())) {
params.putParam("cause", putIndexTemplateRequest.cause());
}
request.setEntity(createEntity(putIndexTemplateRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef();
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
Expand All @@ -71,11 +76,19 @@
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractRawValues;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;

Expand Down Expand Up @@ -189,6 +202,108 @@ public void testCreateIndex() throws IOException {
}
}

public void testGetSettings() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName);
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));

updateIndexSettings(indexName, Settings.builder().put("refresh_interval", "30s"));

GetSettingsResponse updatedResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);
assertEquals("30s", updatedResponse.getSetting(indexName, "index.refresh_interval"));
}

public void testGetSettingsNonExistentIndex() throws IOException {
String nonExistentIndex = "index_that_doesnt_exist";
assertFalse(indexExists(nonExistentIndex));

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(nonExistentIndex);
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(getSettingsRequest, highLevelClient().indices()::getSettings, highLevelClient().indices()::getSettingsAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}

public void testGetSettingsFromMultipleIndices() throws IOException {
String indexName1 = "get_multiple_settings_one";
createIndex(indexName1, Settings.builder().put("number_of_shards", 2).build());

String indexName2 = "get_multiple_settings_two";
createIndex(indexName2, Settings.builder().put("number_of_shards", 3).build());

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("get_multiple_settings*");
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertEquals("2", getSettingsResponse.getSetting(indexName1, "index.number_of_shards"));
assertEquals("3", getSettingsResponse.getSetting(indexName2, "index.number_of_shards"));
}

public void testGetSettingsFiltered() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).names("index.number_of_shards");
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas"));
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
assertEquals(1, getSettingsResponse.getIndexToSettings().get("get_settings_index").size());
}

public void testGetSettingsWithDefaults() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).includeDefaults(true);
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNotNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL,
getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").getAsTime("index.refresh_interval", null));
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
}

public void testGetSettingsWithDefaultsFiltered() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest()
.indices(indexName)
.names("index.refresh_interval")
.includeDefaults(true);
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas"));
assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
assertEquals(0, getSettingsResponse.getIndexToSettings().get("get_settings_index").size());
assertEquals(1, getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").size());
}
public void testPutMapping() throws IOException {
{
// Add mappings to index
Expand Down Expand Up @@ -708,4 +823,59 @@ public void testIndexPutSettingNonExistent() throws IOException {
+ "or check the breaking changes documentation for removed settings]"));
}

@SuppressWarnings("unchecked")
public void testPutTemplate() throws Exception {
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest()
.name("my-template")
.patterns(Arrays.asList("pattern-1", "name-*"))
.order(10)
.create(randomBoolean())
.settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0"))
.mapping("doc", "host_name", "type=keyword", "description", "type=text")
.alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));

PutIndexTemplateResponse putTemplateResponse = execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync);
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));

Map<String, Object> templates = getAsMap("/_template/my-template");
assertThat(templates.keySet(), hasSize(1));
assertThat(extractValue("my-template.order", templates), equalTo(10));
assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*"));
assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3"));
assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0"));
assertThat(extractValue("my-template.mappings.doc.properties.host_name.type", templates), equalTo("keyword"));
assertThat(extractValue("my-template.mappings.doc.properties.description.type", templates), equalTo("text"));
assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc"));
assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz"));
}

public void testPutTemplateBadRequests() throws Exception {
RestHighLevelClient client = highLevelClient();

// Failed to validate because index patterns are missing
PutIndexTemplateRequest withoutPattern = new PutIndexTemplateRequest("t1");
ValidationException withoutPatternError = expectThrows(ValidationException.class,
() -> execute(withoutPattern, client.indices()::putTemplate, client.indices()::putTemplateAsync));
assertThat(withoutPatternError.validationErrors(), contains("index patterns are missing"));

// Create-only specified but an template exists already
PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2").patterns(Arrays.asList("qa-*", "prod-*"));
assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());
goodTemplate.create(true);
ElasticsearchException alreadyExistsError = expectThrows(ElasticsearchException.class,
() -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
assertThat(alreadyExistsError.getDetailedMessage(),
containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]"));
goodTemplate.create(false);
assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());

// Rejected due to unknown settings
PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3")
.patterns(Collections.singletonList("any"))
.settings(Settings.builder().put("this-setting-does-not-exist", 100));
ElasticsearchStatusException unknownSettingError = expectThrows(ElasticsearchStatusException.class,
() -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]"));
}
}
Loading

0 comments on commit d9b9d7d

Please sign in to comment.