Search in sources :

Example 1 with SearchSort

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);
}
Also used : SearchSession(org.infinispan.search.mapper.session.SearchSession) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort)

Example 2 with SearchSort

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]));
}
Also used : ArrayList(java.util.ArrayList) LuceneFieldComparatorSource(org.hibernate.search.backend.lucene.types.sort.comparatorsource.impl.LuceneFieldComparatorSource) Sort(org.apache.lucene.search.Sort) LuceneSearchSort(org.hibernate.search.backend.lucene.search.sort.impl.LuceneSearchSort) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SortField(org.apache.lucene.search.SortField) LuceneSearchSortCollector(org.hibernate.search.backend.lucene.search.sort.impl.LuceneSearchSortCollector) LuceneSearchSort(org.hibernate.search.backend.lucene.search.sort.impl.LuceneSearchSort)

Example 3 with SearchSort

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);
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FieldSortOptionsStep(org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsStep) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SortFinalStep(org.hibernate.search.engine.search.sort.dsl.SortFinalStep) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) SearchSortFactory(org.hibernate.search.engine.search.sort.dsl.SearchSortFactory) Function(java.util.function.Function) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Before(org.junit.Before) Sortable(org.hibernate.search.engine.backend.types.Sortable) SearchPredicateFactory(org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory) Test(org.junit.Test) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Rule(org.junit.Rule) SearchSortFactoryExtension(org.hibernate.search.engine.search.sort.dsl.SearchSortFactoryExtension) Optional(java.util.Optional) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) SearchResult(org.hibernate.search.engine.search.query.SearchResult) DefaultAnalysisDefinitions(org.hibernate.search.integrationtest.backend.tck.testsupport.configuration.DefaultAnalysisDefinitions) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Test(org.junit.Test)

Example 4 with SearchSort

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);
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) Backend(org.hibernate.search.engine.backend.Backend) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) LuceneIndexManager(org.hibernate.search.backend.lucene.index.LuceneIndexManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IndexableField(org.apache.lucene.index.IndexableField) Term(org.apache.lucene.index.Term) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) LuceneSearchQuery(org.hibernate.search.backend.lucene.search.query.LuceneSearchQuery) LuceneExtension(org.hibernate.search.backend.lucene.LuceneExtension) LuceneSearchSortFactory(org.hibernate.search.backend.lucene.search.sort.dsl.LuceneSearchSortFactory) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) Map(java.util.Map) IndexManager(org.hibernate.search.engine.backend.index.IndexManager) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) SortField(org.apache.lucene.search.SortField) FailureReportUtils(org.hibernate.search.util.impl.integrationtest.common.reporting.FailureReportUtils) LuceneSearchQuerySelectStep(org.hibernate.search.backend.lucene.search.query.dsl.LuceneSearchQuerySelectStep) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) LatLonPoint(org.apache.lucene.document.LatLonPoint) Explanation(org.apache.lucene.search.Explanation) Sort(org.apache.lucene.search.Sort) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Collectors(java.util.stream.Collectors) LuceneBackend(org.hibernate.search.backend.lucene.LuceneBackend) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Aggregable(org.hibernate.search.engine.backend.types.Aggregable) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) Optional(java.util.Optional) LuceneSearchResult(org.hibernate.search.backend.lucene.search.query.LuceneSearchResult) LuceneSearchPredicateFactory(org.hibernate.search.backend.lucene.search.predicate.dsl.LuceneSearchPredicateFactory) IndexReader(org.apache.lucene.index.IndexReader) Projectable(org.hibernate.search.engine.backend.types.Projectable) IndexSearcher(org.apache.lucene.search.IndexSearcher) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) DocumentAssert.containsDocument(org.hibernate.search.integrationtest.backend.lucene.testsupport.util.DocumentAssert.containsDocument) StringField(org.apache.lucene.document.StringField) SearchIntegration(org.hibernate.search.engine.common.spi.SearchIntegration) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) ArrayList(java.util.ArrayList) MetadataFields(org.hibernate.search.backend.lucene.lowlevel.common.impl.MetadataFields) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Store(org.apache.lucene.document.Field.Store) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) IntPoint(org.apache.lucene.document.IntPoint) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) ValueWrapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.ValueWrapper) Before(org.junit.Before) TopDocs(org.apache.lucene.search.TopDocs) LuceneSearchScroll(org.hibernate.search.backend.lucene.search.query.LuceneSearchScroll) LuceneSearchQueryWhereStep(org.hibernate.search.backend.lucene.search.query.dsl.LuceneSearchQueryWhereStep) Sortable(org.hibernate.search.engine.backend.types.Sortable) EventContexts(org.hibernate.search.engine.reporting.spi.EventContexts) LuceneSearchScrollResult(org.hibernate.search.backend.lucene.search.query.LuceneSearchScrollResult) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) Consumer(java.util.function.Consumer) LuceneSearchQueryOptionsStep(org.hibernate.search.backend.lucene.search.query.dsl.LuceneSearchQueryOptionsStep) TermQuery(org.apache.lucene.search.TermQuery) Rule(org.junit.Rule) StubLoadingOptionsStep(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingOptionsStep) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) Type(org.apache.lucene.search.SortField.Type) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) Sort(org.apache.lucene.search.Sort) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Test(org.junit.Test)

Example 5 with SearchSort

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));
    });
}
Also used : SearchMapping(org.hibernate.search.mapper.orm.mapping.SearchMapping) SearchSession(org.hibernate.search.mapper.orm.session.SearchSession) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) StubSearchPredicate(org.hibernate.search.util.impl.integrationtest.common.stub.backend.search.predicate.impl.StubSearchPredicate) EntityReference(org.hibernate.search.mapper.orm.common.EntityReference) Map(java.util.Map) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) StubSearchSort(org.hibernate.search.util.impl.integrationtest.common.stub.backend.search.sort.impl.StubSearchSort) Test(org.junit.Test) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue)

Aggregations

SearchSort (org.hibernate.search.engine.search.sort.SearchSort)13 SearchException (org.hibernate.search.util.common.SearchException)9 TestForIssue (org.hibernate.search.util.impl.test.annotation.TestForIssue)9 Test (org.junit.Test)9 List (java.util.List)8 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)8 DocumentReference (org.hibernate.search.engine.backend.common.DocumentReference)8 IndexSchemaElement (org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement)8 Sortable (org.hibernate.search.engine.backend.types.Sortable)8 SearchPredicate (org.hibernate.search.engine.search.predicate.SearchPredicate)8 SearchQuery (org.hibernate.search.engine.search.query.SearchQuery)8 SearchSetupHelper (org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper)8 SearchResultAssert.assertThatQuery (org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery)8 SimpleMappedIndex (org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex)8 StubMappingScope (org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope)8 ArrayList (java.util.ArrayList)6 Optional (java.util.Optional)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 IndexFieldReference (org.hibernate.search.engine.backend.document.IndexFieldReference)6 IndexObjectFieldReference (org.hibernate.search.engine.backend.document.IndexObjectFieldReference)6