Search in sources :

Example 1 with SearchHits

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

the class ElasticsearchPartQuery method execute.

@Override
public Object execute(Object[] parameters) {
    Class<?> clazz = queryMethod.getResultProcessor().getReturnedType().getDomainType();
    ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
    CriteriaQuery query = createQuery(accessor);
    Assert.notNull(query, "unsupported query");
    elasticsearchConverter.updateQuery(query, clazz);
    if (queryMethod.hasAnnotatedHighlight()) {
        query.setHighlightQuery(queryMethod.getAnnotatedHighlightQuery());
    }
    IndexCoordinates index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
    Object result = null;
    if (tree.isLimiting()) {
        // noinspection ConstantConditions
        query.setMaxResults(tree.getMaxResults());
    }
    if (tree.isDelete()) {
        result = countOrGetDocumentsForDelete(query, accessor);
        elasticsearchOperations.delete(query, clazz, index);
        elasticsearchOperations.indexOps(index).refresh();
    } else if (queryMethod.isPageQuery()) {
        query.setPageable(accessor.getPageable());
        SearchHits<?> searchHits = elasticsearchOperations.search(query, clazz, index);
        if (queryMethod.isSearchPageMethod()) {
            result = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
        } else {
            result = SearchHitSupport.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, query.getPageable()));
        }
    } else if (queryMethod.isStreamQuery()) {
        if (accessor.getPageable().isUnpaged()) {
            query.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
        } else {
            query.setPageable(accessor.getPageable());
        }
        result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(query, clazz, index));
    } else if (queryMethod.isCollectionQuery()) {
        if (accessor.getPageable().isUnpaged()) {
            int itemCount = (int) elasticsearchOperations.count(query, clazz, index);
            if (itemCount == 0) {
                result = new SearchHitsImpl<>(0, TotalHitsRelation.EQUAL_TO, Float.NaN, null, Collections.emptyList(), null, null);
            } else {
                query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
            }
        } else {
            query.setPageable(accessor.getPageable());
        }
        if (result == null) {
            result = elasticsearchOperations.search(query, clazz, index);
        }
    } else if (tree.isCountProjection()) {
        result = elasticsearchOperations.count(query, clazz, index);
    } else {
        result = elasticsearchOperations.searchOne(query, clazz, index);
    }
    return (queryMethod.isNotSearchHitMethod() && queryMethod.isNotSearchPageMethod()) ? SearchHitSupport.unwrapSearchHits(result) : result;
}
Also used : ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) SearchHitsImpl(org.springframework.data.elasticsearch.core.SearchHitsImpl) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 2 with SearchHits

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

use of org.springframework.data.elasticsearch.core.SearchHits in project openvsx by eclipse.

the class DatabaseSearchService method search.

@Cacheable("database.search")
public SearchHits<ExtensionSearch> search(ISearchService.Options options, Pageable pageRequest) {
    // grab all extensions
    var matchingExtensions = repositories.findAllActiveExtensions();
    // no extensions in the database
    if (matchingExtensions.isEmpty()) {
        Aggregations aggregations = new Aggregations(Collections.emptyList());
        return new SearchHitsImpl<ExtensionSearch>(0, TotalHitsRelation.OFF, 0f, "", Collections.emptyList(), aggregations);
    }
    // filter category
    if (options.category != null) {
        matchingExtensions = matchingExtensions.filter(extension -> extension.getLatest().getCategories().stream().anyMatch(category -> category.toLowerCase().equals(options.category.toLowerCase())));
    }
    // filter text
    if (options.queryString != null) {
        matchingExtensions = matchingExtensions.filter(extension -> extension.getName().toLowerCase().contains(options.queryString.toLowerCase()) || extension.getNamespace().getName().contains(options.queryString.toLowerCase()) || (extension.getLatest().getDescription() != null && extension.getLatest().getDescription().toLowerCase().contains(options.queryString.toLowerCase())) || (extension.getLatest().getDisplayName() != null && extension.getLatest().getDisplayName().toLowerCase().contains(options.queryString.toLowerCase())));
    }
    List<ExtensionSearch> sortedExtensions;
    if ("relevance".equals(options.sortBy)) {
        // for relevance we're using relevance service to get the relevance item
        var searchStats = new SearchStats(repositories);
        // needs to add relevance on extensions
        sortedExtensions = new ArrayList<>(matchingExtensions.map(extension -> relevanceService.toSearchEntry(extension, searchStats)).toList());
        // sort it
        sortedExtensions.sort(new RelevanceComparator());
    } else {
        sortedExtensions = matchingExtensions.stream().map(extension -> extension.toSearch()).collect(Collectors.toList());
        if ("downloadCount".equals(options.sortBy)) {
            sortedExtensions.sort(new DownloadedCountComparator());
        } else if ("averageRating".equals(options.sortBy)) {
            sortedExtensions.sort(new AverageRatingComparator());
        } else if ("timestamp".equals(options.sortBy)) {
            sortedExtensions.sort(new TimestampComparator());
        }
    }
    // 'asc' | 'desc';
    if ("desc".equals(options.sortOrder)) {
        // reverse the order
        Collections.reverse(sortedExtensions);
    }
    // Paging
    var totalHits = sortedExtensions.size();
    if (pageRequest != null) {
        var pageNumber = pageRequest.getPageNumber();
        var pageSize = pageRequest.getPageSize();
        var toSkip = 0;
        if (pageNumber >= 1) {
            // page is zero indexed
            toSkip = pageNumber * (pageSize - 1) + pageNumber;
        }
        // if something to skip, remove the first elements
        if (toSkip > 0 && toSkip < sortedExtensions.size()) {
            sortedExtensions = sortedExtensions.subList(toSkip, sortedExtensions.size());
        }
        // keep only the pageSize elements
        if (sortedExtensions.size() > pageSize) {
            sortedExtensions = sortedExtensions.subList(0, pageSize);
        }
    }
    List<SearchHit<ExtensionSearch>> searchHits;
    if (sortedExtensions.isEmpty()) {
        searchHits = Collections.emptyList();
    } else {
        // client is interested only in the extension IDs
        searchHits = sortedExtensions.stream().map(extensionSearch -> {
            return new SearchHit<ExtensionSearch>(null, null, 0.0f, Collections.emptyList().toArray(), Collections.emptyMap(), extensionSearch);
        }).collect(Collectors.toList());
    }
    Aggregations aggregations = new Aggregations(Collections.emptyList());
    SearchHits<ExtensionSearch> searchHitsResult = new SearchHitsImpl<ExtensionSearch>(totalHits, TotalHitsRelation.OFF, 0f, "", searchHits, aggregations);
    return searchHitsResult;
}
Also used : Extension(org.eclipse.openvsx.entities.Extension) Aggregations(org.elasticsearch.search.aggregations.Aggregations) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) Cacheable(org.springframework.cache.annotation.Cacheable) SearchHitsImpl(org.springframework.data.elasticsearch.core.SearchHitsImpl) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) RepositoryService(org.eclipse.openvsx.repositories.RepositoryService) List(java.util.List) Component(org.springframework.stereotype.Component) SearchStats(org.eclipse.openvsx.search.RelevanceService.SearchStats) Pageable(org.springframework.data.domain.Pageable) Comparator(java.util.Comparator) Collections(java.util.Collections) TotalHitsRelation(org.springframework.data.elasticsearch.core.TotalHitsRelation) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) Aggregations(org.elasticsearch.search.aggregations.Aggregations) SearchHitsImpl(org.springframework.data.elasticsearch.core.SearchHitsImpl) SearchStats(org.eclipse.openvsx.search.RelevanceService.SearchStats) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 4 with SearchHits

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

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

the class SearchAfterIntegrationTests method shouldReadPagesWithSearchAfter.

// #1143
@Test
@DisplayName("should read pages with search_after")
void shouldReadPagesWithSearchAfter() {
    List<Entity> entities = IntStream.rangeClosed(1, 10).mapToObj(i -> new Entity((long) i, "message " + i)).collect(Collectors.toList());
    operations.save(entities);
    Query query = Query.findAll();
    query.setPageable(PageRequest.of(0, 3));
    query.addSort(Sort.by(Sort.Direction.ASC, "id"));
    List<Object> searchAfter = null;
    List<Entity> foundEntities = new ArrayList<>();
    int loop = 0;
    do {
        query.setSearchAfter(searchAfter);
        SearchHits<Entity> searchHits = operations.search(query, Entity.class);
        if (searchHits.getSearchHits().size() == 0) {
            break;
        }
        foundEntities.addAll(searchHits.stream().map(searchHit -> searchHit.getContent()).collect(Collectors.toList()));
        searchAfter = searchHits.getSearchHit((int) (searchHits.getSearchHits().size() - 1)).getSortValues();
        if (++loop > 10) {
            fail("loop not terminating");
        }
    } while (true);
    assertThat(foundEntities).containsExactlyElementsOf(entities);
}
Also used : IntStream(java.util.stream.IntStream) Field(org.springframework.data.elasticsearch.annotations.Field) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) Query(org.springframework.data.elasticsearch.core.query.Query) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) List(java.util.List) FieldType(org.springframework.data.elasticsearch.annotations.FieldType) Document(org.springframework.data.elasticsearch.annotations.Document) ElasticsearchOperations(org.springframework.data.elasticsearch.core.ElasticsearchOperations) Assertions(org.assertj.core.api.Assertions) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) Id(org.springframework.data.annotation.Id) Query(org.springframework.data.elasticsearch.core.query.Query) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

SearchHits (org.springframework.data.elasticsearch.core.SearchHits)6 List (java.util.List)4 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)3 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Pageable (org.springframework.data.domain.Pageable)2 SearchHitsImpl (org.springframework.data.elasticsearch.core.SearchHitsImpl)2 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)2 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)2 Query (org.springframework.data.elasticsearch.core.query.Query)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 LocalDate (java.time.LocalDate)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 Assertions (org.assertj.core.api.Assertions)1 Extension (org.eclipse.openvsx.entities.Extension)1 RepositoryService (org.eclipse.openvsx.repositories.RepositoryService)1