Search in sources :

Example 26 with RestClient

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

the class ESClientYamlSuiteTestCase method initAndResetContext.

@Before
public void initAndResetContext() throws IOException {
    if (restTestExecutionContext == null) {
        assert adminExecutionContext == null;
        assert blacklistPathMatchers == null;
        String[] specPaths = resolvePathsProperty(REST_TESTS_SPEC, DEFAULT_SPEC_PATH);
        ClientYamlSuiteRestSpec restSpec = null;
        FileSystem fileSystem = getFileSystem();
        // ... and you can't close() the default filesystem
        try {
            restSpec = ClientYamlSuiteRestSpec.parseFrom(fileSystem, DEFAULT_SPEC_PATH, specPaths);
        } finally {
            IOUtils.close(fileSystem);
        }
        validateSpec(restSpec);
        List<HttpHost> hosts = getClusterHosts();
        RestClient restClient = client();
        Version infoVersion = readVersionsFromInfo(restClient, hosts.size());
        Version esVersion;
        try {
            Tuple<Version, Version> versionVersionTuple = readVersionsFromCatNodes(restClient);
            esVersion = versionVersionTuple.v1();
            Version masterVersion = versionVersionTuple.v2();
            logger.info("initializing yaml client, minimum es version: [{}] master version: [{}] hosts: {}", esVersion, masterVersion, hosts);
        } catch (ResponseException ex) {
            if (ex.getResponse().getStatusLine().getStatusCode() == 403) {
                logger.warn("Fallback to simple info '/' request, _cat/nodes is not authorized");
                esVersion = infoVersion;
                logger.info("initializing yaml client, minimum es version: [{}] hosts: {}", esVersion, hosts);
            } else {
                throw ex;
            }
        }
        ClientYamlTestClient clientYamlTestClient = new ClientYamlTestClient(restSpec, restClient, hosts, esVersion);
        restTestExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, randomizeContentType());
        adminExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, false);
        String[] blacklist = resolvePathsProperty(REST_TESTS_BLACKLIST, null);
        blacklistPathMatchers = new ArrayList<>();
        for (String entry : blacklist) {
            blacklistPathMatchers.add(new BlacklistedPathPatternMatcher(entry));
        }
    }
    assert restTestExecutionContext != null;
    assert adminExecutionContext != null;
    assert blacklistPathMatchers != null;
    // admin context must be available for @After always, regardless of whether the test was blacklisted
    adminExecutionContext.clear();
    //skip test if it matches one of the blacklist globs
    for (BlacklistedPathPatternMatcher blacklistedPathMatcher : blacklistPathMatchers) {
        String testPath = testCandidate.getSuitePath() + "/" + testCandidate.getTestSection().getName();
        assumeFalse("[" + testCandidate.getTestPath() + "] skipped, reason: blacklisted", blacklistedPathMatcher.isSuffixMatch(testPath));
    }
    restTestExecutionContext.clear();
    //skip test if the whole suite (yaml file) is disabled
    assumeFalse(testCandidate.getSetupSection().getSkipSection().getSkipMessage(testCandidate.getSuitePath()), testCandidate.getSetupSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
    //skip test if the whole suite (yaml file) is disabled
    assumeFalse(testCandidate.getTeardownSection().getSkipSection().getSkipMessage(testCandidate.getSuitePath()), testCandidate.getTeardownSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
    //skip test if test section is disabled
    assumeFalse(testCandidate.getTestSection().getSkipSection().getSkipMessage(testCandidate.getTestPath()), testCandidate.getTestSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
}
Also used : ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) ClientYamlSuiteRestSpec(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec) Version(org.elasticsearch.Version) HttpHost(org.apache.http.HttpHost) FileSystem(java.nio.file.FileSystem) Before(org.junit.Before)

Example 27 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project components by Talend.

the class ElasticsearchBeamRuntimeTestIT method init.

@Before
public void init() throws IOException, ExecutionException, InterruptedException {
    client = ElasticsearchTestUtils.createClient(ElasticsearchTestConstants.HOSTS.split(":")[0], ElasticsearchTestConstants.TRANSPORT_PORT, ElasticsearchTestConstants.CLUSTER_NAME);
    datastoreProperties = new ElasticsearchDatastoreProperties("datastore");
    datastoreProperties.init();
    datastoreProperties.nodes.setValue(ElasticsearchTestConstants.HOSTS);
    RestClient restClient = ElasticsearchConnection.createClient(datastoreProperties);
    BasicHeader emptyHeader = new BasicHeader("", "");
    Map<String, String> emptyParams = new HashMap<>();
    ElasticsearchTestUtils.deleteIndex(INDEX_NAME, client);
    Response checkExistsResponse = restClient.performRequest("HEAD", "/" + INDEX_NAME, emptyParams);
    ElasticsearchResponse checkExists = new ElasticsearchResponse(checkExistsResponse);
    if (!checkExists.isOk()) {
        // create index for test, name is 'beam'
        restClient.performRequest("PUT", "/" + INDEX_NAME, emptyHeader);
    }
}
Also used : Response(org.elasticsearch.client.Response) HashMap(java.util.HashMap) RestClient(org.elasticsearch.client.RestClient) BasicHeader(org.apache.http.message.BasicHeader) ElasticsearchDatastoreProperties(org.talend.components.elasticsearch.ElasticsearchDatastoreProperties) Before(org.junit.Before)

Example 28 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project kylo by Teradata.

the class ElasticSearchRestService method index.

@Override
public void index(@Nonnull String indexName, @Nonnull String typeName, @Nonnull String id, @Nonnull Map<String, Object> fields) {
    try (RestClient restClient = buildRestClient()) {
        JSONObject jsonContent = new JSONObject(fields);
        HttpEntity httpEntity = new NStringEntity(jsonContent.toString(), ContentType.APPLICATION_JSON);
        restClient.performRequest(PUT_METHOD, getIndexWriteEndPoint(indexName, typeName, id), Collections.emptyMap(), httpEntity);
        log.debug("Wrote to index with name {}", indexName);
    } catch (ResponseException responseException) {
        log.warn("Index write encountered issues in Elasticsearch for index={" + indexName + "}, type={" + typeName + "}, id={" + id + "}", responseException);
    } catch (ClientProtocolException clientProtocolException) {
        log.debug("Http protocol error for write for index {" + indexName + "}", clientProtocolException);
    } catch (IOException ioException) {
        log.error("IO Error in rest client", ioException);
    }
}
Also used : NStringEntity(org.apache.http.nio.entity.NStringEntity) JSONObject(org.codehaus.jettison.json.JSONObject) HttpEntity(org.apache.http.HttpEntity) ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 29 with RestClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClient in project kylo by Teradata.

the class ElasticSearchRestService method deleteAll.

@Override
public int deleteAll(@Nonnull String indexName, @Nonnull String typeName) {
    try (RestClient restClient = buildRestClient()) {
        if ((restClientConfig.getEsversion() != null) && (restClientConfig.getEsversion().equals(VERSION_TWO))) {
            log.info("Elasticsearch v2");
            Response response = restClient.performRequest(DELETE_METHOD, getAllDocumentsDeleteEndPointEsV2(indexName, typeName), new HashMap<>(), getAllDocumentsDeleteRequestBodyDslV2());
            if (response != null) {
                try {
                    String entityString = EntityUtils.toString(response.getEntity());
                    JSONObject entityStringJsonObject = new JSONObject(entityString);
                    JSONObject indicesJsonObject = entityStringJsonObject.getJSONObject("_indices");
                    JSONObject allJsonObject = indicesJsonObject.getJSONObject("_all");
                    return (Integer) allJsonObject.get("deleted");
                } catch (JSONException e) {
                    log.warn("Unable to get number of documents deleted. [{}]", e);
                }
            } else {
                log.warn("Unable to identify number of documents deleted since null response received");
            }
        } else {
            log.info("Elasticsearch v5 or above");
            Response response = restClient.performRequest(POST_METHOD, getAllDocumentsDeleteEndPoint(indexName, typeName), new HashMap<>(), getAllDocumentsDeleteRequestBodyDsl());
            if (response != null) {
                try {
                    String entityString = EntityUtils.toString(response.getEntity());
                    JSONObject entityStringJsonObject = new JSONObject(entityString);
                    return (Integer) entityStringJsonObject.get("deleted");
                } catch (JSONException e) {
                    log.warn("Unable to get number of documents deleted. [{}]", e);
                }
            } else {
                log.warn("Unable to identify number of documents deleted since null response received");
            }
        }
        log.info("Deleted all-documents for index={}, type={}", indexName, typeName);
    } catch (ResponseException responseException) {
        log.error("Index all-documents deletion encountered issues in Elasticsearch for index={}, type={} [{}]", indexName, typeName, responseException);
    } catch (ClientProtocolException clientProtocolException) {
        log.error("Http protocol error for delete all-documents for index={}, type={} [{}]", indexName, typeName, clientProtocolException);
    } catch (IOException ioException) {
        log.error("IO Error in rest client [{}]", ioException);
    }
    return 0;
}
Also used : ElasticSearchRestSearchResponse(com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse) Response(org.elasticsearch.client.Response) JSONObject(org.codehaus.jettison.json.JSONObject) ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 30 with RestClient

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

the class ElasticsearchRestClientAutoConfigurationTests method configureUriWithNoScheme.

@Test
void configureUriWithNoScheme() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=localhost:9876").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:9876");
    });
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) Test(org.junit.jupiter.api.Test)

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