Search in sources :

Example 1 with SearchResponse

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);
}
Also used : Aggregate(co.elastic.clients.elasticsearch._types.aggregations.Aggregate) AvgAggregate(co.elastic.clients.elasticsearch._types.aggregations.AvgAggregate) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) StringTermsBucket(co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket) TotalHitsRelation(co.elastic.clients.elasticsearch.core.search.TotalHitsRelation) ListBuilder(co.elastic.clients.util.ListBuilder) Test(org.junit.Test) StringTermsAggregate(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) MapBuilder(co.elastic.clients.util.MapBuilder) Collections(java.util.Collections) JsonpDeserializer(co.elastic.clients.json.JsonpDeserializer) StringTermsAggregate(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) ListBuilder(co.elastic.clients.util.ListBuilder) MapBuilder(co.elastic.clients.util.MapBuilder) StringTermsBucket(co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket) Aggregate(co.elastic.clients.elasticsearch._types.aggregations.Aggregate) AvgAggregate(co.elastic.clients.elasticsearch._types.aggregations.AvgAggregate) StringTermsAggregate(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) Test(org.junit.Test)

Example 2 with SearchResponse

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();
    }
}
Also used : SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) ConsoleNotes(io.jenkins.plugins.opentelemetry.job.log.ConsoleNotes) Tracer(io.opentelemetry.api.trace.Tracer) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Iterators(com.google.common.collect.Iterators) Level(java.util.logging.Level) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) JenkinsOtelSemanticAttributes(io.jenkins.plugins.opentelemetry.semconv.JenkinsOtelSemanticAttributes) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Time(co.elastic.clients.elasticsearch._types.Time) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) Span(io.opentelemetry.api.trace.Span) Iterator(java.util.Iterator) Scope(io.opentelemetry.context.Scope) IOException(java.io.IOException) Logger(java.util.logging.Logger) Objects(java.util.Objects) Hit(co.elastic.clients.elasticsearch.core.search.Hit) List(java.util.List) TracerProvider(io.opentelemetry.api.trace.TracerProvider) JSONArray(net.sf.json.JSONArray) Closeable(java.io.Closeable) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) VisibleForTesting(com.google.common.annotations.VisibleForTesting) QueryBuilders(co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders) Collections(java.util.Collections) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) Span(io.opentelemetry.api.trace.Span) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Scope(io.opentelemetry.context.Scope)

Example 3 with SearchResponse

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);
}
Also used : Arrays(java.util.Arrays) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) Like(co.elastic.clients.elasticsearch._types.query_dsl.Like) ESUtils.getNestedKey(com.erudika.para.server.search.es.ESUtils.getNestedKey) LoggerFactory(org.slf4j.LoggerFactory) TotalHitsRelation(co.elastic.clients.elasticsearch.core.search.TotalHitsRelation) ESUtils.getRESTClient(com.erudika.para.server.search.es.ESUtils.getRESTClient) StringUtils(org.apache.commons.lang3.StringUtils) ESUtils.qs(com.erudika.para.server.search.es.ESUtils.qs) ESUtils.convertQueryStringToNestedQuery(com.erudika.para.server.search.es.ESUtils.convertQueryStringToNestedQuery) PROPS_PREFIX(com.erudika.para.server.search.es.ESUtils.PROPS_PREFIX) Map(java.util.Map) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) ESUtils.getTermsQuery(com.erudika.para.server.search.es.ESUtils.getTermsQuery) Config(com.erudika.para.core.utils.Config) ParaObject(com.erudika.para.core.ParaObject) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ESUtils.nestedMode(com.erudika.para.server.search.es.ESUtils.nestedMode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Tag(com.erudika.para.core.Tag) ESUtils.executeRequests(com.erudika.para.server.search.es.ESUtils.executeRequests) Optional(java.util.Optional) Para(com.erudika.para.core.utils.Para) QueryBuilders(co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders) App(com.erudika.para.core.App) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) SearchType(co.elastic.clients.elasticsearch._types.SearchType) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) HashMap(java.util.HashMap) ChildScoreMode(co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode) Pager(com.erudika.para.core.utils.Pager) ArrayList(java.util.ArrayList) ESUtils.keyValueBoolQuery(com.erudika.para.server.search.es.ESUtils.keyValueBoolQuery) TotalHits(co.elastic.clients.elasticsearch.core.search.TotalHits) DAO(com.erudika.para.core.persistence.DAO) LinkedList(java.util.LinkedList) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) ESUtils.nestedPropsQuery(com.erudika.para.server.search.es.ESUtils.nestedPropsQuery) Logger(org.slf4j.Logger) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) Address(com.erudika.para.core.Address) ESUtils.getValueFieldName(com.erudika.para.server.search.es.ESUtils.getValueFieldName) ESUtils.getIndexName(com.erudika.para.server.search.es.ESUtils.getIndexName) Utils(com.erudika.para.core.utils.Utils) TimeUnit(java.util.concurrent.TimeUnit) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions) Collections(java.util.Collections) Address(com.erudika.para.core.Address) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) Pager(com.erudika.para.core.utils.Pager) Objects(java.util.Objects) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with SearchResponse

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
}
Also used : ElasticsearchTransport(co.elastic.clients.transport.ElasticsearchTransport) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) RestClient(org.elasticsearch.client.RestClient) Hit(co.elastic.clients.elasticsearch.core.search.Hit) ElasticsearchTransport(co.elastic.clients.transport.ElasticsearchTransport) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) Ignore(org.junit.Ignore) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) Test(org.junit.Test) HttpHost(org.apache.http.HttpHost) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with SearchResponse

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());
}
Also used : CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) BeforeClass(org.junit.BeforeClass) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) CompletableFuture(java.util.concurrent.CompletableFuture) IndexState(co.elastic.clients.elasticsearch.indices.IndexState) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) DateTime(co.elastic.clients.util.DateTime) Map(java.util.Map) OperationType(co.elastic.clients.elasticsearch.core.bulk.OperationType) ModelTestCase(co.elastic.clients.elasticsearch.model.ModelTestCase) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Refresh(co.elastic.clients.elasticsearch._types.Refresh) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) HistogramAggregate(co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate) NodesResponse(co.elastic.clients.elasticsearch.cat.NodesResponse) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ElasticsearchTestServer(co.elastic.clients.elasticsearch.ElasticsearchTestServer) Test(org.junit.Test) IOException(java.io.IOException) GetIndexResponse(co.elastic.clients.elasticsearch.indices.GetIndexResponse) Instant(java.time.Instant) GetIndicesSettingsResponse(co.elastic.clients.elasticsearch.indices.GetIndicesSettingsResponse) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) MsearchResponse(co.elastic.clients.elasticsearch.core.MsearchResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClosePointInTimeResponse(co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) DiskUsageResponse(co.elastic.clients.elasticsearch.indices.DiskUsageResponse) ClearScrollResponse(co.elastic.clients.elasticsearch.core.ClearScrollResponse) Assert(org.junit.Assert) Collections(java.util.Collections) IndexResponse(co.elastic.clients.elasticsearch.core.IndexResponse) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Aggregations

SearchResponse (co.elastic.clients.elasticsearch.core.SearchResponse)9 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)6 Collections (java.util.Collections)6 SortOrder (co.elastic.clients.elasticsearch._types.SortOrder)5 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)5 Hit (co.elastic.clients.elasticsearch.core.search.Hit)5 List (java.util.List)5 Map (java.util.Map)5 Test (org.junit.Test)5 FieldValue (co.elastic.clients.elasticsearch._types.FieldValue)4 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)4 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)4 Assert (org.junit.Assert)4 ElasticsearchAsyncClient (co.elastic.clients.elasticsearch.ElasticsearchAsyncClient)3 CreateIndexResponse (co.elastic.clients.elasticsearch.indices.CreateIndexResponse)3 HashMap (java.util.HashMap)3 TimeUnit (java.util.concurrent.TimeUnit)3 ElasticsearchTestServer (co.elastic.clients.elasticsearch.ElasticsearchTestServer)2 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)2 Refresh (co.elastic.clients.elasticsearch._types.Refresh)2