use of org.hibernate.search.backend.lucene.search.query.LuceneSearchResult in project hibernate-search by hibernate.
the class LuceneExtensionIT method query.
@Test
public void query() {
StubMappingScope scope = mainIndex.createScope();
SearchQuery<DocumentReference> genericQuery = scope.query().where(f -> f.matchAll()).toQuery();
// Put the query and result into variables to check they have the right type
LuceneSearchQuery<DocumentReference> query = genericQuery.extension(LuceneExtension.get());
LuceneSearchResult<DocumentReference> result = query.fetchAll();
assertThatResult(result).fromQuery(query).hasDocRefHitsAnyOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, FOURTH_ID, FIFTH_ID).hasTotalHitCount(5);
// Unsupported extension
assertThatThrownBy(() -> query.extension((SearchQuery<DocumentReference> original, SearchLoadingContext<?, ?> loadingContext) -> Optional.empty())).isInstanceOf(SearchException.class);
}
use of org.hibernate.search.backend.lucene.search.query.LuceneSearchResult in project hibernate-search by hibernate.
the class LuceneExtensionIT method queryContext.
@Test
@SuppressWarnings("unused")
public void queryContext() {
StubMappingScope scope = mainIndex.createScope();
// Put intermediary contexts into variables to check they have the right type
LuceneSearchQuerySelectStep<DocumentReference, DocumentReference, StubLoadingOptionsStep> context1 = scope.query().extension(LuceneExtension.get());
LuceneSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> context2 = context1.select(f -> f.composite().from(f.documentReference(), f.document()).as((docRef, document) -> docRef));
// Note we can use Lucene-specific predicates immediately
LuceneSearchQueryOptionsStep<DocumentReference, StubLoadingOptionsStep> context3 = context2.where(f -> f.fromLuceneQuery(new MatchAllDocsQuery()));
// Note we can use Lucene-specific sorts immediately
LuceneSearchQueryOptionsStep<DocumentReference, StubLoadingOptionsStep> context4 = context3.sort(f -> f.fromLuceneSortField(new SortedSetSortField("sort1", false)));
// Put the query and result into variables to check they have the right type
LuceneSearchQuery<DocumentReference> query = context4.toQuery();
LuceneSearchResult<DocumentReference> result = query.fetchAll();
assertThatResult(result).fromQuery(query).hasDocRefHitsAnyOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, FOURTH_ID, FIFTH_ID).hasTotalHitCount(5);
// Also check (at compile time) the context type for other asXXX() methods, since we need to override each method explicitly
LuceneSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> selectEntityReferenceContext = scope.query().extension(LuceneExtension.get()).selectEntityReference();
LuceneSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> selectEntityContext = scope.query().extension(LuceneExtension.get()).selectEntity();
SearchProjection<DocumentReference> projection = scope.projection().documentReference().toProjection();
LuceneSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> selectProjectionContext = scope.query().extension(LuceneExtension.get()).select(projection);
LuceneSearchQueryWhereStep<List<?>, ?> selectProjectionsContext = scope.query().extension(LuceneExtension.get()).select(projection, projection);
LuceneSearchQueryOptionsStep<DocumentReference, StubLoadingOptionsStep> defaultResultContext = scope.query().extension(LuceneExtension.get()).where(f -> f.fromLuceneQuery(new MatchAllDocsQuery()));
}
use of org.hibernate.search.backend.lucene.search.query.LuceneSearchResult in project hibernate-search by hibernate.
the class LuceneSearchTopDocsTotalHitCountOnMatchAllDocsIT method matchAllDocs_sortByScoreDesc.
@Test
// The regression was spotted early, while introducing it in HSEARCH-4068
@TestForIssue(jiraKey = "HSEARCH-4068")
public void matchAllDocs_sortByScoreDesc() {
LuceneSearchResult<DocumentReference> result = index.query().extension(LuceneExtension.get()).where(f -> f.matchAll()).fetch(10);
assertThatResult(result).hasTotalHitCount(DOCUMENT_COUNT);
TopDocs topDocs = result.topDocs();
assertThat(topDocs.totalHits.relation).isEqualTo(TotalHits.Relation.EQUAL_TO);
assertThat(topDocs.totalHits.value).isEqualTo(DOCUMENT_COUNT);
}
use of org.hibernate.search.backend.lucene.search.query.LuceneSearchResult in project hibernate-search by hibernate.
the class LuceneQueryDslIT method lowLevel.
@Test
public void lowLevel() {
with(entityManagerFactory).runInTransaction(entityManager -> {
SearchSession searchSession = Search.session(entityManager);
// tag::lucene-lowLevel[]
LuceneSearchQuery<Book> query = searchSession.search(Book.class).extension(// <1>
LuceneExtension.get()).where(f -> f.match().field("title").matching("robot")).sort(f -> f.field("title_sort")).toQuery();
// <3>
Sort sort = query.luceneSort();
// <4>
LuceneSearchResult<Book> result = query.fetch(20);
// <5>
TopDocs topDocs = result.topDocs();
// end::lucene-lowLevel[]
assertThat(result.hits()).extracting(Book::getId).containsExactly(BOOK1_ID, BOOK3_ID);
assertThat(sort).isNotNull();
assertThat(sort.getSort()).hasSize(1);
assertThat(sort.getSort()[0].getType()).isEqualTo(SortField.Type.CUSTOM);
assertThat(topDocs).isNotNull();
assertThat(topDocs.totalHits.value).isEqualTo(2L);
assertThat(topDocs.scoreDocs).hasSize(2);
});
}
use of org.hibernate.search.backend.lucene.search.query.LuceneSearchResult in project hibernate-search by hibernate.
the class LuceneExtensionIT method query_topDocs.
@Test
public void query_topDocs() {
StubMappingScope scope = mainIndex.createScope();
LuceneSearchResult<DocumentReference> result = scope.query().extension(LuceneExtension.get()).where(f -> f.matchAll()).fetchAll();
assertThat(result.topDocs()).isNotNull();
}
Aggregations