use of org.hibernate.search.engine.search.query.SearchResult in project hibernate-search by hibernate.
the class SearchProjectionIT method score.
@Test
public void score() {
StubMappingScope scope = mainIndex.createScope();
SearchQuery<Float> query = scope.query().select(f -> f.score()).where(f -> f.match().field(mainIndex.binding().scoreField.relativeFieldName).matching("scorepattern")).sort(f -> f.score().desc()).toQuery();
SearchResult<Float> result = query.fetchAll();
assertThatResult(result).hasTotalHitCount(2);
Float score1 = result.hits().get(0);
Float score2 = result.hits().get(1);
assertThat(score1).isNotNull().isNotNaN();
assertThat(score2).isNotNull().isNotNaN();
assertThat(score1).isGreaterThan(score2);
}
use of org.hibernate.search.engine.search.query.SearchResult in project hibernate-search by hibernate.
the class AnnotationMappingSmokeIT method search_multipleElementsProjection.
@Test
public void search_multipleElementsProjection() {
SearchScope<ParentIndexedEntity> scope = mapping.scope(Arrays.asList(IndexedEntity.class, YetAnotherIndexedEntity.class));
try (SearchSession session = mapping.createSession()) {
SearchQuery<List<?>> query = session.search(scope).select(scope.projection().field("myTextField", String.class).toProjection(), scope.projection().entityReference().toProjection(), scope.projection().id().toProjection(), scope.projection().field("myLocalDateField", LocalDate.class).toProjection(), scope.projection().documentReference().toProjection(), scope.projection().field("customBridgeOnClass.text", String.class).toProjection()).where(f -> f.matchAll()).toQuery();
backendMock.expectSearchProjection(Arrays.asList(IndexedEntity.INDEX, YetAnotherIndexedEntity.INDEX), StubSearchWorkBehavior.of(2L, Arrays.asList("text1", reference(IndexedEntity.INDEX, "0"), reference(IndexedEntity.INDEX, "0"), LocalDate.of(2017, 11, 1), reference(IndexedEntity.INDEX, "0"), "text2"), Arrays.asList(null, reference(YetAnotherIndexedEntity.INDEX, "1"), reference(YetAnotherIndexedEntity.INDEX, "1"), LocalDate.of(2017, 11, 2), reference(YetAnotherIndexedEntity.INDEX, "1"), null)));
SearchResult<List<?>> result = query.fetchAll();
assertThat(result.hits()).containsExactly(Arrays.asList("text1", EntityReferenceImpl.withDefaultName(IndexedEntity.class, 0), 0, LocalDate.of(2017, 11, 1), reference(IndexedEntity.INDEX, "0"), "text2"), Arrays.asList(null, EntityReferenceImpl.withDefaultName(YetAnotherIndexedEntity.class, 1), 1, LocalDate.of(2017, 11, 2), reference(YetAnotherIndexedEntity.INDEX, "1"), null));
assertThat(result.total().hitCount()).isEqualTo(2L);
backendMock.verifyExpectationsMet();
}
}
use of org.hibernate.search.engine.search.query.SearchResult in project hibernate-search by hibernate.
the class ProgrammaticMappingSmokeIT method search_singleElementProjection.
@Test
public void search_singleElementProjection() {
try (SearchSession session = mapping.createSession()) {
SearchQuery<String> query = session.search(Arrays.asList(IndexedEntity.class, YetAnotherIndexedEntity.class)).select(f -> f.field("myTextField", String.class)).where(f -> f.matchAll()).toQuery();
backendMock.expectSearchProjection(Arrays.asList(IndexedEntity.INDEX, YetAnotherIndexedEntity.INDEX), b -> b.offset(3).limit(2), StubSearchWorkBehavior.of(2L, "text1", null));
SearchResult<String> result = query.fetch(3, 2);
assertThat(result.hits()).containsExactly("text1", null);
assertThat(result.total().hitCount()).isEqualTo(2L);
backendMock.verifyExpectationsMet();
}
}
use of org.hibernate.search.engine.search.query.SearchResult in project hibernate-search by hibernate.
the class AggregationDslIT method entryPoint.
@Test
public void entryPoint() {
with(entityManagerFactory).runInTransaction(entityManager -> {
// tag::entryPoint-lambdas[]
SearchSession searchSession = Search.session(entityManager);
// <1>
AggregationKey<Map<Genre, Long>> countsByGenreKey = AggregationKey.of("countsByGenre");
SearchResult<Book> result = // <2>
searchSession.search(Book.class).where(f -> // <3>
f.match().field("title").matching("robot")).aggregation(countsByGenreKey, f -> // <4>
f.terms().field("genre", Genre.class)).fetch(// <5>
20);
// <6>
Map<Genre, Long> countsByGenre = result.aggregation(countsByGenreKey);
// end::entryPoint-lambdas[]
assertThat(countsByGenre).containsExactly(entry(Genre.SCIENCE_FICTION, 2L));
});
with(entityManagerFactory).runInTransaction(entityManager -> {
// tag::entryPoint-objects[]
SearchSession searchSession = Search.session(entityManager);
SearchScope<Book> scope = searchSession.scope(Book.class);
AggregationKey<Map<Genre, Long>> countsByGenreKey = AggregationKey.of("countsByGenre");
SearchResult<Book> result = searchSession.search(scope).where(scope.predicate().match().field("title").matching("robot").toPredicate()).aggregation(countsByGenreKey, scope.aggregation().terms().field("genre", Genre.class).toAggregation()).fetch(20);
Map<Genre, Long> countsByGenre = result.aggregation(countsByGenreKey);
// end::entryPoint-objects[]
assertThat(countsByGenre).containsExactly(entry(Genre.SCIENCE_FICTION, 2L));
});
}
use of org.hibernate.search.engine.search.query.SearchResult in project hibernate-search by hibernate.
the class AbstractOnTheFlyIndexingBenchmarks method concurrentQuery.
@Benchmark
@GroupThreads(2 * AbstractBackendHolder.INDEX_COUNT)
@Group("concurrentReadWrite")
public void concurrentQuery(QueryParams params, Blackhole blackhole) {
PerThreadIndexPartition partition = getIndexPartition();
MappedIndex index = partition.getIndex();
SearchResult<DocumentReference> results = index.createScope().query().where(f -> f.matchAll()).sort(f -> f.field(MappedIndex.SHORT_TEXT_FIELD_NAME)).fetch(params.getQueryMaxResults());
blackhole.consume(results.total().hitCount());
for (DocumentReference hit : results.hits()) {
blackhole.consume(hit);
}
}
Aggregations