Search in sources :

Example 16 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project warn-report by saaavsaaa.

the class ElasticClient method search.

@Test
public void search() throws IOException {
    String elasticsearchHost = "sl010a-analysisdb1:9200,sl010a-analysisdb2:9200,sl010a-analysisdb3:9200";
    String[] elasticHosts = elasticsearchHost.split(",");
    HttpHost[] httpHosts = new HttpHost[elasticHosts.length];
    for (int i = 0; i < elasticHosts.length; i++) {
        String ip = elasticHosts[i].split(":")[0];
        String port = elasticHosts[i].split(":")[1];
        httpHosts[i] = new HttpHost(ip, Integer.parseInt(port), "https");
    }
    RestClient restClient = RestClient.builder(httpHosts).setDefaultHeaders(new Header[] { new BasicHeader("Authorization", "Basic Q049ZGVtb3VzZXI6Q049ZGVtb3VzZXI=") }).build();
    RestHighLevelClient client = new RestHighLevelClient(restClient);
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest);
    System.out.println(searchResponse.getTotalShards());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BasicHeader(org.apache.http.message.BasicHeader) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 17 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project sonarqube by SonarSource.

the class EsConnectorImpl method buildRestHighLevelClient.

private RestHighLevelClient buildRestHighLevelClient() {
    HttpHost[] httpHosts = hostAndPorts.stream().map(hostAndPort -> new HttpHost(hostAndPort.getHost(), hostAndPort.getPortOrDefault(9001))).toArray(HttpHost[]::new);
    if (LOG.isDebugEnabled()) {
        String addresses = Arrays.stream(httpHosts).map(t -> t.getHostName() + ":" + t.getPort()).collect(Collectors.joining(", "));
        LOG.debug("Connected to Elasticsearch node: [{}]", addresses);
    }
    RestClientBuilder builder = RestClient.builder(httpHosts).setHttpClientConfigCallback(httpClientBuilder -> {
        if (searchPassword != null) {
            BasicCredentialsProvider provider = getBasicCredentialsProvider(searchPassword);
            httpClientBuilder.setDefaultCredentialsProvider(provider);
        }
        return httpClientBuilder;
    });
    return new RestHighLevelClient(builder);
}
Also used : RestClient(org.elasticsearch.client.RestClient) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) TimeValue.timeValueSeconds(org.elasticsearch.core.TimeValue.timeValueSeconds) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) HostAndPort(com.google.common.net.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) AuthScope(org.apache.http.auth.AuthScope) RequestOptions(org.elasticsearch.client.RequestOptions) Optional(java.util.Optional) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) HttpHost(org.apache.http.HttpHost) Nullable(javax.annotation.Nullable) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 18 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project sonarqube by SonarSource.

the class EsConnectorImpl method getRestHighLevelClient.

private RestHighLevelClient getRestHighLevelClient() {
    RestHighLevelClient res = this.restClient.get();
    if (res != null) {
        return res;
    }
    RestHighLevelClient restHighLevelClient = buildRestHighLevelClient();
    this.restClient.set(restHighLevelClient);
    return restHighLevelClient;
}
Also used : RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 19 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project sonarqube by SonarSource.

the class EsClientProviderTest method es_client_provider_must_add_default_port_when_not_specified.

@Test
public void es_client_provider_must_add_default_port_when_not_specified() {
    settings.setProperty(CLUSTER_ENABLED.getKey(), true);
    settings.setProperty(CLUSTER_NODE_TYPE.getKey(), "application");
    settings.setProperty(CLUSTER_SEARCH_HOSTS.getKey(), format("%s,%s:8081", localhostHostname, localhostHostname));
    EsClient client = underTest.provide(settings.asConfig());
    RestHighLevelClient nativeClient = client.nativeClient();
    assertThat(nativeClient.getLowLevelClient().getNodes()).hasSize(2);
    Node node = nativeClient.getLowLevelClient().getNodes().get(0);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(9001);
    node = nativeClient.getLowLevelClient().getNodes().get(1);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(8081);
    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":9001, http://" + localhostHostname + ":8081]"), ""));
}
Also used : SEARCH_PORT(org.sonar.process.ProcessProperties.Property.SEARCH_PORT) ES_PORT(org.sonar.process.ProcessProperties.Property.ES_PORT) CLUSTER_ENABLED(org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Node(org.elasticsearch.client.Node) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) MapSettings(org.sonar.api.config.internal.MapSettings) String.format(java.lang.String.format) InetAddress(java.net.InetAddress) CLUSTER_NODE_TYPE(org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_TYPE) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) CLUSTER_SEARCH_HOSTS(org.sonar.process.ProcessProperties.Property.CLUSTER_SEARCH_HOSTS) Condition(org.assertj.core.api.Condition) CLUSTER_NAME(org.sonar.process.ProcessProperties.Property.CLUSTER_NAME) SEARCH_HOST(org.sonar.process.ProcessProperties.Property.SEARCH_HOST) Before(org.junit.Before) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Node(org.elasticsearch.client.Node) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Test(org.junit.Test)

Example 20 with RestHighLevelClient

use of org.elasticsearch.client.RestHighLevelClient in project metron by apache.

the class ElasticsearchRequestSubmitterTest method setup.

public ElasticsearchRequestSubmitter setup(SearchResponse response) throws IOException {
    // mocks
    RestHighLevelClient highLevelClient = mock(RestHighLevelClient.class);
    ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevelClient);
    // the client should return the given search response
    when(highLevelClient.search(any())).thenReturn(response);
    return new ElasticsearchRequestSubmitter(client);
}
Also used : RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient)

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