use of org.elasticsearch.client.RestHighLevelClient in project jnosql-diana-driver by eclipse.
the class EntityConverter method queryAsync.
static void queryAsync(DocumentQuery query, RestHighLevelClient client, String index, Consumer<List<DocumentEntity>> callBack) {
FindAsyncListener listener = new FindAsyncListener(callBack, query.getDocumentCollection());
QueryConverterResult select = QueryConverter.select(query);
if (!select.getIds().isEmpty()) {
MultiGetRequest multiGetRequest = new MultiGetRequest();
select.getIds().stream().map(id -> new MultiGetRequest.Item(index, query.getDocumentCollection(), id)).forEach(multiGetRequest::add);
client.multiGetAsync(multiGetRequest, listener.getIds());
}
if (select.hasStatement()) {
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(query.getDocumentCollection());
if (select.hasQuery()) {
setQueryBuilder(query, select, searchRequest);
}
client.searchAsync(searchRequest, listener.getSearch());
}
}
use of org.elasticsearch.client.RestHighLevelClient in project main by JohnPeng739.
the class ElasticAccessorRest method afterPropertiesSet.
/**
* {@inheritDoc}
*
* @see InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
int servers = env.getProperty("elastic.servers", Integer.class, 0);
if (servers <= 0) {
if (logger.isErrorEnabled()) {
logger.error("There are not define the elastic api server.");
}
return;
}
this.index = env.getProperty("elastic.index", "default");
HttpHost[] elasticServers = new HttpHost[servers];
for (int index = 1; index <= servers; index++) {
String protocol = env.getProperty("elastic.api.protocol", "http");
String server = env.getProperty("elastic.api.server", "localhost");
int port = env.getProperty("elastic.api.port", Integer.class, 9200);
elasticServers[index - 1] = new HttpHost(server, port, protocol);
}
client = new RestHighLevelClient(RestClient.builder(elasticServers));
// 初始化Index
try {
OpenIndexRequest request = new OpenIndexRequest(this.index);
client.indices().open(request);
} catch (ElasticsearchStatusException ex) {
if (ex.status() == RestStatus.NOT_FOUND) {
CreateIndexRequest request = new CreateIndexRequest(this.index);
client.indices().create(request);
}
} catch (IOException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Open index fail.", ex);
}
}
if (logger.isInfoEnabled()) {
logger.info("Create the elastic search api connection successfully.");
}
}
use of org.elasticsearch.client.RestHighLevelClient in project drill by apache.
the class ElasticInfoSchemaTest method prepareData.
private static void prepareData() throws IOException {
restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create(HOST)));
String indexName = "t1";
indexNames.add(indexName);
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("string_field", "a");
builder.field("int_field", 123);
builder.endObject();
IndexRequest indexRequest = new IndexRequest(indexName).source(builder);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
restHighLevelClient.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT);
indexName = "t2";
indexNames.add(indexName);
createIndexRequest = new CreateIndexRequest(indexName);
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("another_int_field", 321);
builder.field("another_string_field", "b");
builder.endObject();
indexRequest = new IndexRequest(indexName).source(builder);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
restHighLevelClient.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT);
}
use of org.elasticsearch.client.RestHighLevelClient in project logging-log4j2 by apache.
the class LogstashIT method createClient.
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;
}
use of org.elasticsearch.client.RestHighLevelClient in project logging-log4j2 by apache.
the class LogstashIT method testEvents.
private static void testEvents(final List<LogEvent> logEvents) throws IOException {
try (final RestHighLevelClient client = createClient()) {
final Appender appender = createStartedAppender(JSON_TEMPLATE_GELF_LAYOUT, MavenHardcodedConstants.LS_GELF_INPUT_PORT);
try {
// Append events.
LOGGER.info("appending events");
logEvents.forEach(appender::append);
LOGGER.info("completed appending events");
// Wait all messages to arrive.
Awaitility.await().atMost(Duration.ofSeconds(60)).pollDelay(Duration.ofSeconds(2)).until(() -> queryDocumentCount(client) == LOG_EVENT_COUNT);
// Verify indexed messages.
final Set<String> expectedMessages = logEvents.stream().map(LogstashIT::expectedLogstashMessageField).collect(Collectors.toSet());
final Set<String> actualMessages = queryDocuments(client).stream().map(source -> (String) source.get(ES_INDEX_MESSAGE_FIELD_NAME)).filter(Objects::nonNull).collect(Collectors.toSet());
Assertions.assertThat(actualMessages).isEqualTo(expectedMessages);
} finally {
appender.stop();
}
}
}
Aggregations