use of org.hibernate.search.engine.search.sort.SearchSort in project infinispan by infinispan.
the class SearchQueryMaker method transform.
public SearchQueryParsingResult transform(IckleParsingResult<TypeMetadata> parsingResult, Map<String, Object> namedParameters, Class<?> targetedType, String targetedTypeName) {
if (searchMapping == null) {
throw log.noTypeIsIndexed(parsingResult.getQueryString());
}
this.namedParameters = namedParameters;
SearchSession querySession = searchMapping.getMappingSession();
SearchScope<?> scope = targetedTypeName == null ? querySession.scope(targetedType) : querySession.scope(targetedType, targetedTypeName);
predicateFactory = scope.predicate().extension(LuceneExtension.get());
indexedEntity = targetedTypeName == null ? searchMapping.indexedEntity(targetedType) : searchMapping.indexedEntity(targetedTypeName);
SearchPredicate predicate = makePredicate(parsingResult.getWhereClause()).toPredicate();
SearchProjectionInfo projection = makeProjection(parsingResult.getTargetEntityMetadata(), scope.projection(), parsingResult.getProjections(), parsingResult.getProjectedTypes());
SearchSort sort = makeSort(scope.sort(), parsingResult.getSortFields());
return new SearchQueryParsingResult(targetedType, targetedTypeName, projection, predicate, sort);
}
use of org.hibernate.search.engine.search.sort.SearchSort in project hibernate-search by hibernate.
the class LuceneMigrationUtils method toLuceneSort.
public static Sort toLuceneSort(SearchSort sort) {
List<SortField> result = new ArrayList<>();
LuceneSearchSortCollector collector = new LuceneSearchSortCollector() {
@Override
public void collectSortField(SortField sortField) {
result.add(sortField);
}
@Override
public void collectSortField(SortField sortField, LuceneFieldComparatorSource nestedFieldSort) {
result.add(sortField);
}
@Override
public void collectSortFields(SortField[] sortFields) {
Collections.addAll(result, sortFields);
}
};
((LuceneSearchSort) sort).toSortFields(collector);
return new Sort(result.toArray(new SortField[0]));
}
use of org.hibernate.search.engine.search.sort.SearchSort in project hibernate-search by hibernate.
the class SearchSortIT method reuseSortInstance_onScopeTargetingSameIndexes.
@Test
public void reuseSortInstance_onScopeTargetingSameIndexes() {
StubMappingScope scope = mainIndex.createScope();
SearchSort sort = scope.sort().field("string").asc().missing().last().toSort();
SearchQuery<DocumentReference> query = scope.query().where(f -> f.matchAll()).sort(sort).toQuery();
assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID);
// reuse the same sort instance on the same scope
query = scope.query().where(f -> f.matchAll()).sort(sort).toQuery();
assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID);
// reuse the same sort instance on a different scope,
// targeting the same index
query = mainIndex.createScope().query().where(f -> f.matchAll()).sort(sort).toQuery();
assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID);
sort = mainIndex.createScope(otherIndex).sort().field("string").asc().missing().last().toSort();
// reuse the same sort instance on a different scope,
// targeting same indexes
query = otherIndex.createScope(mainIndex).query().where(f -> f.matchAll()).sort(sort).toQuery();
assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, EMPTY_ID);
}
use of org.hibernate.search.engine.search.sort.SearchSort in project hibernate-search by hibernate.
the class LuceneExtensionIT method sort_fromLuceneSortField_separateSort.
@Test
public void sort_fromLuceneSortField_separateSort() {
StubMappingScope scope = mainIndex.createScope();
SearchSort sort1 = scope.sort().extension().ifSupported(LuceneExtension.get(), c2 -> c2.fromLuceneSortField(new SortedSetSortField("sort1", false))).orElseFail().toSort();
SearchSort sort2 = scope.sort().extension(LuceneExtension.get()).fromLuceneSortField(new SortedSetSortField("sort2", false)).toSort();
SearchSort sort3 = scope.sort().extension().ifSupported(LuceneExtension.get(), c2 -> c2.fromLuceneSortField(new SortedSetSortField("sort3", false))).orElseFail().toSort();
SearchQuery<DocumentReference> query = scope.query().where(f -> f.matchAll()).sort(f -> f.composite().add(sort1).add(sort2).add(sort3)).toQuery();
assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, FOURTH_ID, FIFTH_ID);
SearchSort sort = scope.sort().extension(LuceneExtension.get()).fromLuceneSort(new Sort(new SortedSetSortField("sort3", false), new SortedSetSortField("sort2", false), new SortedSetSortField("sort1", false))).toSort();
query = scope.query().where(f -> f.matchAll()).sort(sort).toQuery();
assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), THIRD_ID, SECOND_ID, FIRST_ID, FOURTH_ID, FIFTH_ID);
}
use of org.hibernate.search.engine.search.sort.SearchSort in project hibernate-search by hibernate.
the class SearchQueryBaseIT method componentsFromMappingWithoutSession.
/**
* A smoke test for components (predicate, sort, ...) created from the mapping without a session
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3671")
public void componentsFromMappingWithoutSession() {
SearchMapping mapping = Search.mapping(setupHolder.sessionFactory());
SearchScope<Book> scope = mapping.scope(Book.class);
/*
* The backend is a stub, so these components are stub too:
* we simply test that the mapper correctly delegates to the backend.
*/
SearchProjection<EntityReference> projection = scope.projection().entityReference().toProjection();
SearchPredicate predicate = scope.predicate().matchAll().toPredicate();
SearchSort sort = scope.sort().field("title").toSort();
SearchAggregation<Map<String, Long>> aggregation = scope.aggregation().terms().field("title", String.class).toAggregation();
assertSoftly(a -> {
a.assertThat(projection).isInstanceOf(StubSearchProjection.class);
a.assertThat(predicate).isInstanceOf(StubSearchPredicate.class);
a.assertThat(sort).isInstanceOf(StubSearchSort.class);
a.assertThat(aggregation).isInstanceOf(StubSearchAggregation.class);
});
/*
* ... and below we test that passing these objects to the DSL will work correctly.
* Objects are casted explicitly by the stub backend,
* so if a wrong object was passed, the whole query would fail.
*/
AggregationKey<Map<String, Long>> aggregationKey = AggregationKey.of("titleAgg");
setupHolder.runInTransaction(session -> {
SearchSession searchSession = Search.session(session);
SearchQuery<EntityReference> query = searchSession.search(scope).select(projection).where(predicate).sort(sort).aggregation(aggregationKey, aggregation).toQuery();
backendMock.expectSearchProjection(Book.NAME, StubSearchWorkBehavior.of(3L, StubBackendUtils.reference(Book.NAME, "1"), StubBackendUtils.reference(Book.NAME, "2"), StubBackendUtils.reference(Book.NAME, "3")));
assertThat(query.fetchAllHits()).containsExactlyInAnyOrder(EntityReferenceImpl.withName(Book.class, Book.NAME, 1), EntityReferenceImpl.withName(Book.class, Book.NAME, 2), EntityReferenceImpl.withName(Book.class, Book.NAME, 3));
});
}
Aggregations