Search in sources :

Example 21 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-native by spring-projects-experimental.

the class CLR method run.

@Override
public void run(String... args) throws Exception {
    {
        System.out.println("refresh index");
        repository.deleteAll();
        operations.indexOps(Conference.class).delete();
        operations.indexOps(Conference.class).create();
    }
    {
        System.out.println("\n--- REPOSITORY ---");
        // Save data sample
        repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London").keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build());
        repository.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London").keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build());
        repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin").keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999)).build());
        repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014").keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build());
        repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build());
        System.out.println("repository.count(): " + repository.count());
    }
    {
        System.out.println("\n--- CUSTOM REPOSITORY ---");
        SearchPage<Conference> searchPage = repository.findBySomeCustomImplementation("eXchange", PageRequest.of(0, 10));
        System.out.println("custom implementation finder.size(): " + searchPage.getSearchHits().getTotalHits());
    }
    String expectedDate = "2014-10-29";
    String expectedWord = "java";
    CriteriaQuery query = new CriteriaQuery(new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
    {
        System.out.println("\n--- TEMPLATE FIND ---");
        SearchHits<Conference> result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index"));
        System.out.println("result.size(): " + result.getSearchHits().size());
    }
    {
        System.out.println("\n--- REPOSITORY FINDER ---");
        List<Conference> result = repository.findByKeywordsContaining("spring");
        System.out.println("result.size(): " + result.size());
    }
    {
        System.out.println("\n--- REACTIVE TEMPLATE ---");
        System.out.println("reactiveTemplate.count(): " + reactiveOps.count(Query.findAll(), Conference.class, IndexCoordinates.of("conference-index")).block());
    }
    {
        System.out.println("\n--- REACTIVE REPOSITORY ---");
        System.out.println("reactiveRepository.count(): " + reactiveRepository.count().block());
    }
    {
    // does currently not work in SD ES - wrong reactor-netty dependency
    // System.out.println("\n--- REACTIVE REPOSITORY FINDER ---");
    // System.out.println("result.size(): " + reactiveRepository.findByKeywordsContaining("spring").collectList().block().size());
    }
    System.out.println("DONE");
}
Also used : SearchPage(org.springframework.data.elasticsearch.core.SearchPage) GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) List(java.util.List) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) SearchHits(org.springframework.data.elasticsearch.core.SearchHits)

Example 22 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplateUnitTests method searchShouldApplyIndexOptionsIfSet.

// DATAES-504, DATAES-518
@Test
public void searchShouldApplyIndexOptionsIfSet() {
    ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
    when(client.search(captor.capture())).thenReturn(Flux.empty());
    template.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    Query query = new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(0, 10));
    // 
    template.search(query, SampleEntity.class, index).as(// 
    StepVerifier::create).verifyComplete();
    assertThat(captor.getValue().indicesOptions()).isEqualTo(IndicesOptions.LENIENT_EXPAND_OPEN);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) StringQuery(org.springframework.data.elasticsearch.core.query.StringQuery) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 23 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplateUnitTests method searchShouldFallBackToDefaultIndexOptionsIfNotSet.

// DATAES-504, DATAES-518
@Test
public void searchShouldFallBackToDefaultIndexOptionsIfNotSet() {
    ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
    when(client.search(captor.capture())).thenReturn(Flux.empty());
    // 
    template.search(new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(0, 10)), SampleEntity.class).as(// 
    StepVerifier::create).verifyComplete();
    assertThat(captor.getValue().indicesOptions()).isEqualTo(DEFAULT_INDICES_OPTIONS);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 24 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplateUnitTests method searchShouldApplyPaginationIfSet.

// DATAES-504
@Test
public void searchShouldApplyPaginationIfSet() {
    ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
    when(client.search(captor.capture())).thenReturn(Flux.empty());
    Query query = new CriteriaQuery(new Criteria("*")).setPageable(PageRequest.of(2, 50));
    // 
    template.search(query, SampleEntity.class, index).as(// 
    StepVerifier::create).verifyComplete();
    assertThat(captor.getValue().source().from()).isEqualTo(100);
    assertThat(captor.getValue().source().size()).isEqualTo(50);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) StringQuery(org.springframework.data.elasticsearch.core.query.StringQuery) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 25 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplateUnitTests method searchShouldUseScrollIfPaginationNotSet.

// DATAES-504, DATAES-518
@Test
public void searchShouldUseScrollIfPaginationNotSet() {
    ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
    when(client.scroll(captor.capture())).thenReturn(Flux.empty());
    // 
    template.search(new CriteriaQuery(new Criteria("*")).setPageable(Pageable.unpaged()), SampleEntity.class).as(// 
    StepVerifier::create).verifyComplete();
    verify(client).scroll(any());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Aggregations

CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)38 Criteria (org.springframework.data.elasticsearch.core.query.Criteria)30 Test (org.junit.jupiter.api.Test)29 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)16 DisplayName (org.junit.jupiter.api.DisplayName)10 Query (org.springframework.data.elasticsearch.core.query.Query)8 MemberDocument (com.example.elasticsearch.member.domain.MemberDocument)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchHits (org.springframework.data.elasticsearch.core.SearchHits)2 StringQuery (org.springframework.data.elasticsearch.core.query.StringQuery)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 Method (java.lang.reflect.Method)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1 List (java.util.List)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 SearchHitsImpl (org.springframework.data.elasticsearch.core.SearchHitsImpl)1 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)1 GeoJsonPoint (org.springframework.data.elasticsearch.core.geo.GeoJsonPoint)1 GeoPoint (org.springframework.data.elasticsearch.core.geo.GeoPoint)1