use of org.springframework.data.elasticsearch.core.SearchHitsImpl 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;
}
use of org.springframework.data.elasticsearch.core.SearchHitsImpl 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;
}
use of org.springframework.data.elasticsearch.core.SearchHitsImpl in project openvsx by eclipse.
the class VSCodeAdapterTest method mockSearch.
// ---------- UTILITY ----------//
private void mockSearch(boolean active) {
var entry1 = new ExtensionSearch();
entry1.id = 1;
var searchHit = new SearchHit<ExtensionSearch>("0", "1", 1.0f, null, null, entry1);
var searchHits = new SearchHitsImpl<ExtensionSearch>(1, TotalHitsRelation.EQUAL_TO, 1.0f, "1", Arrays.asList(searchHit), new Aggregations(Collections.emptyList()));
Mockito.when(search.isEnabled()).thenReturn(true);
var searchOptions = new ISearchService.Options("yaml", null, 50, 0, "desc", "relevance", false);
Mockito.when(search.search(searchOptions, PageRequest.of(0, 50))).thenReturn(searchHits);
var extension = mockExtensionDTO();
List<ExtensionDTO> results = active ? List.of(extension) : Collections.emptyList();
Mockito.when(repositories.findAllActiveExtensionDTOsById(List.of(entry1.id))).thenReturn(results);
var publicIds = Set.of(extension.getPublicId());
Mockito.when(repositories.findAllActiveExtensionDTOsByPublicId(publicIds)).thenReturn(results);
var ids = List.of(extension.getId());
Mockito.when(repositories.findAllActiveReviewCountsByExtensionId(ids)).thenReturn(Map.of(extension.getId(), 10));
var name = extension.getName();
var namespaceName = extension.getNamespace().getName();
Mockito.when(repositories.findActiveExtensionDTOByNameAndNamespaceName(name, namespaceName)).thenReturn(extension);
mockFileResourceDTOs(extension.getLatest());
}
use of org.springframework.data.elasticsearch.core.SearchHitsImpl in project openvsx by eclipse.
the class RegistryAPITest method mockSearch.
private List<Extension> mockSearch() {
var extVersion = mockExtension();
var extension = extVersion.getExtension();
extension.setId(1l);
var entry1 = new ExtensionSearch();
entry1.id = 1;
var searchHit = new SearchHit<ExtensionSearch>("0", "1", 1.0f, null, null, entry1);
var searchHits = new SearchHitsImpl<ExtensionSearch>(1, TotalHitsRelation.EQUAL_TO, 1.0f, "1", Arrays.asList(searchHit), new Aggregations(Collections.emptyList()));
Mockito.when(search.isEnabled()).thenReturn(true);
var searchOptions = new ISearchService.Options("foo", null, 10, 0, "desc", "relevance", false);
Mockito.when(search.search(searchOptions, PageRequest.of(0, 10))).thenReturn(searchHits);
Mockito.when(entityManager.find(Extension.class, 1l)).thenReturn(extension);
return Arrays.asList(extension);
}
Aggregations