Search in sources :

Example 26 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 27 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 28 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 29 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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 30 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.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)

Aggregations

RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)56 HttpHost (org.apache.http.HttpHost)22 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)19 IOException (java.io.IOException)12 RestClient (org.elasticsearch.client.RestClient)11 HashMap (java.util.HashMap)9 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 Test (org.junit.jupiter.api.Test)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 RequestOptions (org.elasticsearch.client.RequestOptions)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 Test (org.junit.Test)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)4