Search in sources :

Example 86 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientUnitTests method searchShouldHitSearchEndpoint.

// --> SEARCH
// DATAES-488
@Test
public void searchShouldHitSearchEndpoint() {
    // 
    hostProvider.when(HOST).receiveSearchOk();
    client.search(new SearchRequest("twitter")).as(StepVerifier::create).verifyComplete();
    verify(hostProvider.client(HOST)).method(HttpMethod.POST);
    URI uri = hostProvider.when(HOST).captureUri();
    assertThat(uri.getRawPath()).isEqualTo("/twitter/_search");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 87 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientUnitTests method searchShouldReturnEmptyFluxIfNothingFound.

// DATAES-488
@Test
public void searchShouldReturnEmptyFluxIfNothingFound() {
    // 
    hostProvider.when(HOST).receiveSearchOk();
    // 
    client.search(new SearchRequest("twitter")).as(// 
    StepVerifier::create).verifyComplete();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Test(org.junit.jupiter.api.Test)

Example 88 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientUnitTests method aggregateShouldReturnMultipleResultsCorrectly.

// DATAES-567
@Test
public void aggregateShouldReturnMultipleResultsCorrectly() {
    // 
    hostProvider.when(HOST).receive(// 
    Receive::json).body(fromPath("aggregate-ok-multiple-results"));
    // 
    client.aggregate(new SearchRequest("twitter")).as(// 
    StepVerifier::create).consumeNextWith(aggregation -> {
        assertThat(aggregation.getName()).isEqualTo("users");
        assertThat(aggregation instanceof ParsedStringTerms);
        ParsedStringTerms parsedStringTerms = (ParsedStringTerms) aggregation;
        assertThat(parsedStringTerms.getBuckets().size()).isEqualTo(2);
        assertThat(parsedStringTerms.getBucketByKey("kimchy").getDocCount()).isEqualTo(2);
        assertThat(parsedStringTerms.getBucketByKey("elastic").getDocCount()).isEqualTo(1);
    }).consumeNextWith(aggregation -> {
        assertThat(aggregation.getName()).isEqualTo("max_post_date");
        assertThat(aggregation instanceof ParsedMax);
        ParsedMax parsedMax = (ParsedMax) aggregation;
        assertThat(Instant.ofEpochMilli((long) parsedMax.getValue())).isEqualTo(Instant.parse("2010-01-15T01:46:38Z"));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) VersionType(org.elasticsearch.index.VersionType) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) IndexRequest(org.elasticsearch.action.index.IndexRequest) Assertions(org.assertj.core.api.Assertions) URI(java.net.URI) Result(org.elasticsearch.action.DocWriteResponse.Result) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) TimeValue(org.elasticsearch.core.TimeValue) StreamUtils(org.springframework.util.StreamUtils) GetRequest(org.elasticsearch.action.get.GetRequest) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Instant(java.time.Instant) ParsedMax(org.elasticsearch.search.aggregations.metrics.ParsedMax) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) XContentType(org.elasticsearch.xcontent.XContentType) HttpStatus(org.springframework.http.HttpStatus) RestStatus(org.elasticsearch.rest.RestStatus) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) MockDelegatingElasticsearchHostProvider(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider) SearchRequest(org.elasticsearch.action.search.SearchRequest) ParsedMax(org.elasticsearch.search.aggregations.metrics.ParsedMax) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) Test(org.junit.jupiter.api.Test)

Example 89 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplate method doCount.

private Mono<Long> doCount(Query query, Class<?> entityType, IndexCoordinates index) {
    return Mono.defer(() -> {
        SearchRequest request = requestFactory.searchRequest(query, entityType, index);
        request = prepareSearchRequest(request, false);
        return doCount(request);
    });
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest)

Example 90 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplate method doFind.

private Flux<SearchDocument> doFind(Query query, Class<?> clazz, IndexCoordinates index) {
    return Flux.defer(() -> {
        SearchRequest request = requestFactory.searchRequest(query, clazz, index);
        boolean useScroll = !(query.getPageable().isPaged() || query.isLimiting());
        request = prepareSearchRequest(request, useScroll);
        if (useScroll) {
            return doScroll(request);
        } else {
            return doFind(request);
        }
    });
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)608 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)420 SearchResponse (org.elasticsearch.action.search.SearchResponse)363 IOException (java.io.IOException)197 SearchHit (org.elasticsearch.search.SearchHit)111 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)101 ArrayList (java.util.ArrayList)71 Test (org.junit.jupiter.api.Test)55 SearchHits (org.elasticsearch.search.SearchHits)53 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)49 Test (org.junit.Test)47 TimeValue (org.elasticsearch.common.unit.TimeValue)45 HashMap (java.util.HashMap)41 List (java.util.List)40 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)38 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)37 Map (java.util.Map)33 IndexRequest (org.elasticsearch.action.index.IndexRequest)33 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)30 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)29