Skip to content

Commit

Permalink
Merge branch 'master' into pr/28118
Browse files Browse the repository at this point in the history
* master: (23 commits)
  Update Netty to 4.1.16.Final (elastic#28345)
  Fix peer recovery flushing loop (elastic#28350)
  REST high-level client: add support for exists alias (elastic#28332)
  REST high-level client: move to POST when calling API to retrieve which support request body (elastic#28342)
  Add Indices Aliases API to the high level REST client (elastic#27876)
  Java Api clean up: remove deprecated `isShardsAcked` (elastic#28311)
  [Docs] Fix explanation for `from` and `size` example (elastic#28320)
  Adapt bwc version after backport elastic#28358
  Always return the after_key in composite aggregation response (elastic#28358)
  Adds test name to MockPageCacheRecycler exception (elastic#28359)
  Adds a note in the `terms` aggregation docs regarding pagination (elastic#28360)
  [Test] Fix DiscoveryNodesTests.testDeltas() (elastic#28361)
  Update packaging tests to work with meta plugins (elastic#28336)
  Remove Painless Type from MethodWriter in favor of Java Class. (elastic#28346)
  [Doc] Fixs typo in reverse-nested-aggregation.asciidoc (elastic#28348)
  Reindex: Shore up rethrottle test
  Only assert single commit iff index created on 6.2
  isHeldByCurrentThread should return primitive bool
  [Docs] Clarify `html` encoder in highlighting.asciidoc (elastic#27766)
  Fix GeoDistance query example (elastic#28355)
  ...
  • Loading branch information
jasontedor committed Jan 25, 2018
2 parents 9e912c5 + e59f14d commit 92fa897
Show file tree
Hide file tree
Showing 77 changed files with 1,655 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

import org.apache.http.Header;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
Expand All @@ -33,7 +36,8 @@
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;

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

import static java.util.Collections.emptySet;

/**
* A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Indices API.
Expand All @@ -55,7 +59,7 @@ public final class IndicesClient {
*/
public DeleteIndexResponse delete(DeleteIndexRequest deleteIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}

/**
Expand All @@ -66,7 +70,7 @@ public DeleteIndexResponse delete(DeleteIndexRequest deleteIndexRequest, Header.
*/
public void deleteAsync(DeleteIndexRequest deleteIndexRequest, ActionListener<DeleteIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}

/**
Expand All @@ -77,7 +81,7 @@ public void deleteAsync(DeleteIndexRequest deleteIndexRequest, ActionListener<De
*/
public CreateIndexResponse create(CreateIndexRequest createIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(createIndexRequest, Request::createIndex, CreateIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}

/**
Expand All @@ -88,7 +92,7 @@ public CreateIndexResponse create(CreateIndexRequest createIndexRequest, Header.
*/
public void createAsync(CreateIndexRequest createIndexRequest, ActionListener<CreateIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, Request::createIndex, CreateIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}

/**
Expand All @@ -99,7 +103,7 @@ public void createAsync(CreateIndexRequest createIndexRequest, ActionListener<Cr
*/
public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}

/**
Expand All @@ -111,7 +115,32 @@ public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header
public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<PutMappingResponse> listener,
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}

/**
* Updates aliases using the Index Aliases API
* <p>
* See <a href=
* "https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html">
* Index Aliases API on elastic.co</a>
*/
public IndicesAliasesResponse updateAliases(IndicesAliasesRequest indicesAliasesRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(indicesAliasesRequest, Request::updateAliases,
IndicesAliasesResponse::fromXContent, emptySet(), headers);
}

/**
* Asynchronously updates aliases using the Index Aliases API
* <p>
* See <a href=
* "https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html">
* Index Aliases API on elastic.co</a>
*/
public void updateAliasesAsync(IndicesAliasesRequest indicesAliasesRequestRequest, ActionListener<IndicesAliasesResponse> listener,
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(indicesAliasesRequestRequest, Request::updateAliases,
IndicesAliasesResponse::fromXContent, listener, emptySet(), headers);
}

/**
Expand All @@ -122,7 +151,7 @@ public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<
*/
public OpenIndexResponse open(OpenIndexRequest openIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}

/**
Expand All @@ -133,7 +162,7 @@ public OpenIndexResponse open(OpenIndexRequest openIndexRequest, Header... heade
*/
public void openAsync(OpenIndexRequest openIndexRequest, ActionListener<OpenIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}

/**
Expand All @@ -144,7 +173,7 @@ public void openAsync(OpenIndexRequest openIndexRequest, ActionListener<OpenInde
*/
public CloseIndexResponse close(CloseIndexRequest closeIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(closeIndexRequest, Request::closeIndex, CloseIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}

/**
Expand All @@ -155,6 +184,28 @@ public CloseIndexResponse close(CloseIndexRequest closeIndexRequest, Header... h
*/
public void closeAsync(CloseIndexRequest closeIndexRequest, ActionListener<CloseIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(closeIndexRequest, Request::closeIndex, CloseIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}

/**
* Checks if one or more aliases exist using the Aliases Exist API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html">
* Indices Aliases API on elastic.co</a>
*/
public boolean existsAlias(GetAliasesRequest getAliasesRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequest(getAliasesRequest, Request::existsAlias, RestHighLevelClient::convertExistsResponse,
emptySet(), headers);
}

/**
* Asynchronously checks if one or more aliases exist using the Aliases Exist API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html">
* Indices Aliases API on elastic.co</a>
*/
public void existsAliasAsync(GetAliasesRequest getAliasesRequest, ActionListener<Boolean> listener, Header... headers) {
restHighLevelClient.performRequestAsync(getAliasesRequest, Request::existsAlias, RestHighLevelClient::convertExistsResponse,
listener, emptySet(), headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.apache.http.entity.ContentType;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
Expand Down Expand Up @@ -132,7 +134,7 @@ static Request delete(DeleteRequest deleteRequest) {
}

static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) {
String endpoint = endpoint(deleteIndexRequest.indices(), Strings.EMPTY_ARRAY, "");
String endpoint = endpoint(deleteIndexRequest.indices());

Params parameters = Params.builder();
parameters.withTimeout(deleteIndexRequest.timeout());
Expand All @@ -143,7 +145,7 @@ static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) {
}

static Request openIndex(OpenIndexRequest openIndexRequest) {
String endpoint = endpoint(openIndexRequest.indices(), Strings.EMPTY_ARRAY, "_open");
String endpoint = endpoint(openIndexRequest.indices(), "_open");

Params parameters = Params.builder();

Expand All @@ -156,7 +158,7 @@ static Request openIndex(OpenIndexRequest openIndexRequest) {
}

static Request closeIndex(CloseIndexRequest closeIndexRequest) {
String endpoint = endpoint(closeIndexRequest.indices(), Strings.EMPTY_ARRAY, "_close");
String endpoint = endpoint(closeIndexRequest.indices(), "_close");

Params parameters = Params.builder();

Expand All @@ -168,7 +170,7 @@ static Request closeIndex(CloseIndexRequest closeIndexRequest) {
}

static Request createIndex(CreateIndexRequest createIndexRequest) throws IOException {
String endpoint = endpoint(createIndexRequest.indices(), Strings.EMPTY_ARRAY, "");
String endpoint = endpoint(createIndexRequest.indices());

Params parameters = Params.builder();
parameters.withTimeout(createIndexRequest.timeout());
Expand All @@ -178,6 +180,15 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep
HttpEntity entity = createEntity(createIndexRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
}

static Request updateAliases(IndicesAliasesRequest indicesAliasesRequest) throws IOException {
Params parameters = Params.builder();
parameters.withTimeout(indicesAliasesRequest.timeout());
parameters.withMasterTimeout(indicesAliasesRequest.masterNodeTimeout());

HttpEntity entity = createEntity(indicesAliasesRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request(HttpPost.METHOD_NAME, "/_aliases", parameters.getParams(), entity);
}

static Request putMapping(PutMappingRequest putMappingRequest) throws IOException {
// The concreteIndex is an internal concept, not applicable to requests made over the REST API.
Expand Down Expand Up @@ -348,7 +359,7 @@ static Request multiGet(MultiGetRequest multiGetRequest) throws IOException {
parameters.withRealtime(multiGetRequest.realtime());
parameters.withRefresh(multiGetRequest.refresh());
HttpEntity entity = createEntity(multiGetRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request(HttpGet.METHOD_NAME, "/_mget", parameters.getParams(), entity);
return new Request(HttpPost.METHOD_NAME, "/_mget", parameters.getParams(), entity);
}

static Request index(IndexRequest indexRequest) {
Expand Down Expand Up @@ -437,17 +448,17 @@ static Request search(SearchRequest searchRequest) throws IOException {
if (searchRequest.source() != null) {
entity = createEntity(searchRequest.source(), REQUEST_BODY_CONTENT_TYPE);
}
return new Request(HttpGet.METHOD_NAME, endpoint, params.getParams(), entity);
return new Request(HttpPost.METHOD_NAME, endpoint, params.getParams(), entity);
}

static Request searchScroll(SearchScrollRequest searchScrollRequest) throws IOException {
HttpEntity entity = createEntity(searchScrollRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request("GET", "/_search/scroll", Collections.emptyMap(), entity);
return new Request(HttpPost.METHOD_NAME, "/_search/scroll", Collections.emptyMap(), entity);
}

static Request clearScroll(ClearScrollRequest clearScrollRequest) throws IOException {
HttpEntity entity = createEntity(clearScrollRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request("DELETE", "/_search/scroll", Collections.emptyMap(), entity);
return new Request(HttpDelete.METHOD_NAME, "/_search/scroll", Collections.emptyMap(), entity);
}

static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOException {
Expand All @@ -459,26 +470,57 @@ static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOExcep
XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
byte[] source = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, xContent);
HttpEntity entity = new ByteArrayEntity(source, createContentType(xContent.type()));
return new Request("GET", "/_msearch", params.getParams(), entity);
return new Request(HttpPost.METHOD_NAME, "/_msearch", params.getParams(), entity);
}

static Request existsAlias(GetAliasesRequest getAliasesRequest) {
Params params = Params.builder();
params.withIndicesOptions(getAliasesRequest.indicesOptions());
params.withLocal(getAliasesRequest.local());
if (getAliasesRequest.indices().length == 0 && getAliasesRequest.aliases().length == 0) {
throw new IllegalArgumentException("existsAlias requires at least an alias or an index");
}
String endpoint = endpoint(getAliasesRequest.indices(), "_alias", getAliasesRequest.aliases());
return new Request("HEAD", endpoint, params.getParams(), null);
}

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));
}

static String endpoint(String index, String type, String id) {
return buildEndpoint(index, type, id);
}

static String endpoint(String index, String type, String id, String endpoint) {
return buildEndpoint(index, type, id, endpoint);
}

static String endpoint(String[] indices) {
return buildEndpoint(String.join(",", indices));
}

static String endpoint(String[] indices, String endpoint) {
return buildEndpoint(String.join(",", indices), endpoint);
}

static String endpoint(String[] indices, String[] types, String endpoint) {
return endpoint(String.join(",", indices), String.join(",", types), endpoint);
return buildEndpoint(String.join(",", indices), String.join(",", types), endpoint);
}

static String endpoint(String[] indices, String endpoint, String[] suffixes) {
return buildEndpoint(String.join(",", indices), endpoint, String.join(",", suffixes));
}

static String endpoint(String[] indices, String endpoint, String type) {
return endpoint(String.join(",", indices), endpoint, type);
}

/**
* Utility method to build request's endpoint.
* Utility method to build request's endpoint given its parts as strings
*/
static String endpoint(String... parts) {
static String buildEndpoint(String... parts) {
StringJoiner joiner = new StringJoiner("/", "/", "");
for (String part : parts) {
if (Strings.hasLength(part)) {
Expand Down Expand Up @@ -646,6 +688,11 @@ Params withIndicesOptions(IndicesOptions indicesOptions) {
return this;
}

Params withLocal(boolean local) {
putParam("local", Boolean.toString(local));
return this;
}

Map<String, String> getParams() {
return Collections.unmodifiableMap(params);
}
Expand Down
Loading

0 comments on commit 92fa897

Please sign in to comment.