Search in sources :

Example 1 with SearchResult

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);
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) Strictness(org.mockito.quality.Strictness) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StubDocumentReferenceConverter(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubDocumentReferenceConverter) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) Function(java.util.function.Function) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) StubTransformedReference(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubTransformedReference) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) NormalizationUtils.reference(org.hibernate.search.util.impl.integrationtest.common.NormalizationUtils.reference) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) MockitoJUnit(org.mockito.junit.MockitoJUnit) StandardFieldMapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.StandardFieldMapper) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) IndexFieldTypeFactory(org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactory) Before(org.junit.Before) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) ProjectionFinalStep(org.hibernate.search.engine.search.projection.dsl.ProjectionFinalStep) MapperMockUtils.expectHitMapping(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.MapperMockUtils.expectHitMapping) 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) GenericStubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.GenericStubMappingScope) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Rule(org.junit.Rule) SearchProjectionFactoryExtension(org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactoryExtension) DocumentReferenceConverter(org.hibernate.search.engine.backend.common.spi.DocumentReferenceConverter) StubLoadedObject(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubLoadedObject) StandardIndexFieldTypeOptionsStep(org.hibernate.search.engine.backend.types.dsl.StandardIndexFieldTypeOptionsStep) Optional(java.util.Optional) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) SearchResult(org.hibernate.search.engine.search.query.SearchResult) MockitoRule(org.mockito.junit.MockitoRule) Projectable(org.hibernate.search.engine.backend.types.Projectable) DefaultAnalysisDefinitions(org.hibernate.search.integrationtest.backend.tck.testsupport.configuration.DefaultAnalysisDefinitions) Mockito.mock(org.mockito.Mockito.mock) SearchProjectionFactory(org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) GenericStubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.GenericStubMappingScope) Test(org.junit.Test)

Example 2 with SearchResult

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();
    }
}
Also used : EntityReferenceImpl(org.hibernate.search.mapper.javabean.common.impl.EntityReferenceImpl) IntegerAsStringValueBridge(org.hibernate.search.integrationtest.mapper.pojo.smoke.bridge.IntegerAsStringValueBridge) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StubBackendUtils.reference(org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils.reference) JavaBeanMappingSetupHelper(org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) DocumentId(org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId) PropertyValue(org.hibernate.search.mapper.pojo.mapping.definition.annotation.PropertyValue) BuiltinContainerExtractors(org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) Map(java.util.Map) StubSearchWorkBehavior(org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior) LinkedHashSet(java.util.LinkedHashSet) AssociationInverseSide(org.hibernate.search.mapper.pojo.mapping.definition.annotation.AssociationInverseSide) Before(org.junit.Before) SearchScope(org.hibernate.search.mapper.javabean.scope.SearchScope) ContainerExtraction(org.hibernate.search.mapper.pojo.extractor.mapping.annotation.ContainerExtraction) CustomPropertyBinding(org.hibernate.search.integrationtest.mapper.pojo.smoke.bridge.CustomPropertyBinding) MethodHandles(java.lang.invoke.MethodHandles) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Set(java.util.Set) ValueBridgeRef(org.hibernate.search.mapper.pojo.bridge.mapping.annotation.ValueBridgeRef) Test(org.junit.Test) IndexedEmbedded(org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexedEmbedded) Indexed(org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed) List(java.util.List) Rule(org.junit.Rule) GenericField(org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField) LocalDate(java.time.LocalDate) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) SearchResult(org.hibernate.search.engine.search.query.SearchResult) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) ObjectPath(org.hibernate.search.mapper.pojo.mapping.definition.annotation.ObjectPath) CustomTypeBinding(org.hibernate.search.integrationtest.mapper.pojo.smoke.bridge.CustomTypeBinding) CollectionHelper(org.hibernate.search.util.common.impl.CollectionHelper) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with SearchResult

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();
    }
}
Also used : EntityReferenceImpl(org.hibernate.search.mapper.javabean.common.impl.EntityReferenceImpl) IntegerAsStringValueBridge(org.hibernate.search.integrationtest.mapper.pojo.smoke.bridge.IntegerAsStringValueBridge) Arrays(java.util.Arrays) PojoModelPath(org.hibernate.search.mapper.pojo.model.path.PojoModelPath) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StubBackendUtils.reference(org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils.reference) JavaBeanMappingSetupHelper(org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) BuiltinContainerExtractors(org.hibernate.search.mapper.pojo.extractor.builtin.BuiltinContainerExtractors) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) Map(java.util.Map) TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) StubSearchWorkBehavior(org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) SearchScope(org.hibernate.search.mapper.javabean.scope.SearchScope) CustomPropertyBridge(org.hibernate.search.integrationtest.mapper.pojo.smoke.bridge.CustomPropertyBridge) CustomTypeBridge(org.hibernate.search.integrationtest.mapper.pojo.smoke.bridge.CustomTypeBridge) MethodHandles(java.lang.invoke.MethodHandles) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test) List(java.util.List) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext) Rule(org.junit.Rule) LocalDate(java.time.LocalDate) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) SearchResult(org.hibernate.search.engine.search.query.SearchResult) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) CollectionHelper(org.hibernate.search.util.common.impl.CollectionHelper) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test)

Example 4 with SearchResult

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));
    });
}
Also used : Arrays(java.util.Arrays) Range(org.hibernate.search.util.common.data.Range) Search(org.hibernate.search.mapper.orm.Search) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BackendConfigurations(org.hibernate.search.documentation.testsupport.BackendConfigurations) DocumentationSetupHelper(org.hibernate.search.documentation.testsupport.DocumentationSetupHelper) Map(java.util.Map) OrmUtils.with(org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) ZoneOffset(java.time.ZoneOffset) Before(org.junit.Before) SearchScope(org.hibernate.search.mapper.orm.scope.SearchScope) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Date(java.sql.Date) Consumer(java.util.function.Consumer) List(java.util.List) Rule(org.junit.Rule) SearchSession(org.hibernate.search.mapper.orm.session.SearchSession) EntityManagerFactory(javax.persistence.EntityManagerFactory) LocalDate(java.time.LocalDate) RangeBoundInclusion(org.hibernate.search.util.common.data.RangeBoundInclusion) SearchResult(org.hibernate.search.engine.search.query.SearchResult) SearchSession(org.hibernate.search.mapper.orm.session.SearchSession) Map(java.util.Map) Test(org.junit.Test)

Example 5 with SearchResult

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);
    }
}
Also used : Group(org.openjdk.jmh.annotations.Group) Futures(org.hibernate.search.util.common.impl.Futures) Blackhole(org.openjdk.jmh.infra.Blackhole) AbstractBackendHolder(org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.AbstractBackendHolder) Random(java.util.Random) Scope(org.openjdk.jmh.annotations.Scope) GroupThreads(org.openjdk.jmh.annotations.GroupThreads) ArrayList(java.util.ArrayList) Dataset(org.hibernate.search.integrationtest.performance.backend.base.testsupport.dataset.Dataset) Threads(org.openjdk.jmh.annotations.Threads) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) DocumentRefreshStrategy(org.hibernate.search.engine.backend.work.execution.DocumentRefreshStrategy) Setup(org.openjdk.jmh.annotations.Setup) DatasetHolder(org.hibernate.search.integrationtest.performance.backend.base.testsupport.dataset.DatasetHolder) Param(org.openjdk.jmh.annotations.Param) MappedIndex(org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.MappedIndex) State(org.openjdk.jmh.annotations.State) Benchmark(org.openjdk.jmh.annotations.Benchmark) StubMapperUtils(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMapperUtils) DocumentCommitStrategy(org.hibernate.search.engine.backend.work.execution.DocumentCommitStrategy) List(java.util.List) Stream(java.util.stream.Stream) PerThreadIndexPartition(org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.PerThreadIndexPartition) Level(org.openjdk.jmh.annotations.Level) IndexIndexingPlan(org.hibernate.search.engine.backend.work.execution.spi.IndexIndexingPlan) Fork(org.openjdk.jmh.annotations.Fork) SearchResult(org.hibernate.search.engine.search.query.SearchResult) Collections(java.util.Collections) PerThreadIndexPartition(org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.PerThreadIndexPartition) MappedIndex(org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.MappedIndex) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Group(org.openjdk.jmh.annotations.Group) GroupThreads(org.openjdk.jmh.annotations.GroupThreads) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

SearchResult (org.hibernate.search.engine.search.query.SearchResult)28 List (java.util.List)26 Before (org.junit.Before)25 Rule (org.junit.Rule)25 Test (org.junit.Test)25 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)24 Arrays (java.util.Arrays)19 SearchQuery (org.hibernate.search.engine.search.query.SearchQuery)19 ArrayList (java.util.ArrayList)18 Search (org.hibernate.search.mapper.orm.Search)18 SearchSession (org.hibernate.search.mapper.orm.session.SearchSession)18 EntityManagerFactory (javax.persistence.EntityManagerFactory)17 OrmUtils.with (org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with)17 BackendConfigurations (org.hibernate.search.documentation.testsupport.BackendConfigurations)15 DocumentationSetupHelper (org.hibernate.search.documentation.testsupport.DocumentationSetupHelper)15 Optional (java.util.Optional)13 EntityManager (javax.persistence.EntityManager)12 Duration (java.time.Duration)11 TimeUnit (java.util.concurrent.TimeUnit)11 EntityGraph (javax.persistence.EntityGraph)11