use of co.elastic.clients.elasticsearch.core.SearchResponse in project elasticsearch-java by elastic.
the class RequestTest method testSearchAggregation.
@Test
public void testSearchAggregation() throws IOException {
client.create(_1 -> _1.index("products").id("A").document(new Product(5)).refresh(Refresh.True));
client.create(_1 -> _1.index("products").id("B").document(new Product(15)).refresh(Refresh.True));
client.create(_1 -> _1.index("products").id("C").document(new Product(25)).refresh(Refresh.True));
SearchResponse<Product> searchResponse = client.search(_1 -> _1.index("products").size(0).aggregations("prices", _3 -> _3.histogram(_4 -> _4.field("price").interval(10.0)).aggregations("average", _5 -> _5.avg(_6 -> _6.field("price")))), Product.class);
HistogramAggregate prices = searchResponse.aggregations().get("prices").histogram();
assertEquals(3, prices.buckets().array().size());
assertEquals(1, prices.buckets().array().get(0).docCount());
assertEquals(5.0, prices.buckets().array().get(0).aggregations().get("average").avg().value(), 0.01);
// We've set "size" to zero
assertEquals(0, searchResponse.hits().hits().size());
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project elasticsearch-java by elastic.
the class ApiConventionsTest method builderIntervals.
@Test(expected = TransportException.class)
public void builderIntervals() throws Exception {
ElasticsearchClient client = new ElasticsearchClient(transport);
// tag::builder-intervals
SearchResponse<SomeApplicationData> results = client.search(_0 -> _0.query(_1 -> _1.intervals(_2 -> _2.field("my_text").allOf(_3 -> _3.ordered(true).intervals(_4 -> _4.match(_5 -> _5.query("my favorite food").maxGaps(0).ordered(true))).intervals(_4 -> _4.anyOf(_5 -> _5.intervals(_6 -> _6.match(_7 -> _7.query("hot water"))).intervals(_6 -> _6.match(_7 -> _7.query("cold porridge")))))))), // <1>
SomeApplicationData.class);
// end::builder-intervals
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project opentelemetry-plugin by jenkinsci.
the class OpenTelemetryLogToElasticsearchIT method test.
@Test
public void test() throws Exception {
InputStream envAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(".env");
Assert.assertNotNull(".env file not found in classpath", envAsStream);
Properties env = new Properties();
env.load(envAsStream);
Map<String, String> configuration = new HashMap<>();
env.forEach((k, v) -> configuration.put(k.toString(), v.toString()));
configuration.put("otel.traces.exporter", "otlp");
configuration.put("otel.metrics.exporter", "otlp");
configuration.put("otel.logs.exporter", "otlp");
final int LOG_MESSAGE_COUNT = 100;
String traceId;
// PRODUCE OPEN TELEMETRY LOG MESSAGES
{
AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = AutoConfiguredOpenTelemetrySdk.builder().addPropertiesSupplier(() -> configuration).build();
try {
OpenTelemetrySdk sdk = autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk();
Tracer tracer = sdk.getTracer("test");
LogEmitter logEmitter = sdk.getSdkLogEmitterProvider().get("test");
Span span = tracer.spanBuilder("my-test-pipeline").startSpan();
try (Scope scope = span.makeCurrent()) {
for (int i = 0; i < LOG_MESSAGE_COUNT; i++) {
logEmitter.logBuilder().setContext(Context.current()).setBody("Log Message " + i).setAttributes(Attributes.of(AttributeKey.stringKey("myStringAttribute"), "Value " + i, AttributeKey.longKey("myNumericAttribute"), (long) i)).emit();
Thread.sleep(RANDOM.nextInt(100));
}
} finally {
span.end();
}
traceId = span.getSpanContext().getTraceId();
} finally {
autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk().getSdkLogEmitterProvider().close();
autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk().getSdkTracerProvider().close();
}
}
Thread.sleep(3_000);
// VERIFY LOG MESSAGES IN ELASTICSEARCH
{
String elasticsearchUrl = configuration.get("elasticsearch.url");
String elasticsearchUsername = configuration.get("elasticsearch.username");
String elasticsearchPassword = configuration.get("elasticsearch.password");
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticsearchUsername, elasticsearchPassword));
RestClient restClient = RestClient.builder(HttpHost.create(elasticsearchUrl)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();
RestClientTransport elasticsearchTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(elasticsearchTransport);
SearchRequest searchRequest = new SearchRequest.Builder().index("logs-apm.app-*").size(500).sort(s -> s.field(f -> f.field("@timestamp").order(SortOrder.Asc))).query(q -> q.match(m -> m.field("trace.id").query(FieldValue.of(traceId)))).build();
SearchResponse<ObjectNode> searchResponse = elasticsearchClient.search(searchRequest, ObjectNode.class);
List<Hit<ObjectNode>> hits = searchResponse.hits().hits();
if (hits.size() != LOG_MESSAGE_COUNT) {
System.err.println("Invalid number of log messages: actual: " + hits.size() + ", expected: " + LOG_MESSAGE_COUNT);
}
for (Hit<ObjectNode> hit : hits) {
ObjectNode source = hit.source();
ObjectNode labels = (ObjectNode) source.findValue("labels");
ObjectNode numericLabels = (ObjectNode) source.findValue("numeric_labels");
try {
String message = source.findValue("message").asText();
String myStringAttribute = labels.findValue("myStringAttribute").asText();
long myNumericAttribute = numericLabels.findValue("myNumericAttribute").longValue();
System.out.println(hit.id() + "\tmessage:'" + message + "', \tmyStringAttribute: '" + myStringAttribute + "', myNumericAttribute: " + myNumericAttribute);
} catch (Exception e) {
System.err.println("Error parsing " + source);
}
}
}
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project para-search-elasticsearch by Erudika.
the class ES method searchQueryRaw.
/**
* Executes an ElasticSearch query. This is the core method of the class.
* @param appid name of the {@link com.erudika.para.core.App}
* @param type type of object
* @param query the search query builder
* @param pager a {@link com.erudika.para.core.utils.Pager}
* @return a list of search results
*/
protected static SearchResponse<Map> searchQueryRaw(String appid, String type, QueryVariant query, Pager... pager) {
if (StringUtils.isBlank(appid)) {
return null;
}
Pager page = ESUtils.getPager(pager);
SortOrder order = page.isDesc() ? SortOrder.Desc : SortOrder.Asc;
int max = page.getLimit();
int pageNum = (int) page.getPage();
int start = (pageNum < 1 || pageNum > Para.getConfig().maxPages()) ? 0 : (pageNum - 1) * max;
if (query == null) {
query = QueryBuilders.matchAll().build();
}
if (!StringUtils.isBlank(type)) {
query = QueryBuilders.bool().must(query._toQuery(), QueryBuilders.term().field(Config._TYPE).value(v -> v.stringValue(type)).build()._toQuery()).build();
}
SearchResponse<Map> hits = null;
String debugQuery = "";
try {
SearchRequest.Builder search = new SearchRequest.Builder();
search.index(getIndexName(appid)).searchType(SearchType.DfsQueryThenFetch).query(query._toQuery()).size(max).trackTotalHits(ESUtils.getTrackTotalHits());
if (pageNum <= 1 && !StringUtils.isBlank(page.getLastKey())) {
search.searchAfter(page.getLastKey());
search.from(0);
search.sort(SortOptions.of(b -> b.field(f -> f.field("_docid").order(order))));
} else {
search.from(start);
for (SortOptions sortField : ESUtils.getSortFieldsFromPager(page)) {
search.sort(sortField);
}
}
debugQuery = search.toString();
logger.debug("Elasticsearch query: {}", debugQuery);
hits = getRESTClient().search(search.build(), Map.class);
page.setCount(Optional.ofNullable(hits.hits().total()).orElse(TotalHits.of(t -> t.relation(TotalHitsRelation.Eq).value(page.getCount()))).value());
if (hits.hits().hits().size() > 0) {
Object id = hits.hits().hits().get(hits.hits().hits().size() - 1).source().get("_docid");
if (id != null) {
page.setLastKey(id.toString());
}
}
} catch (Exception e) {
Throwable cause = e.getCause();
String msg = cause != null ? cause.getMessage() : e.getMessage();
logger.debug("No search results for type '{}' in app '{}': {}.\nQuery: {}", type, appid, msg, debugQuery);
}
return hits;
}
Aggregations