Search in sources :

Example 1 with SearchPage

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

the class SimpleElasticsearchRepository method findAll.

@SuppressWarnings("unchecked")
@Override
public Page<T> findAll(Pageable pageable) {
    NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withPageable(pageable).build();
    SearchHits<T> searchHits = execute(operations -> operations.search(query, entityClass, getIndexCoordinates()));
    // noinspection ConstantConditions
    SearchPage<T> page = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
    return (Page<T>) SearchHitSupport.unwrapSearchHits(page);
}
Also used : SearchPage(org.springframework.data.elasticsearch.core.SearchPage) Page(org.springframework.data.domain.Page) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery)

Example 2 with SearchPage

use of org.springframework.data.elasticsearch.core.SearchPage in project prolog by woowacourse.

the class StudylogDocumentService method findBySearchKeyword.

@Override
public StudylogDocumentResponse findBySearchKeyword(String keyword, List<Long> tags, List<Long> missions, List<Long> levels, List<String> usernames, LocalDate start, LocalDate end, Pageable pageable) {
    final Query query = StudylogDocumentQueryBuilder.makeQuery(preprocess(keyword), tags, missions, levels, usernames, start, end, pageable);
    // Query 결과를 ES에서 조회한다.
    final SearchHits<StudylogDocument> searchHits = elasticsearchRestTemplate.search(query, StudylogDocument.class, IndexCoordinates.of("studylog-document"));
    // 조회된 SearchHits를 페이징할 수 있는 SearchPage로 변경한다.
    final SearchPage<StudylogDocument> searchPages = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
    final List<Long> studylogIds = searchPages.stream().map(searchPage -> searchPage.getContent().getId()).collect(toList());
    return StudylogDocumentResponse.of(studylogIds, searchPages.getTotalElements(), searchPages.getTotalPages(), searchPages.getNumber());
}
Also used : IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) Query(org.springframework.data.elasticsearch.core.query.Query) SearchPage(org.springframework.data.elasticsearch.core.SearchPage) Profile(org.springframework.context.annotation.Profile) StudylogDocumentRepository(wooteco.prolog.studylog.domain.repository.StudylogDocumentRepository) StudylogDocumentResponse(wooteco.prolog.studylog.application.dto.StudylogDocumentResponse) ElasticsearchRestTemplate(org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate) SearchHitSupport(org.springframework.data.elasticsearch.core.SearchHitSupport) StudylogDocumentQueryBuilder(wooteco.prolog.studylog.domain.StudylogDocumentQueryBuilder) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) StudylogDocument(wooteco.prolog.studylog.domain.StudylogDocument) Pageable(org.springframework.data.domain.Pageable) StudylogRepository(wooteco.prolog.studylog.domain.repository.StudylogRepository) Query(org.springframework.data.elasticsearch.core.query.Query) StudylogDocument(wooteco.prolog.studylog.domain.StudylogDocument)

Example 3 with SearchPage

use of org.springframework.data.elasticsearch.core.SearchPage 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 4 with SearchPage

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

the class SimpleElasticsearchRepository method searchSimilar.

@SuppressWarnings("unchecked")
@Override
public Page<T> searchSimilar(T entity, @Nullable String[] fields, Pageable pageable) {
    Assert.notNull(entity, "Cannot search similar records for 'null'.");
    Assert.notNull(pageable, "'pageable' cannot be 'null'");
    MoreLikeThisQuery query = new MoreLikeThisQuery();
    query.setId(stringIdRepresentation(extractIdFromBean(entity)));
    query.setPageable(pageable);
    if (fields != null) {
        query.addFields(fields);
    }
    SearchHits<T> searchHits = execute(operations -> operations.search(query, entityClass, getIndexCoordinates()));
    SearchPage<T> searchPage = SearchHitSupport.searchPageFor(searchHits, pageable);
    return (Page<T>) SearchHitSupport.unwrapSearchHits(searchPage);
}
Also used : MoreLikeThisQuery(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery) SearchPage(org.springframework.data.elasticsearch.core.SearchPage) Page(org.springframework.data.domain.Page)

Aggregations

SearchPage (org.springframework.data.elasticsearch.core.SearchPage)4 List (java.util.List)2 Page (org.springframework.data.domain.Page)2 SearchHits (org.springframework.data.elasticsearch.core.SearchHits)2 LocalDate (java.time.LocalDate)1 Collectors.toList (java.util.stream.Collectors.toList)1 Profile (org.springframework.context.annotation.Profile)1 Pageable (org.springframework.data.domain.Pageable)1 ElasticsearchRestTemplate (org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate)1 SearchHitSupport (org.springframework.data.elasticsearch.core.SearchHitSupport)1 GeoPoint (org.springframework.data.elasticsearch.core.geo.GeoPoint)1 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)1 Criteria (org.springframework.data.elasticsearch.core.query.Criteria)1 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)1 MoreLikeThisQuery (org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery)1 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1 Query (org.springframework.data.elasticsearch.core.query.Query)1 Service (org.springframework.stereotype.Service)1 StudylogDocumentResponse (wooteco.prolog.studylog.application.dto.StudylogDocumentResponse)1