use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.
the class RestoreSnapshotIT method testChangeSettingsOnRestore.
public void testChangeSettingsOnRestore() throws Exception {
Client client = client();
createRepository("test-repo", "fs");
logger.info("--> create test index with case-preserving search analyzer");
Settings.Builder indexSettings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s").put("index.analysis.analyzer.my_analyzer.type", "custom").put("index.analysis.analyzer.my_analyzer.tokenizer", "standard");
assertAcked(prepareCreate("test-idx", 2, indexSettings));
int numberOfShards = getNumShards("test-idx").numPrimaries;
assertAcked(client().admin().indices().preparePutMapping("test-idx").setSource("field1", "type=text,analyzer=standard,search_analyzer=my_analyzer"));
final int numdocs = randomIntBetween(10, 100);
IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test-idx").setId(Integer.toString(i)).setSource("field1", "Foo bar " + i);
}
indexRandom(true, builders);
flushAndRefresh();
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "foo")).get(), numdocs);
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), 0);
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
logger.info("--> snapshot it");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
logger.info("--> delete the index and recreate it while changing refresh interval and analyzer");
cluster().wipeIndices("test-idx");
Settings newIndexSettings = Settings.builder().put("refresh_interval", "5s").put("index.analysis.analyzer.my_analyzer.type", "standard").build();
Settings newIncorrectIndexSettings = Settings.builder().put(newIndexSettings).put(SETTING_NUMBER_OF_SHARDS, numberOfShards + 100).build();
logger.info("--> try restoring while changing the number of shards - should fail");
assertRequestBuilderThrows(client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings("index.analysis.*").setIndexSettings(newIncorrectIndexSettings).setWaitForCompletion(true), SnapshotRestoreException.class);
logger.info("--> try restoring while changing the number of replicas to a negative number - should fail");
Settings newIncorrectReplicasIndexSettings = Settings.builder().put(newIndexSettings).put(SETTING_NUMBER_OF_REPLICAS.substring(IndexMetadata.INDEX_SETTING_PREFIX.length()), randomIntBetween(-10, -1)).build();
assertRequestBuilderThrows(client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings("index.analysis.*").setIndexSettings(newIncorrectReplicasIndexSettings).setWaitForCompletion(true), IllegalArgumentException.class);
logger.info("--> restore index with correct settings from the snapshot");
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings("index.analysis.*").setIndexSettings(newIndexSettings).setWaitForCompletion(true).execute().actionGet();
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
logger.info("--> assert that correct settings are restored");
GetSettingsResponse getSettingsResponse = client.admin().indices().prepareGetSettings("test-idx").execute().actionGet();
assertThat(getSettingsResponse.getSetting("test-idx", INDEX_REFRESH_INTERVAL_SETTING.getKey()), equalTo("5s"));
// Make sure that number of shards didn't change
assertThat(getSettingsResponse.getSetting("test-idx", SETTING_NUMBER_OF_SHARDS), equalTo("" + numberOfShards));
assertThat(getSettingsResponse.getSetting("test-idx", "index.analysis.analyzer.my_analyzer.type"), equalTo("standard"));
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), numdocs);
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
logger.info("--> delete the index and recreate it while deleting all index settings");
cluster().wipeIndices("test-idx");
logger.info("--> restore index with correct settings from the snapshot");
restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings(// delete everything we can delete
"*").setIndexSettings(newIndexSettings).setWaitForCompletion(true).execute().actionGet();
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
logger.info("--> assert that correct settings are restored and index is still functional");
getSettingsResponse = client.admin().indices().prepareGetSettings("test-idx").execute().actionGet();
assertThat(getSettingsResponse.getSetting("test-idx", INDEX_REFRESH_INTERVAL_SETTING.getKey()), equalTo("5s"));
// Make sure that number of shards didn't change
assertThat(getSettingsResponse.getSetting("test-idx", SETTING_NUMBER_OF_SHARDS), equalTo("" + numberOfShards));
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), numdocs);
assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
}
use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetSettingsWithDefaults.
public void testGetSettingsWithDefaults() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
GetSettingsRequest request = new GetSettingsRequest().indices("index");
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// tag::get-settings-request-include-defaults
// <1>
request.includeDefaults(true);
// end::get-settings-request-include-defaults
GetSettingsResponse getSettingsResponse = client.indices().getSettings(request, RequestOptions.DEFAULT);
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index");
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
// tag::get-settings-defaults-response
// <1>
String refreshInterval = getSettingsResponse.getSetting("index", "index.refresh_interval");
// <2>
Settings indexDefaultSettings = getSettingsResponse.getIndexToDefaultSettings().get("index");
// end::get-settings-defaults-response
assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);
assertNotNull("with defaults enabled we should get a value for refresh_interval!", refreshInterval);
assertEquals(refreshInterval, indexDefaultSettings.get("index.refresh_interval"));
ActionListener<GetSettingsResponse> listener = new ActionListener<GetSettingsResponse>() {
@Override
public void onResponse(GetSettingsResponse GetSettingsResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
client.indices().getSettingsAsync(request, RequestOptions.DEFAULT, listener);
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetSettings.
public void testGetSettings() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
// tag::get-settings-request
// <1>
GetSettingsRequest request = new GetSettingsRequest().indices("index");
// end::get-settings-request
// tag::get-settings-request-names
// <1>
request.names("index.number_of_shards");
// end::get-settings-request-names
// tag::get-settings-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::get-settings-request-indicesOptions
// tag::get-settings-execute
GetSettingsResponse getSettingsResponse = client.indices().getSettings(request, RequestOptions.DEFAULT);
// end::get-settings-execute
// tag::get-settings-response
// <1>
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");
// <2>
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index");
// <3>
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
// end::get-settings-response
assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);
assertNull("refresh_interval returned but was never set!", getSettingsResponse.getSetting("index", "index.refresh_interval"));
// tag::get-settings-execute-listener
ActionListener<GetSettingsResponse> listener = new ActionListener<GetSettingsResponse>() {
@Override
public void onResponse(GetSettingsResponse GetSettingsResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-settings-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::get-settings-execute-async
// <1>
client.indices().getSettingsAsync(request, RequestOptions.DEFAULT, listener);
// end::get-settings-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.
the class ShrinkIndexIT method testCreateShrinkWithIndexSort.
public void testCreateShrinkWithIndexSort() throws Exception {
SortField expectedSortField = new SortedSetSortField("id", true, SortedSetSelector.Type.MAX);
expectedSortField.setMissingValue(SortedSetSortField.STRING_FIRST);
Sort expectedIndexSort = new Sort(expectedSortField);
internalCluster().ensureAtLeastNumDataNodes(2);
prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("sort.field", "id").put("sort.order", "desc").put("number_of_shards", 8).put("number_of_replicas", 0)).addMapping(MapperService.SINGLE_MAPPING_NAME, "id", "type=keyword,doc_values=true").get();
for (int i = 0; i < 20; i++) {
client().prepareIndex("source").setId(Integer.toString(i)).setSource("{\"foo\" : \"bar\", \"id\" : " + i + "}", XContentType.JSON).get();
}
ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes();
assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
String mergeNode = discoveryNodes[0].getName();
// ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
// if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
// to the require._name below.
ensureGreen();
flushAndRefresh();
assertSortedSegments("source", expectedIndexSort);
// relocate all shards to one node such that we can merge it.
client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)).get();
ensureGreen();
// check that index sort cannot be set on the target index
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> client().admin().indices().prepareResizeIndex("source", "target").setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", "2").put("index.sort.field", "foo").build()).get());
assertThat(exc.getMessage(), containsString("can't override index sort when resizing an index"));
// check that the index sort order of `source` is correctly applied to the `target`
assertAcked(client().admin().indices().prepareResizeIndex("source", "target").setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", "2").putNull("index.blocks.write").build()).get());
ensureGreen();
flushAndRefresh();
GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("target").execute().actionGet();
assertEquals(settingsResponse.getSetting("target", "index.sort.field"), "id");
assertEquals(settingsResponse.getSetting("target", "index.sort.order"), "desc");
assertSortedSegments("target", expectedIndexSort);
// ... and that the index sort is also applied to updates
for (int i = 20; i < 40; i++) {
client().prepareIndex("target").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
}
flushAndRefresh();
assertSortedSegments("target", expectedIndexSort);
}
use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.
the class GetSettingsBlocksIT method testGetSettingsWithBlocks.
public void testGetSettingsWithBlocks() throws Exception {
assertAcked(prepareCreate("test").setSettings(Settings.builder().put("index.refresh_interval", -1).put("index.merge.policy.expunge_deletes_allowed", "30").put(FieldMapper.IGNORE_MALFORMED_SETTING.getKey(), false)));
for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY, SETTING_READ_ONLY_ALLOW_DELETE)) {
try {
enableIndexBlock("test", block);
GetSettingsResponse response = client().admin().indices().prepareGetSettings("test").get();
assertThat(response.getIndexToSettings().size(), greaterThanOrEqualTo(1));
assertThat(response.getSetting("test", "index.refresh_interval"), equalTo("-1"));
assertThat(response.getSetting("test", "index.merge.policy.expunge_deletes_allowed"), equalTo("30"));
assertThat(response.getSetting("test", FieldMapper.IGNORE_MALFORMED_SETTING.getKey()), equalTo("false"));
} finally {
disableIndexBlock("test", block);
}
}
try {
enableIndexBlock("test", SETTING_BLOCKS_METADATA);
assertBlocked(client().admin().indices().prepareGetSettings("test"));
} finally {
disableIndexBlock("test", SETTING_BLOCKS_METADATA);
}
}
Aggregations