use of org.elasticsearch.client.RestHighLevelClient in project elasticsearch by elastic.
the class DeleteDocumentationIT method testDelete.
/**
* This test documents docs/java-rest/high-level/document/delete.asciidoc
*/
public void testDelete() throws IOException {
RestHighLevelClient client = highLevelClient();
// tag::delete-request[]
DeleteRequest request = new DeleteRequest(// <1>
"index", // <2>
"type", // <3>
"id");
// end::delete-request[]
// tag::delete-request-props[]
// <1>
request.timeout(TimeValue.timeValueSeconds(1));
// <2>
request.timeout("1s");
// <3>
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
// <4>
request.setRefreshPolicy("wait_for");
// <5>
request.version(2);
// <6>
request.versionType(VersionType.EXTERNAL);
// end::delete-request-props[]
// tag::delete-execute[]
DeleteResponse response = client.delete(request);
try {
// tag::delete-notfound[]
if (response.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) {
// <1>
throw new Exception("Can't find document to be removed");
}
// end::delete-notfound[]
} catch (Exception ignored) {
}
// tag::delete-execute-async[]
client.deleteAsync(request, new ActionListener<DeleteResponse>() {
@Override
public void onResponse(DeleteResponse deleteResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
});
// tag::delete-conflict[]
try {
client.delete(request);
} catch (ElasticsearchException exception) {
if (exception.status().equals(RestStatus.CONFLICT)) {
// <1>
}
}
// end::delete-conflict[]
}
use of org.elasticsearch.client.RestHighLevelClient in project elastest-torm by elastest.
the class ElasticsearchService method init.
@PostConstruct
private void init() {
URL url;
try {
url = new URL(this.esApiUrl);
this.host = url.getHost();
this.port = url.getPort();
logger.debug("Elasticsearch API host: {} / port: {}", this.host, this.port);
this.esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(this.host, this.port, "http")));
} catch (MalformedURLException e) {
logger.error("Cannot get Elasticsearch url by given: {}", this.esApiUrl);
}
}
use of org.elasticsearch.client.RestHighLevelClient in project hazelcast by hazelcast.
the class ElasticSinkBuilder method build.
/**
* Create a sink that writes data into Elasticsearch based on this builder configuration
*/
@Nonnull
public Sink<T> build() {
requireNonNull(clientFn, "clientFn is not set");
requireNonNull(mapToRequestFn, "mapToRequestFn is not set");
return SinkBuilder.sinkBuilder(DEFAULT_NAME, ctx -> new BulkContext(new RestHighLevelClient(clientFn.get()), bulkRequestFn, optionsFn, retries, ctx.logger())).<T>receiveFn((bulkContext, item) -> bulkContext.add(mapToRequestFn.apply(item))).flushFn(BulkContext::flush).destroyFn(BulkContext::close).preferredLocalParallelism(DEFAULT_LOCAL_PARALLELISM).build();
}
use of org.elasticsearch.client.RestHighLevelClient in project hazelcast by hazelcast.
the class ElasticSourcePTest method runProcessor.
private TestSupport runProcessor(FunctionEx<ActionRequest, RequestOptions> optionsFn, List<Shard> shards, boolean slicing, boolean coLocatedReading) throws Exception {
RestHighLevelClient client = mockClient;
ElasticSourceConfiguration<String> configuration = new ElasticSourceConfiguration<String>(() -> client, () -> new SearchRequest("*"), optionsFn, SearchHit::getSourceAsString, slicing, coLocatedReading, KEEP_ALIVE, 5);
// This constructor calls the client so it has to be called after specific mock setup in each test method
// rather than in setUp()
processor = new ElasticSourceP<>(configuration, shards);
return TestSupport.verifyProcessor(() -> processor).disableSnapshots();
}
use of org.elasticsearch.client.RestHighLevelClient in project nutch by apache.
the class ElasticIndexWriter method makeClient.
/**
* Generates a RestHighLevelClient with the hosts given
* @param parameters implementation specific {@link org.apache.nutch.indexer.IndexWriterParams}
* @return an initialized {@link org.elasticsearch.client.RestHighLevelClient}
* @throws IOException if there is an error reading the
* {@link org.apache.nutch.indexer.IndexWriterParams}
*/
protected RestHighLevelClient makeClient(IndexWriterParams parameters) throws IOException {
hosts = parameters.getStrings(ElasticConstants.HOSTS);
port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);
scheme = parameters.get(ElasticConstants.SCHEME, HttpHost.DEFAULT_SCHEME_NAME);
auth = parameters.getBoolean(ElasticConstants.USE_AUTH, false);
user = parameters.get(ElasticConstants.USER, DEFAULT_USER);
password = parameters.get(ElasticConstants.PASSWORD, "");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
RestHighLevelClient client = null;
if (hosts != null && port > 1) {
HttpHost[] hostsList = new HttpHost[hosts.length];
int i = 0;
for (String host : hosts) {
hostsList[i++] = new HttpHost(host, port, scheme);
}
RestClientBuilder restClientBuilder = RestClient.builder(hostsList);
if (auth) {
restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder arg0) {
return arg0.setDefaultCredentialsProvider(credentialsProvider);
}
});
}
client = new RestHighLevelClient(restClientBuilder);
} else {
throw new IOException("ElasticRestClient initialization Failed!!!\\n\\nPlease Provide the hosts");
}
return client;
}
Aggregations