org.elasticsearch.client.RestHighLevelClient Java Examples

The following examples show how to use org.elasticsearch.client.RestHighLevelClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 8 votes vote down vote up
@BeforeClass
public static void startElasticsearchContainerAndClient() throws IOException {
    container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION);
    container.start();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD));

    RestClientBuilder builder = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

    client = new RestHighLevelClient(builder);

    client.indices().create(new CreateIndexRequest(INDEX), RequestOptions.DEFAULT);
    reporter.reset();
}
 
Example #2
Source File: ExistApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        ExistsQueryBuilder matchQueryBuilder = QueryBuilders.existsQuery("retcode");//查询某个字段存在的记录

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(1);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example #3
Source File: PolicyElasticRepositoryTest.java    From micronaut-microservices-poc with Apache License 2.0 6 votes vote down vote up
@Test
public void canIndexPolicy() {
    PolicyDocument policyDocument = new PolicyDocument(
            "111-111",
            LocalDate.of(2019, 1, 1),
            LocalDate.of(2019, 12, 31),
            "John Smith",
            "SAFE_HOUSE",
            BigDecimal.valueOf(1000),
            "m.smith"
    );

    PolicyElasticRepository repository = new PolicyElasticRepository(
            new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", el.getHttpPort(), "http"))),
            new JsonConverter(objectMapper())
    );

    repository.save(policyDocument);

    PolicyDocument saved = repository.findByNumber("111-111");

    assertNotNull(saved);
}
 
Example #4
Source File: ClientFactory.java    From act-platform with ISC License 6 votes vote down vote up
private boolean waitForConnection(RestHighLevelClient client) {
  long timeout = System.currentTimeMillis() + INITIALIZATION_TIMEOUT;
  while (System.currentTimeMillis() < timeout) {
    try {
      ClusterHealthResponse response = client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
      LOGGER.debug("ElasticSearch cluster (%s) status is %s.", response.getClusterName(), response.getStatus());
      // If ElasticSearch is reachable and its status is at least 'yellow' return immediately.
      if (response.status() == RestStatus.OK && response.getStatus() != ClusterHealthStatus.RED) return true;
    } catch (ElasticsearchException | IOException ex) {
      LOGGER.debug(ex, "Could not fetch ElasticSearch cluster health information.");
    }

    try {
      Thread.sleep(INITIALIZATION_RETRY_WAIT);
    } catch (InterruptedException ignored) {
      // Re-interrupt thread and return immediately in order to trigger a component shutdown.
      Thread.currentThread().interrupt();
      return false;
    }

    LOGGER.warning("ElasticSearch cluster is not available. Trying again.");
  }

  return false;
}
 
Example #5
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startElasticsearchContainerAndClient() throws IOException {
    // Start the container
    container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION);
    container.start();

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD));

    RestClientBuilder builder =  RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    lowLevelClient = builder.build();
    client = new RestHighLevelClient(lowLevelClient);

    lowLevelClient.performRequest("PUT", "/" + INDEX);
    reporter.reset();
}
 
Example #6
Source File: ElasticsearchQueryRunner.java    From presto with Apache License 2.0 6 votes vote down vote up
private static void loadTpchTopic(RestHighLevelClient client, TestingPrestoClient prestoClient, TpchTable<?> table)
{
    long start = System.nanoTime();
    LOG.info("Running import for %s", table.getTableName());
    ElasticsearchLoader loader = new ElasticsearchLoader(client, table.getTableName().toLowerCase(ENGLISH), prestoClient.getServer(), prestoClient.getDefaultSession());
    loader.execute(format("SELECT * from %s", new QualifiedObjectName(TPCH_SCHEMA, TINY_SCHEMA_NAME, table.getTableName().toLowerCase(ENGLISH))));
    LOG.info("Imported %s in %s", table.getTableName(), nanosSince(start).convertToMostSuccinctTimeUnit());
}
 
Example #7
Source File: QueryAllMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.matchAllQuery();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(5);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example #8
Source File: ElasticSearchUtils.java    From ElasticUtils with MIT License 6 votes vote down vote up
private static AcknowledgedResponse internalUpdateMapping(RestHighLevelClient client, String indexName, IElasticSearchMapping mapping) throws IOException {

        // An update apparently only takes the "properties" field in Elasticsearch 7.0.
        //
        // So... First let's get a Map:
        Map<String, Object> objectMap = convertToMap(mapping);

        // Supress Warnings, because the cast is safe to do at this point (at least I hope so!):
        @SuppressWarnings("unchecked")
        Map<String, Object> innerMap = (Map<String, Object>) objectMap.get(mapping.getIndexType());

        final PutMappingRequest putMappingRequest = new PutMappingRequest(indexName)
                .source(innerMap);

        final AcknowledgedResponse putMappingResponse = client.indices().putMapping(putMappingRequest, RequestOptions.DEFAULT);

        if(log.isDebugEnabled()) {
            log.debug("PutMappingResponse: isAcknowledged {}", putMappingResponse.isAcknowledged());
        }

        return putMappingResponse;
    }
 
Example #9
Source File: EsHelper.java    From occurrence with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes all the documents of a given dataset in a given index.
 *
 * @param esClient client to connect to ES
 * @param datasetKey key of the dataset whose documents will be deleted
 * @param index index where the the documents will be deleted from
 */
public static void deleteByDatasetKey(
    final RestHighLevelClient esClient, String datasetKey, String index) {
  LOG.info("Deleting all documents of dataset {} from ES index {}", datasetKey, index);
  Objects.requireNonNull(esClient);
  Preconditions.checkArgument(!Strings.isNullOrEmpty(datasetKey), "datasetKey is required");
  Preconditions.checkArgument(!Strings.isNullOrEmpty(index), "index is required");

  DeleteByQueryRequest request =
      new DeleteByQueryRequest(index)
          .setBatchSize(5000)
          .setQuery(QueryBuilders.matchQuery(DATASET_KEY_FIELD, datasetKey));

  try {
    esClient.deleteByQuery(request, HEADERS.get());
  } catch (IOException e) {
    LOG.error("Could not delete records of dataset {} from index {}", datasetKey, index);
  }
}
 
Example #10
Source File: ElasticSearchConfiguration.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
@Bean
@SneakyThrows
public ElasticRestClient elasticRestClient() {
    if (embeddedProperties.isEnabled()) {
        log.debug("starting embedded elasticsearch on {}:{}",
            embeddedProperties.getHost(),
            embeddedProperties.getPort());

        new EmbeddedElasticSearch(embeddedProperties)
            .start();
    }
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(properties.createHosts())
        .setRequestConfigCallback(properties::applyRequestConfigBuilder)
        .setHttpClientConfigCallback(properties::applyHttpAsyncClientBuilder));
    return new ElasticRestClient(client, client);
}
 
Example #11
Source File: ElasticSearchUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public SearchResponse searchResources(SearchCriteria searchCriteria, List<SearchField> searchFields, List<SortField> sortFields, RestHighLevelClient client, String index) throws IOException {
    // See Also: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html
    QueryAccumulator queryAccumulator = new QueryAccumulator(searchCriteria);
    if (searchCriteria.getParamList() != null) {
        searchFields.stream().forEach(queryAccumulator::addQuery);
        // For now assuming there is only date field where range query will
        // be done. If we there are more than one, then we should create a
        // hashmap for each field name
        if (queryAccumulator.fromDate != null || queryAccumulator.toDate != null) {
            queryAccumulator.queries.add(setDateRange(queryAccumulator.dateFieldName, queryAccumulator.fromDate, queryAccumulator.toDate));
        }
    }
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    queryAccumulator.queries.stream().filter(x -> x != null).forEach(boolQueryBuilder::must);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    setSortClause(searchCriteria, sortFields, searchSourceBuilder);
    searchSourceBuilder.from(searchCriteria.getStartIndex());
    searchSourceBuilder.size(searchCriteria.getMaxRows());
    searchSourceBuilder.fetchSource(true);
    SearchRequest query = new SearchRequest();
    query.indices(index);
    query.source(searchSourceBuilder.query(boolQueryBuilder));
    return client.search(query, RequestOptions.DEFAULT);
}
 
Example #12
Source File: TestElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private void startElasticSearchWithBatchSize(int i) throws Exception {
    System.setProperty(ElasticSearchConfiguration.ELASTIC_SEARCH_INDEX_BATCH_SIZE_PROPERTY_NAME, String.valueOf(i));

    configuration = new SystemPropertiesElasticSearchConfiguration();

    String host = configuration.getEmbeddedHost();
    int port = configuration.getEmbeddedPort();
    String clusterName = configuration.getEmbeddedClusterName();

    embeddedElasticSearch = new EmbeddedElasticSearchV5(clusterName, host, port);
    embeddedElasticSearch.start();

    ElasticSearchRestClientProvider restClientProvider =
            new ElasticSearchRestClientProvider(configuration);
    restClient = restClientProvider.get();
    elasticSearchClient = new RestHighLevelClient(restClient);

    Map<String, String> params = new HashMap<>();
    params.put("wait_for_status", "yellow");
    params.put("timeout", "30s");

    restClient.performRequest("GET", "/_cluster/health", params);

    objectMapper = new JsonMapperProvider().get();
    indexDAO = new ElasticSearchRestDAOV5(restClient, configuration, objectMapper);
}
 
Example #13
Source File: MultiGetApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    try{
        RestHighLevelClient client = HighLevelClient.getInstance();
        MultiGetRequest multiGetRequest = new MultiGetRequest();

        multiGetRequest.add(new MultiGetRequest.Item("jingma2_20180716","testlog","1"));
        multiGetRequest.add(new MultiGetRequest.Item("jingma2_20180716","testlog","2"));
        multiGetRequest.add(new MultiGetRequest.Item("jingma2_20180716","testlog","3"));

        MultiGetResponse multiGetResponse = client.multiGet(multiGetRequest);
        MultiGetItemResponse[] itemResponses = multiGetResponse.getResponses();
        for(int i=0;i<itemResponses.length;i++){
            System.out.println(itemResponses[i].getResponse());
        }
    }finally{
        HighLevelClient.close();
    }
}
 
Example #14
Source File: ElasticsearchClientFactory.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<RestHighLevelClient> p) throws Exception {
	if (p.getObject() != null) {
		try {
			if (p.getObject().ping()) {
				p.getObject().close();
			}
		} catch (IOException e) {
			log.debug("es http client close exception:{}", e.getMessage());
		}
	}

}
 
Example #15
Source File: EsHelper.java    From occurrence with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes an ES index.
 *
 * @param esClient client to connect to ES
 * @param index index to delete
 */
public static void deleteIndex(final RestHighLevelClient esClient, String index) {
  LOG.info("Deleting ES index {}", index);
  Objects.requireNonNull(esClient);
  Preconditions.checkArgument(!Strings.isNullOrEmpty(index), "index is required");

  DeleteIndexRequest request = new DeleteIndexRequest(index);
  try {
    esClient.indices().delete(request, DEFAULT);
  } catch (IOException e) {
    LOG.error("Could not delete index {}", index);
  }
}
 
Example #16
Source File: ElasticSearchClient.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public boolean exists(GetRequest getRequest, Header... headers){
    RestHighLevelClient client = null;
    try{
        client = getResource();
        boolean result = client.exists(getRequest,headers);
        returnResource(client);
        return result;
    }catch(Exception e){
        returnBrokenResource(client);
        throw new ElasticSearchException(e);
    }
}
 
Example #17
Source File: DeleteApiMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
        try{
            RestHighLevelClient client = HighLevelClient.getInstance();
            DeleteRequest deleteRequest = new DeleteRequest("jingma2_20180716", "testlog", "2");
//            deleteRequest.version(2);//指定源文档版本,防止误删除
//            deleteRequest.timeout(TimeValue.timeValueSeconds(5));//设置请求超时时间

            DeleteResponse deleteResponse = client.delete(deleteRequest);
            System.out.println(deleteResponse);
        }finally{
            HighLevelClient.close();
        }
    }
 
Example #18
Source File: AvgAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        AvgAggregationBuilder aggregationBuilder = AggregationBuilders.avg("utm").field("utm").missing(0);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

        //统计结果
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
            System.out.println(((ParsedAvg)(each.getValue())).getValue());
        }

    }finally{
        HighLevelClient.close();
    }
}
 
Example #19
Source File: ElasticSearchRestHighImplTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
private void mockBaseRules() {
  client = mock(RestHighLevelClient.class);
  PowerMockito.mockStatic(ConnectionManager.class);
  try {
    doNothing().when(ConnectionManager.class, "registerShutDownHook");
  } catch (Exception e) {
    Assert.fail("Initialization of test case failed due to " + e.getLocalizedMessage());
  }
  when(ConnectionManager.getRestClient()).thenReturn(client);
}
 
Example #20
Source File: ElasticsearchSinkITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected ElasticsearchSinkBase<Tuple2<Integer, String>, RestHighLevelClient> createElasticsearchSink(
		int bulkFlushMaxActions,
		String clusterName,
		List<HttpHost> httpHosts,
		ElasticsearchSinkFunction<Tuple2<Integer, String>> elasticsearchSinkFunction) {

	ElasticsearchSink.Builder<Tuple2<Integer, String>> builder = new ElasticsearchSink.Builder<>(httpHosts, elasticsearchSinkFunction);
	builder.setBulkFlushMaxActions(bulkFlushMaxActions);

	return builder.build();
}
 
Example #21
Source File: EnhancedRestHighLevelClient.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
private RestHighLevelClient getClient() {
	if (threadLocal.get() != null) {
		releaseClient();
	}
	try {
		RestHighLevelClient restHighLevelClient = esClientPool.borrowObject();
		threadLocal.set(restHighLevelClient);
		return restHighLevelClient;
	} catch (Exception e) {
		throw new GetActiveClientException(e);
	}

}
 
Example #22
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void delete(RestHighLevelClient client, String indexName) throws IOException {
    DeleteIndexRequest request = new DeleteIndexRequest(indexName);
    AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
    if (!deleteIndexResponse.isAcknowledged()) {
        String message = "elasticsearch delete index fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #23
Source File: LogstashIT.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static RestHighLevelClient createClient() throws IOException {

        // Instantiate the client.
        LOGGER.info("instantiating the ES client");
        final HttpHost httpHost = new HttpHost(HOST_NAME, MavenHardcodedConstants.ES_PORT);
        final RestClientBuilder clientBuilder =
                RestClient.builder(httpHost);
        final RestHighLevelClient client = new RestHighLevelClient(clientBuilder);

        // Verify the connection.
        LOGGER.info("verifying the ES connection");
        final ClusterHealthResponse healthResponse = client
                .cluster()
                .health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
        Assertions
                .assertThat(healthResponse.getStatus())
                .isNotEqualTo(ClusterHealthStatus.RED);

        // Delete the index.
        LOGGER.info("deleting the ES index");
        final DeleteIndexRequest deleteRequest =
                new DeleteIndexRequest(MavenHardcodedConstants.ES_INDEX_NAME);
        try {
            final AcknowledgedResponse deleteResponse = client
                    .indices()
                    .delete(deleteRequest, RequestOptions.DEFAULT);
            Assertions
                    .assertThat(deleteResponse.isAcknowledged())
                    .isTrue();
        } catch (ElasticsearchStatusException error) {
            Assertions.assertThat(error)
                    .satisfies(ignored -> Assertions
                            .assertThat(error.status())
                            .isEqualTo(RestStatus.NOT_FOUND));
        }

        return client;

    }
 
Example #24
Source File: Elasticsearch7ApiCallBridge.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void verifyClientConnection(RestHighLevelClient client) throws IOException {
	if (LOG.isInfoEnabled()) {
		LOG.info("Pinging Elasticsearch cluster via hosts {} ...", httpHosts);
	}

	if (!client.ping(RequestOptions.DEFAULT)) {
		throw new RuntimeException("There are no reachable Elasticsearch nodes!");
	}

	if (LOG.isInfoEnabled()) {
		LOG.info("Elasticsearch RestHighLevelClient is connected to {}", httpHosts.toString());
	}
}
 
Example #25
Source File: ElasticSearchClient.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public RestHighLevelClient getResource(){
    RestHighLevelClient client = null;
    try{
        client = pool.getResource();
        return client;
    }catch(RuntimeException e){
        if(client!=null) {
            returnBrokenResource(client);
        }
        throw new ElasticSearchException(e);
    }
}
 
Example #26
Source File: ElasticsearchMigration.java    From elasticsearch-migration with Apache License 2.0 5 votes vote down vote up
private RestHighLevelClient createElasticsearchClient(ElasticsearchConfig elasticsearchConfig) {
    final RestClientBuilder builder = RestClient.builder(
            elasticsearchConfig.getUrls().stream().map(e -> new HttpHost(e.getHost(), e.getPort(), e.getProtocol())).collect(Collectors.toSet()).toArray(new HttpHost[0])
    );
    builder.setDefaultHeaders(elasticsearchConfig.getHeaders().entries().stream().map(e -> new BasicHeader(e.getKey(), e.getValue())).collect(Collectors.toList()).toArray(new Header[0]));

    if (elasticsearchConfig.getMaxRetryTimeoutMillis() != null) {
        builder.setMaxRetryTimeoutMillis(elasticsearchConfig.getMaxRetryTimeoutMillis());
    }
    if (elasticsearchConfig.getPathPrefix() != null) {
        builder.setPathPrefix(elasticsearchConfig.getPathPrefix());
    }

    return new RestHighLevelClient(builder);
}
 
Example #27
Source File: EnhancedRestHighLevelClient.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 *
 * 执行方法,执行前从连接池获取一个连接
 * 
 * @param call
 *            {@link Call}
 * @param releaseClient
 *            该方法执行完成后是否释放client到资源池
 * @return {@link Object}
 */

public Object exec(Call call, boolean releaseClient) {
	RestHighLevelClient restHighLevelClient = getClient();
	try {
		return call.hanl(restHighLevelClient);
	} catch (IOException e) {
		return null;
	} finally {
		if (releaseClient) {
			releaseClient();
		}
	}
}
 
Example #28
Source File: ESRichClientImp.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
public ESRichClientImp(Properties properties)
{
    String[] urls = properties.getProperty("db.host.urls").split("\\,");
    String[] ports = properties.getProperty("db.host.ports").split("\\,");

    HttpHost[] httpHosts = new HttpHost[urls.length];

    for (int i = 0; i < urls.length; i++)
    {
        HttpHost httpHost = new HttpHost(urls[i], Integer.parseInt(ports[i]), "http");
        httpHosts[i] = httpHost;
    }

    rhlclient = new RestHighLevelClient(RestClient.builder(httpHosts));
}
 
Example #29
Source File: ElasticVindClient.java    From vind with Apache License 2.0 5 votes vote down vote up
private ElasticVindClient(int port, String scheme, String host, String user, String key) {
    this.port = port;
    this.host = host;
    this.scheme = scheme;
    this.user = user;
    this.key = key;

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, key));
    final RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, scheme))
            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

    this.client = new RestHighLevelClient(restClientBuilder);
}
 
Example #30
Source File: ElasticSearchUtils.java    From ElasticUtils with MIT License 5 votes vote down vote up
public static boolean mappingsExist(RestHighLevelClient client, String indexName) {
    try {
        GetMappingsRequest request = new GetMappingsRequest().indices(indexName);
        GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT);

        return responseContainsMappings(response);
    } catch(Exception e) {
        if(log.isErrorEnabled()) {
            log.error("Error Getting Mappings", e);
        }
        throw new GetMappingsRequestFailedException(indexName, e);
    }
}