Skip to content

Commit

Permalink
Merge remote-tracking branch 'es/6.x' into ccr-6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvg committed Oct 30, 2017
2 parents 7cc7cd5 + 870884f commit 03225b4
Show file tree
Hide file tree
Showing 213 changed files with 5,243 additions and 2,238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,19 @@ class BuildPlugin implements Plugin<Project> {
}
println " Random Testing Seed : ${project.testSeed}"

// enforce gradle version
GradleVersion minGradle = GradleVersion.version('3.3')
if (GradleVersion.current() < minGradle) {
// enforce Gradle version
final GradleVersion currentGradleVersion = GradleVersion.current();

final GradleVersion minGradle = GradleVersion.version('3.3')
if (currentGradleVersion < minGradle) {
throw new GradleException("${minGradle} or above is required to build elasticsearch")
}

final GradleVersion maxGradle = GradleVersion.version('4.2')
if (currentGradleVersion >= maxGradle) {
throw new GradleException("${maxGradle} or above is not compatible with the elasticsearch build")
}

// enforce Java version
if (javaVersionEnum < minimumJava) {
throw new GradleException("Java ${minimumJava} or above is required to build Elasticsearch")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.client;

import org.apache.http.Header;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;

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

/**
* A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Indices API.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html">Indices API on elastic.co</a>
*/
public final class IndicesClient {
private final RestHighLevelClient restHighLevelClient;

public IndicesClient(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}

/**
* Deletes an index using the Delete Index API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html">
* Delete Index API on elastic.co</a>
*/
public DeleteIndexResponse deleteIndex(DeleteIndexRequest deleteIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
Collections.emptySet(), headers);
}

/**
* Asynchronously deletes an index using the Delete Index API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html">
* Delete Index API on elastic.co</a>
*/
public void deleteIndexAsync(DeleteIndexRequest deleteIndexRequest, ActionListener<DeleteIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
}
}
16 changes: 16 additions & 0 deletions client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.http.entity.ContentType;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
Expand Down Expand Up @@ -123,6 +124,17 @@ static Request delete(DeleteRequest deleteRequest) {
return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
}

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

Params parameters = Params.builder();
parameters.withTimeout(deleteIndexRequest.timeout());
parameters.withMasterTimeout(deleteIndexRequest.masterNodeTimeout());
parameters.withIndicesOptions(deleteIndexRequest.indicesOptions());

return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
}

static Request info() {
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
}
Expand Down Expand Up @@ -449,6 +461,10 @@ Params withFetchSourceContext(FetchSourceContext fetchSourceContext) {
return this;
}

Params withMasterTimeout(TimeValue masterTimeout) {
return putParam("master_timeout", masterTimeout);
}

Params withParent(String parent) {
return putParam("parent", parent);
}
Expand Down
27 changes: 19 additions & 8 deletions client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public class RestHighLevelClient implements Closeable {
private final NamedXContentRegistry registry;
private final CheckedConsumer<RestClient, IOException> doClose;

private final IndicesClient indicesClient = new IndicesClient(this);

/**
* Creates a {@link RestHighLevelClient} given the low level {@link RestClientBuilder} that allows to build the
* {@link RestClient} to be used to perform requests.
Expand Down Expand Up @@ -220,6 +222,15 @@ public final void close() throws IOException {
doClose.accept(client);
}

/**
* Provides an {@link IndicesClient} which can be used to access the Indices API.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html">Indices API on elastic.co</a>
*/
public IndicesClient indices() {
return indicesClient;
}

/**
* Executes a bulk request using the Bulk API
*
Expand Down Expand Up @@ -327,7 +338,7 @@ public void updateAsync(UpdateRequest updateRequest, ActionListener<UpdateRespon
}

/**
* Deletes a document by id using the Delete api
* Deletes a document by id using the Delete API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
*/
Expand All @@ -337,7 +348,7 @@ public DeleteResponse delete(DeleteRequest deleteRequest, Header... headers) thr
}

/**
* Asynchronously deletes a document by id using the Delete api
* Asynchronously deletes a document by id using the Delete API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
*/
Expand All @@ -347,7 +358,7 @@ public void deleteAsync(DeleteRequest deleteRequest, ActionListener<DeleteRespon
}

/**
* Executes a search using the Search api
* Executes a search using the Search API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">Search API on elastic.co</a>
*/
Expand All @@ -356,7 +367,7 @@ public SearchResponse search(SearchRequest searchRequest, Header... headers) thr
}

/**
* Asynchronously executes a search using the Search api
* Asynchronously executes a search using the Search API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">Search API on elastic.co</a>
*/
Expand All @@ -365,7 +376,7 @@ public void searchAsync(SearchRequest searchRequest, ActionListener<SearchRespon
}

/**
* Executes a search using the Search Scroll api
* Executes a search using the Search Scroll API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html">Search Scroll
* API on elastic.co</a>
Expand All @@ -375,7 +386,7 @@ public SearchResponse searchScroll(SearchScrollRequest searchScrollRequest, Head
}

/**
* Asynchronously executes a search using the Search Scroll api
* Asynchronously executes a search using the Search Scroll API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html">Search Scroll
* API on elastic.co</a>
Expand All @@ -386,7 +397,7 @@ public void searchScrollAsync(SearchScrollRequest searchScrollRequest, ActionLis
}

/**
* Clears one or more scroll ids using the Clear Scroll api
* Clears one or more scroll ids using the Clear Scroll API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html#_clear_scroll_api">
* Clear Scroll API on elastic.co</a>
Expand All @@ -397,7 +408,7 @@ public ClearScrollResponse clearScroll(ClearScrollRequest clearScrollRequest, He
}

/**
* Asynchronously clears one or more scroll ids using the Clear Scroll api
* Asynchronously clears one or more scroll ids using the Clear Scroll API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html#_clear_scroll_api">
* Clear Scroll API on elastic.co</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -50,7 +49,6 @@
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.threadpool.ThreadPool;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -614,14 +612,14 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure)
}
};

ThreadPool threadPool = new ThreadPool(Settings.builder().put("node.name", getClass().getName()).build());
// Pull the client to a variable to work around https://bugs.eclipse.org/bugs/show_bug.cgi?id=514884
RestHighLevelClient hlClient = highLevelClient();
try(BulkProcessor processor = new BulkProcessor.Builder(hlClient::bulkAsync, listener, threadPool)
.setConcurrentRequests(0)
.setBulkSize(new ByteSizeValue(5, ByteSizeUnit.GB))
.setBulkActions(nbItems + 1)
.build()) {

try (BulkProcessor processor = BulkProcessor.builder(hlClient::bulkAsync, listener)
.setConcurrentRequests(0)
.setBulkSize(new ByteSizeValue(5, ByteSizeUnit.GB))
.setBulkActions(nbItems + 1)
.build()) {
for (int i = 0; i < nbItems; i++) {
String id = String.valueOf(i);
boolean erroneous = randomBoolean();
Expand All @@ -631,7 +629,7 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure)
if (opType == DocWriteRequest.OpType.DELETE) {
if (erroneous == false) {
assertEquals(RestStatus.CREATED,
highLevelClient().index(new IndexRequest("index", "test", id).source("field", -1)).status());
highLevelClient().index(new IndexRequest("index", "test", id).source("field", -1)).status());
}
DeleteRequest deleteRequest = new DeleteRequest("index", "test", id);
processor.add(deleteRequest);
Expand All @@ -653,10 +651,10 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure)

} else if (opType == DocWriteRequest.OpType.UPDATE) {
UpdateRequest updateRequest = new UpdateRequest("index", "test", id)
.doc(new IndexRequest().source(xContentType, "id", i));
.doc(new IndexRequest().source(xContentType, "id", i));
if (erroneous == false) {
assertEquals(RestStatus.CREATED,
highLevelClient().index(new IndexRequest("index", "test", id).source("field", -1)).status());
highLevelClient().index(new IndexRequest("index", "test", id).source("field", -1)).status());
}
processor.add(updateRequest);
}
Expand All @@ -676,8 +674,6 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure)
assertNull(error.get());

validateBulkResponses(nbItems, errors, bulkResponse, bulkRequest);

terminate(threadPool);
}

private void validateBulkResponses(int nbItems, boolean[] errors, BulkResponse bulkResponse, BulkRequest bulkRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.client;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;

public class IndicesClientIT extends ESRestHighLevelClientTestCase {

public void testDeleteIndex() throws IOException {
{
// Delete index if exists
String indexName = "test_index";
createIndex(indexName);

DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
DeleteIndexResponse deleteIndexResponse =
execute(deleteIndexRequest, highLevelClient().indices()::deleteIndex, highLevelClient().indices()::deleteIndexAsync);
assertTrue(deleteIndexResponse.isAcknowledged());

assertFalse(indexExists(indexName));
}
{
// Return 404 if index doesn't exist
String nonExistentIndex = "non_existent_index";
assertFalse(indexExists(nonExistentIndex));

DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(nonExistentIndex);

ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(deleteIndexRequest, highLevelClient().indices()::deleteIndex, highLevelClient().indices()::deleteIndexAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}
}

private static void createIndex(String index) throws IOException {
Response response = client().performRequest("PUT", index);

assertEquals(200, response.getStatusLine().getStatusCode());
}

private static boolean indexExists(String index) throws IOException {
Response response = client().performRequest("HEAD", index);

return response.getStatusLine().getStatusCode() == 200;
}
}
Loading

0 comments on commit 03225b4

Please sign in to comment.