-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Open Index API to the high level REST client #27574
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e25962
Add open index api to the high level rest client
olcbean cbfaf34
Addressing reviewers remarks
olcbean ae7ede9
Setting getAsync to `final`
olcbean c3f85ee
Added tests for strictExpandOpen and lenientExpandOpen
olcbean f63d4a3
Merge branch 'master' into hlrc_open_index
javanna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,14 +25,21 @@ | |
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentHelper; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; | ||
|
@@ -128,16 +135,73 @@ public void testDeleteIndex() throws IOException { | |
} | ||
} | ||
|
||
public void testOpenExistingIndex() throws IOException { | ||
String[] indices = randomIndices(1, 5); | ||
for (String index : indices) { | ||
createIndex(index); | ||
closeIndex(index); | ||
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("GET", index + "/_search")); | ||
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); | ||
assertThat(exception.getMessage().contains(index), equalTo(true)); | ||
} | ||
|
||
OpenIndexRequest openIndexRequest = new OpenIndexRequest(indices); | ||
OpenIndexResponse openIndexResponse = execute(openIndexRequest, highLevelClient().indices()::openIndex, | ||
highLevelClient().indices()::openIndexAsync); | ||
assertTrue(openIndexResponse.isAcknowledged()); | ||
|
||
for (String index : indices) { | ||
Response response = client().performRequest("GET", index + "/_search"); | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
} | ||
} | ||
|
||
public void testOpenNonExistentIndex() throws IOException { | ||
String[] nonExistentIndices = randomIndices(1, 5); | ||
for (String nonExistentIndex : nonExistentIndices) { | ||
assertFalse(indexExists(nonExistentIndex)); | ||
} | ||
|
||
OpenIndexRequest openIndexRequest = new OpenIndexRequest(nonExistentIndices); | ||
ElasticsearchException exception = expectThrows(ElasticsearchException.class, | ||
() -> execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync)); | ||
assertEquals(RestStatus.NOT_FOUND, exception.status()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would you mind checking also that with |
||
|
||
OpenIndexRequest lenientOpenIndexRequest = new OpenIndexRequest(nonExistentIndices); | ||
lenientOpenIndexRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); | ||
OpenIndexResponse lenientOpenIndexResponse = execute(lenientOpenIndexRequest, highLevelClient().indices()::openIndex, | ||
highLevelClient().indices()::openIndexAsync); | ||
assertThat(lenientOpenIndexResponse.isAcknowledged(), equalTo(true)); | ||
|
||
OpenIndexRequest strictOpenIndexRequest = new OpenIndexRequest(nonExistentIndices); | ||
strictOpenIndexRequest.indicesOptions(IndicesOptions.strictExpandOpen()); | ||
ElasticsearchException strictException = expectThrows(ElasticsearchException.class, | ||
() -> execute(openIndexRequest, highLevelClient().indices()::openIndex, highLevelClient().indices()::openIndexAsync)); | ||
assertEquals(RestStatus.NOT_FOUND, strictException.status()); | ||
} | ||
|
||
private static String[] randomIndices(int minIndicesNum, int maxIndicesNum) { | ||
int numIndices = randomIntBetween(minIndicesNum, maxIndicesNum); | ||
String[] indices = new String[numIndices]; | ||
for (int i = 0; i < numIndices; i++) { | ||
indices[i] = "index-" + randomAlphaOfLengthBetween(2, 5).toLowerCase(Locale.ROOT); | ||
} | ||
return indices; | ||
} | ||
|
||
private static void createIndex(String index) throws IOException { | ||
Response response = client().performRequest("PUT", index); | ||
|
||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
} | ||
|
||
private static boolean indexExists(String index) throws IOException { | ||
Response response = client().performRequest("HEAD", index); | ||
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); | ||
} | ||
|
||
return response.getStatusLine().getStatusCode() == 200; | ||
private static void closeIndex(String index) throws IOException { | ||
Response response = client().performRequest("POST", index + "/_close"); | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
import org.elasticsearch.action.bulk.BulkRequest; | ||
import org.elasticsearch.action.bulk.BulkShardRequest; | ||
import org.elasticsearch.action.delete.DeleteRequest; | ||
|
@@ -37,7 +38,6 @@ | |
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchScrollRequest; | ||
import org.elasticsearch.action.search.SearchType; | ||
import org.elasticsearch.action.support.ActiveShardCount; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
|
@@ -83,7 +83,6 @@ | |
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.StringJoiner; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
@@ -93,10 +92,12 @@ | |
import static org.elasticsearch.client.Request.enforceSameContentType; | ||
import static org.elasticsearch.search.RandomSearchRequestGenerator.randomSearchRequest; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class RequestTests extends ESTestCase { | ||
|
||
public void testConstructor() throws Exception { | ||
public void testConstructor() { | ||
final String method = randomFrom("GET", "PUT", "POST", "HEAD", "DELETE"); | ||
final String endpoint = randomAlphaOfLengthBetween(1, 10); | ||
final Map<String, String> parameters = singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)); | ||
|
@@ -122,7 +123,7 @@ public void testConstructor() throws Exception { | |
assertTrue("Request constructor is not public", Modifier.isPublic(constructors[0].getModifiers())); | ||
} | ||
|
||
public void testClassVisibility() throws Exception { | ||
public void testClassVisibility() { | ||
assertTrue("Request class is not public", Modifier.isPublic(Request.class.getModifiers())); | ||
} | ||
|
||
|
@@ -146,7 +147,7 @@ public void testGet() { | |
getAndExistsTest(Request::get, "GET"); | ||
} | ||
|
||
public void testDelete() throws IOException { | ||
public void testDelete() { | ||
String index = randomAlphaOfLengthBetween(3, 10); | ||
String type = randomAlphaOfLengthBetween(3, 10); | ||
String id = randomAlphaOfLengthBetween(3, 10); | ||
|
@@ -283,7 +284,7 @@ public void testCreateIndex() throws IOException { | |
assertToXContentBody(createIndexRequest, request.getEntity()); | ||
} | ||
|
||
public void testDeleteIndex() throws IOException { | ||
public void testDeleteIndex() { | ||
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(); | ||
|
||
int numIndices = randomIntBetween(0, 5); | ||
|
@@ -307,6 +308,29 @@ public void testDeleteIndex() throws IOException { | |
assertNull(request.getEntity()); | ||
} | ||
|
||
public void testOpenIndex() { | ||
OpenIndexRequest openIndexRequest = new OpenIndexRequest(); | ||
int numIndices = randomIntBetween(1, 5); | ||
String[] indices = new String[numIndices]; | ||
for (int i = 0; i < numIndices; i++) { | ||
indices[i] = "index-" + randomAlphaOfLengthBetween(2, 5); | ||
} | ||
openIndexRequest.indices(indices); | ||
|
||
Map<String, String> expectedParams = new HashMap<>(); | ||
setRandomTimeout(openIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
setRandomMasterTimeout(openIndexRequest, expectedParams); | ||
setRandomIndicesOptions(openIndexRequest::indicesOptions, openIndexRequest::indicesOptions, expectedParams); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we also add the set wait for active shards ? |
||
setRandomWaitForActiveShards(openIndexRequest::waitForActiveShards, expectedParams); | ||
|
||
Request request = Request.openIndex(openIndexRequest); | ||
StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_open"); | ||
assertThat(endpoint.toString(), equalTo(request.getEndpoint())); | ||
assertThat(expectedParams, equalTo(request.getParameters())); | ||
assertThat(request.getMethod(), equalTo("POST")); | ||
assertThat(request.getEntity(), nullValue()); | ||
} | ||
|
||
public void testIndex() throws IOException { | ||
String index = randomAlphaOfLengthBetween(3, 10); | ||
String type = randomAlphaOfLengthBetween(3, 10); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, seems like I left this out :)