Search in sources :

Example 11 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project yyl_example by Relucent.

the class ElasticsearchTest method main.

public static void main(String[] args) throws Exception {
    HttpHost[] hosts = { new HttpHost(HOST, PORT, SCHEME) };
    try (RestClient client = RestClient.builder(hosts).build()) {
        info(client);
        catApi(client);
        if (!existsIndices(client)) {
            createIndex(client);
        }
        createDocument(client);
        pause();
        createDocumentPost(client);
        pause();
        getDocument(client);
        pause();
        queryByField(client);
        pause();
        deleteDocument(client);
        pause();
        queryAll(client);
        pause();
        deleteIndex(client);
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient)

Example 12 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project spring-boot by spring-projects.

the class ElasticsearchRestClientAutoConfigurationTests method configureUriWithUsernameAndEmptyPassword.

@Test
void configureUriWithUsernameAndEmptyPassword() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user:@localhost:9200").run((context) -> {
        RestClient client = context.getBean(org.elasticsearch.client.RestHighLevelClient.class).getLowLevelClient();
        assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)).containsExactly("http://localhost:9200");
        assertThat(client).extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)).satisfies((credentialsProvider) -> {
            Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200));
            assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user");
            assertThat(credentials.getPassword()).isEmpty();
        });
    });
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials) Test(org.junit.jupiter.api.Test)

Example 13 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project spring-boot by spring-projects.

the class ElasticsearchRestClientAutoConfigurationTests method configureUriWithUsernameOnly.

@Test
void configureUriWithUsernameOnly() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200").run((context) -> {
        RestClient client = context.getBean(org.elasticsearch.client.RestHighLevelClient.class).getLowLevelClient();
        assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)).containsExactly("http://localhost:9200");
        assertThat(client).extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)).satisfies((credentialsProvider) -> {
            Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200));
            assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user");
            assertThat(credentials.getPassword()).isNull();
        });
    });
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials) Test(org.junit.jupiter.api.Test)

Example 14 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project spring-boot by spring-projects.

the class ElasticsearchRestClientAutoConfigurationTests method configureWhenBuilderCustomizerShouldApply.

@Test
void configureWhenBuilderCustomizerShouldApply() {
    this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(org.elasticsearch.client.RestHighLevelClient.class);
        org.elasticsearch.client.RestHighLevelClient restClient = context.getBean(org.elasticsearch.client.RestHighLevelClient.class);
        RestClient lowLevelClient = restClient.getLowLevelClient();
        assertThat(lowLevelClient).hasFieldOrPropertyWithValue("pathPrefix", "/test");
        assertThat(lowLevelClient).extracting("client.connmgr.pool.maxTotal").isEqualTo(100);
        assertThat(lowLevelClient).extracting("client.defaultConfig.cookieSpec").isEqualTo("rfc6265-lax");
    });
}
Also used : RestClient(org.elasticsearch.client.RestClient) Test(org.junit.jupiter.api.Test)

Example 15 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project graylog2-server by Graylog2.

the class FilteredElasticsearchNodesSniffer method create.

static FilteredElasticsearchNodesSniffer create(RestClient restClient, long sniffRequestTimeoutMillis, ElasticsearchNodesSniffer.Scheme scheme, String filter) {
    final String attribute;
    final String value;
    if (!Strings.isNullOrEmpty(filter)) {
        final String[] conditions = filter.split(":");
        if (conditions.length < 2) {
            throw new IllegalArgumentException("Invalid filter specified for ES node discovery: " + filter);
        }
        attribute = conditions[0].trim();
        value = conditions[1].trim();
    } else {
        attribute = null;
        value = null;
    }
    final NodesSniffer nodesSniffer = new ElasticsearchNodesSniffer(restClient, sniffRequestTimeoutMillis, scheme);
    return new FilteredElasticsearchNodesSniffer(nodesSniffer, attribute, value);
}
Also used : ElasticsearchNodesSniffer(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.sniff.ElasticsearchNodesSniffer) NodesSniffer(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.sniff.NodesSniffer) ElasticsearchNodesSniffer(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.sniff.ElasticsearchNodesSniffer)

Aggregations

RestClient (org.elasticsearch.client.RestClient)41 HttpHost (org.apache.http.HttpHost)21 IOException (java.io.IOException)15 Response (org.elasticsearch.client.Response)9 HttpEntity (org.apache.http.HttpEntity)6 BasicHeader (org.apache.http.message.BasicHeader)6 ResponseException (org.elasticsearch.client.ResponseException)6 Test (org.junit.jupiter.api.Test)6 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)5 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 AuthScope (org.apache.http.auth.AuthScope)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 List (java.util.List)3 StringEntity (org.apache.http.entity.StringEntity)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)2