Search in sources :

Example 31 with RestHighLevelClient

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

the class EsClientProviderTest method connection_to_local_es_when_cluster_mode_is_disabled.

@Test
public void connection_to_local_es_when_cluster_mode_is_disabled() {
    settings.setProperty(CLUSTER_ENABLED.getKey(), false);
    settings.setProperty(SEARCH_HOST.getKey(), localhostHostname);
    settings.setProperty(SEARCH_PORT.getKey(), 9000);
    settings.setProperty(ES_PORT.getKey(), 8080);
    EsClient client = underTest.provide(settings.asConfig());
    RestHighLevelClient nativeClient = client.nativeClient();
    assertThat(nativeClient.getLowLevelClient().getNodes()).hasSize(1);
    Node node = nativeClient.getLowLevelClient().getNodes().get(0);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(9000);
    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to local Elasticsearch: [http://" + localhostHostname + ":9000]"), ""));
}
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 32 with RestHighLevelClient

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

the class EsClientProviderTest method connection_to_remote_es_nodes_when_cluster_mode_is_enabled_and_local_es_is_disabled.

@Test
public void connection_to_remote_es_nodes_when_cluster_mode_is_enabled_and_local_es_is_disabled() {
    settings.setProperty(CLUSTER_ENABLED.getKey(), true);
    settings.setProperty(CLUSTER_NODE_TYPE.getKey(), "application");
    settings.setProperty(CLUSTER_SEARCH_HOSTS.getKey(), format("%s:8080,%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(8080);
    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 + ":8080, 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 33 with RestHighLevelClient

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

the class ElasticsearchDaoTest method setup.

private void setup(RestStatus status, int maxSearchResults, Map<String, FieldType> metadata) throws Exception {
    // setup the mock search hits
    SearchHit hit1 = mock(SearchHit.class);
    when(hit1.getId()).thenReturn("id1");
    when(hit1.getSource()).thenReturn(new HashMap<String, Object>() {

        {
            put("field", "value1");
        }
    });
    when(hit1.getScore()).thenReturn(0.1f);
    SearchHit hit2 = mock(SearchHit.class);
    when(hit2.getId()).thenReturn("id2");
    when(hit2.getSource()).thenReturn(new HashMap<String, Object>() {

        {
            put("field", "value2");
        }
    });
    when(hit2.getScore()).thenReturn(0.2f);
    // search hits
    SearchHit[] hits = { hit1, hit2 };
    SearchHits searchHits = mock(SearchHits.class);
    when(searchHits.getHits()).thenReturn(hits);
    when(searchHits.getTotalHits()).thenReturn(Integer.toUnsignedLong(hits.length));
    // search response which returns the search hits
    org.elasticsearch.action.search.SearchResponse response = mock(org.elasticsearch.action.search.SearchResponse.class);
    when(response.status()).thenReturn(status);
    when(response.getHits()).thenReturn(searchHits);
    // provides column metadata
    ElasticsearchColumnMetadataDao columnMetadataDao = mock(ElasticsearchColumnMetadataDao.class);
    when(columnMetadataDao.getColumnMetadata(any())).thenReturn(metadata);
    // returns the search response
    requestSubmitter = mock(ElasticsearchRequestSubmitter.class);
    when(requestSubmitter.submitSearch(any())).thenReturn(response);
    RestHighLevelClient highLevel = mock(RestHighLevelClient.class);
    ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevel);
    // provides configuration
    AccessConfig config = mock(AccessConfig.class);
    when(config.getMaxSearchResults()).thenReturn(maxSearchResults);
    ElasticsearchSearchDao elasticsearchSearchDao = new ElasticsearchSearchDao(client, config, columnMetadataDao, requestSubmitter);
    ElasticsearchRetrieveLatestDao elasticsearchRetrieveLatestDao = new ElasticsearchRetrieveLatestDao(client);
    ElasticsearchUpdateDao elasticsearchUpdateDao = new ElasticsearchUpdateDao(client, config, elasticsearchRetrieveLatestDao);
    dao = new ElasticsearchDao(client, config, elasticsearchSearchDao, elasticsearchUpdateDao, elasticsearchRetrieveLatestDao, columnMetadataDao, requestSubmitter);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) org.apache.metron.indexing.dao.search(org.apache.metron.indexing.dao.search) JSONObject(org.json.simple.JSONObject) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient)

Example 34 with RestHighLevelClient

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

the class ElasticsearchClientFactory method create.

/**
 * Creates an Elasticsearch client from settings provided via the global config.
 *
 * @return new client
 */
public static ElasticsearchClient create(Map<String, Object> globalConfig) {
    ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig(getEsSettings(globalConfig));
    HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme());
    RestClientBuilder builder = RestClient.builder(httpHosts);
    builder.setRequestConfigCallback(reqConfigBuilder -> {
        // Modifies request config builder with connection and socket timeouts.
        // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html
        reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis());
        reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis());
        return reqConfigBuilder;
    });
    builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis());
    builder.setHttpClientConfigCallback(clientBuilder -> {
        clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig));
        clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig));
        clientBuilder.setSSLContext(getSSLContext(esClientConfig));
        return clientBuilder;
    });
    RestClient lowLevelClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(lowLevelClient);
    return new ElasticsearchClient(lowLevelClient, client);
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClientConfig(org.apache.metron.elasticsearch.config.ElasticsearchClientConfig)

Example 35 with RestHighLevelClient

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

the class ElasticClient method get.

@Test
public void get() throws IOException {
    RestClientBuilder builder = RestClient.builder(new HttpHost("sl010a-analysisdb1", 9200, "https"), new HttpHost("sl010a-analysisdb2", 9200, "https"), new HttpHost("sl010a-analysisdb3", 9200, "https"));
    Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic YWRtaW46YWRtaW4=") };
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(restClient);
    GetRequest getRequest = new GetRequest("test-index", "test-all", "26269");
    GetResponse getResponse = client.get(getRequest);
    System.out.println(getResponse.getSourceAsString());
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) GetRequest(org.elasticsearch.action.get.GetRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) GetResponse(org.elasticsearch.action.get.GetResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

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