Search in sources :

Example 26 with RestHighLevelClient

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[]
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchException(org.elasticsearch.ElasticsearchException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException)

Example 27 with RestHighLevelClient

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) URL(java.net.URL) PostConstruct(javax.annotation.PostConstruct)

Example 28 with RestHighLevelClient

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();
}
Also used : FunctionEx(com.hazelcast.function.FunctionEx) ActionRequest(org.elasticsearch.action.ActionRequest) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Util.checkNonNullAndSerializable(com.hazelcast.jet.impl.util.Util.checkNonNullAndSerializable) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) SupplierEx(com.hazelcast.function.SupplierEx) Serializable(java.io.Serializable) JetException(com.hazelcast.jet.JetException) ILogger(com.hazelcast.logging.ILogger) Objects.requireNonNull(java.util.Objects.requireNonNull) RequestOptions(org.elasticsearch.client.RequestOptions) SinkBuilder(com.hazelcast.jet.pipeline.SinkBuilder) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) Nonnull(javax.annotation.Nonnull) Sink(com.hazelcast.jet.pipeline.Sink) RetryUtils.withRetry(com.hazelcast.jet.elastic.impl.RetryUtils.withRetry) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Nonnull(javax.annotation.Nonnull)

Example 29 with RestHighLevelClient

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();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 30 with RestHighLevelClient

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;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) HttpHost(org.apache.http.HttpHost) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback)

Aggregations

RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)61 HttpHost (org.apache.http.HttpHost)23 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)21 IOException (java.io.IOException)14 RestClient (org.elasticsearch.client.RestClient)13 HashMap (java.util.HashMap)10 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 RequestOptions (org.elasticsearch.client.RequestOptions)7 Test (org.junit.jupiter.api.Test)7 CredentialsProvider (org.apache.http.client.CredentialsProvider)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)5 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5