use of co.elastic.clients.elasticsearch.core.SearchResponse in project elasticsearch-java by elastic.
the class TypedKeysTest method testAdditionalProperties.
@Test
public void testAdditionalProperties() {
Aggregate avg1 = AvgAggregate.of(_1 -> _1.value(1.0))._toAggregate();
Aggregate avg2 = AvgAggregate.of(_1 -> _1.value(2.0))._toAggregate();
Aggregate aggregate = StringTermsAggregate.of(_0 -> _0.sumOtherDocCount(1).buckets(b -> b.array(ListBuilder.of(StringTermsBucket.Builder::new).add(_1 -> _1.key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1))).add(_1 -> _1.key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2))).build())))._toAggregate();
SearchResponse<Void> resp = new SearchResponse.Builder<Void>().aggregations("foo", aggregate).took(1).shards(_1 -> _1.successful(1).failed(0).total(1)).hits(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)).hits(Collections.emptyList())).timedOut(false).build();
String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}," + "\"aggregations\":{\"sterms#foo\":{\"buckets\":[" + "{\"avg#bar\":{\"value\":1.0},\"doc_count\":1,\"key\":\"key_1\"}," + "{\"avg#bar\":{\"value\":2.0},\"doc_count\":2,\"key\":\"key_2\"}" + "],\"sum_other_doc_count\":1}}}";
assertEquals(json, toJson(resp));
resp = fromJson(json, SearchResponse.createSearchResponseDeserializer(JsonpDeserializer.voidDeserializer()));
StringTermsAggregate foo = resp.aggregations().get("foo").sterms();
assertEquals(1, foo.sumOtherDocCount());
assertEquals(1, foo.buckets().array().get(0).docCount());
assertEquals("key_1", foo.buckets().array().get(0).key());
assertEquals(1.0, foo.buckets().array().get(0).aggregations().get("bar").avg().value(), 0.01);
assertEquals("key_2", foo.buckets().array().get(1).key());
assertEquals(2.0, foo.buckets().array().get(1).aggregations().get("bar").avg().value(), 0.01);
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project opentelemetry-plugin by jenkinsci.
the class ElasticsearchLogsSearchIterator method loadNextFormattedLogLines.
protected Iterator<String> loadNextFormattedLogLines() throws IOException {
String loadPointInTimeId = this.lazyLoadPointInTimeId();
Span esSearchSpan = tracer.spanBuilder("Elasticsearch.search").startSpan();
try (Scope esSearchSpanScope = esSearchSpan.makeCurrent()) {
esSearchSpan.setAttribute("query.pointInTimeId", lazyLoadPointInTimeId()).setAttribute("query.from", context.from).setAttribute("query.size", PAGE_SIZE).setAttribute("query.match.traceId", traceId).setAttribute("query.match.jobFullName", jobFullName).setAttribute("query.match.runNumber", runNumber);
BoolQuery.Builder queryBuilder = QueryBuilders.bool().must(QueryBuilders.match().field(ElasticsearchFields.FIELD_TRACE_ID).query(FieldValue.of(traceId)).build()._toQuery(), QueryBuilders.match().field(ElasticsearchFields.FIELD_CI_PIPELINE_ID).query(FieldValue.of(jobFullName)).build()._toQuery(), QueryBuilders.match().field(ElasticsearchFields.FIELD_CI_PIPELINE_RUN_NUMBER).query(FieldValue.of(runNumber)).build()._toQuery());
if (flowNodeId != null) {
esSearchSpan.setAttribute("query.match.flowNodeId", flowNodeId);
queryBuilder.must(QueryBuilders.match().field(ElasticsearchFields.FIELD_JENKINS_STEP_ID).query(FieldValue.of(flowNodeId)).build()._toQuery());
}
Query query = queryBuilder.build()._toQuery();
SearchRequest searchRequest = new SearchRequest.Builder().pit(pit -> pit.id(loadPointInTimeId).keepAlive(POINT_IN_TIME_KEEP_ALIVE)).from(context.from).size(PAGE_SIZE).sort(s -> s.field(f -> f.field(ElasticsearchFields.FIELD_TIMESTAMP).order(SortOrder.Asc))).query(query).build();
SearchResponse<ObjectNode> searchResponse = this.esClient.search(searchRequest, ObjectNode.class);
List<Hit<ObjectNode>> hits = searchResponse.hits().hits();
esSearchSpan.setAttribute("response.size", hits.size());
context.from = context.from + hits.size();
return hits.stream().map(new ElasticsearchHitToFormattedLogLine()).filter(Objects::nonNull).iterator();
} finally {
esSearchSpan.end();
queryCounter.incrementAndGet();
}
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project para-search-elasticsearch by Erudika.
the class ES method findNearbyInternal.
public static <P extends ParaObject> List<P> findNearbyInternal(String appid, String type, String query, int radius, double lat, double lng, Pager... pager) {
if (StringUtils.isBlank(type) || StringUtils.isBlank(appid)) {
return Collections.emptyList();
}
if (StringUtils.isBlank(query)) {
query = "*";
}
// find nearby Address objects
Pager page = getPager(pager);
QueryVariant qb1 = QueryBuilders.geoDistance().field("latlng").location(l -> l.latlon(ll -> ll.lat(lat).lon(lng))).distance(Integer.toString(radius) + " km").build();
SearchResponse<Map> hits1 = searchQueryRaw(appid, Utils.type(Address.class), qb1, page);
// will cause problems if not cleared
page.setLastKey(null);
if (hits1 == null) {
return Collections.emptyList();
}
if (type.equals(Utils.type(Address.class))) {
return searchQuery(appid, hits1);
}
// then find their parent objects
List<String> parentIds = hits1.hits().hits().stream().filter(Objects::nonNull).map(h -> (String) h.source().get(Config._PARENTID)).collect(Collectors.toList());
QueryVariant qb2 = QueryBuilders.bool().must(QueryBuilders.queryString().query(qs(query)).build()._toQuery()).filter(QueryBuilders.ids().values(parentIds).build()._toQuery()).build();
SearchResponse<Map> hits2 = searchQueryRaw(appid, type, qb2, page);
return searchQuery(appid, hits2);
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project elasticsearch-java by elastic.
the class ConnectingTest method createClient.
// we don't have a running ES
@Ignore
@Test
public void createClient() throws Exception {
// tag::create-client
// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);
// end::create-client
// tag::first-request
SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);
for (Hit<Product> hit : search.hits().hits()) {
processProduct(hit.source());
}
// end::first-request
}
use of co.elastic.clients.elasticsearch.core.SearchResponse in project elasticsearch-java by elastic.
the class RequestTest method testDataIngestion.
@Test
public void testDataIngestion() throws Exception {
String index = "ingest-test";
// Create an index
CreateIndexResponse createIndexResponse = client.indices().create(b -> b.index(index));
assertEquals(index, createIndexResponse.index());
// Check that it actually exists. Example of a boolean response
BooleanResponse existsResponse = client.indices().exists(b -> b.index(index));
assertTrue(existsResponse.value());
// Ingest some data
AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");
String docId = client.index(b -> b.index(index).id(// test with url-unsafe string
"my/Id").document(appData).refresh(// Make it visible for search
Refresh.True)).id();
assertEquals("my/Id", docId);
// Check auto-created mapping
GetMappingResponse mapping = client.indices().getMapping(b -> b.index(index));
assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind());
// Query by id
AppData esData = client.get(b -> b.index(index).id(docId), AppData.class).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
// Query by id a non-existing document
final GetResponse<AppData> notExists = client.get(b -> b.index(index).id("some-random-id"), AppData.class);
assertFalse(notExists.found());
assertNull(notExists.source());
// Search
SearchResponse<AppData> search = client.search(b -> b.index(index), AppData.class);
long hits = search.hits().total().value();
assertEquals(1, hits);
esData = search.hits().hits().get(0).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
RequestItem item = RequestItem.of(_1 -> _1.header(_2 -> _2.index("test")).body(_2 -> _2.size(4)));
// MSearch: 1st search on an existing index, 2nd one on a non-existing index
final MsearchResponse<AppData> msearch = client.msearch(_0 -> _0.searches(_1 -> _1.header(_3 -> _3.index(index)).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))).searches(_1 -> _1.header(_3 -> _3.index("non-existing")).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))), AppData.class);
assertEquals(2, msearch.responses().size());
assertTrue(msearch.responses().get(0).isResult());
assertEquals(1, msearch.responses().get(0).result().hits().hits().size());
assertTrue(msearch.responses().get(1).isFailure());
assertEquals(404, msearch.responses().get(1).failure().status());
}
Aggregations