Search in sources :

Example 1 with EntityReference

use of org.hibernate.search.mapper.javabean.common.EntityReference in project hibernate-search by hibernate.

the class SearchTimeoutIT method failAfter.

@Test
public void failAfter() {
    try (SearchSession session = mapping.createSession()) {
        SearchQuery<EntityReference> query = session.search(IndexedEntity.class).selectEntityReference().where(f -> f.matchAll()).failAfter(5L, TimeUnit.SECONDS).toQuery();
        SearchException timeoutException = new SearchException("Timed out");
        backendMock.expectSearchReferences(Collections.singletonList(INDEX_NAME), // timeout is supposed to be set on the backend
        b -> b.failAfter(5L, TimeUnit.SECONDS), StubSearchWorkBehavior.failing(() -> timeoutException));
        // Just check that the exception is propagated
        assertThatThrownBy(() -> query.fetchAll()).isSameAs(timeoutException);
    }
}
Also used : EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) SearchException(org.hibernate.search.util.common.SearchException) Test(org.junit.Test)

Example 2 with EntityReference

use of org.hibernate.search.mapper.javabean.common.EntityReference 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 EntityReference

use of org.hibernate.search.mapper.javabean.common.EntityReference in project hibernate-search by hibernate.

the class DocumentIdBaseIT method customBridge_withParams_programmaticMapping.

@Test
public void customBridge_withParams_programmaticMapping() {
    class IndexedEntity {

        Integer id;

        String value;

        IndexedEntity(Integer id, String value) {
            this.id = id;
            this.value = value;
        }
    }
    backendMock.expectSchema(INDEX_NAME, b -> b.field("value", String.class));
    SearchMapping mapping = setupHelper.start().withConfiguration(builder -> {
        builder.addEntityType(IndexedEntity.class);
        TypeMappingStep indexedEntity = builder.programmaticMapping().type(IndexedEntity.class);
        indexedEntity.indexed().index(INDEX_NAME);
        indexedEntity.property("id").documentId().identifierBinder(new ParametricBridge.ParametricBinder(), Collections.singletonMap("fixedPrefix", "fixed-prefix-"));
        indexedEntity.property("value").genericField();
    }).expectCustomBeans().setup(IndexedEntity.class);
    backendMock.verifyExpectationsMet();
    try (SearchSession session = mapping.createSession()) {
        IndexedEntity entity = new IndexedEntity(1, "bla-bla-bla");
        session.indexingPlan().add(entity);
        backendMock.expectWorks(INDEX_NAME).add("fixed-prefix-1", b -> b.field("value", "bla-bla-bla"));
    }
    backendMock.verifyExpectationsMet();
    try (SearchSession session = mapping.createSession()) {
        backendMock.expectSearchReferences(Collections.singletonList(INDEX_NAME), StubSearchWorkBehavior.of(1L, StubBackendUtils.reference("IndexedEntity", "fixed-prefix-1")));
        SearchQuery<EntityReference> query = session.search(IndexedEntity.class).selectEntityReference().where(f -> f.matchAll()).toQuery();
        assertThat(query.fetchAll().hits()).containsExactly(EntityReferenceImpl.withName(IndexedEntity.class, "IndexedEntity", 1));
    }
    backendMock.verifyExpectationsMet();
}
Also used : EntityReferenceImpl(org.hibernate.search.mapper.javabean.common.impl.EntityReferenceImpl) SearchException(org.hibernate.search.util.common.SearchException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) 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) Param(org.hibernate.search.mapper.pojo.common.annotation.Param) IdentifierBindingContext(org.hibernate.search.mapper.pojo.bridge.binding.IdentifierBindingContext) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) IdentifierBinderRef(org.hibernate.search.mapper.pojo.bridge.mapping.annotation.IdentifierBinderRef) TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) FailureReportUtils(org.hibernate.search.util.impl.integrationtest.common.reporting.FailureReportUtils) StubSearchWorkBehavior(org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior) IdentifierBridgeRef(org.hibernate.search.mapper.pojo.bridge.mapping.annotation.IdentifierBridgeRef) MethodHandles(java.lang.invoke.MethodHandles) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test) Indexed(org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed) IdentifierBridge(org.hibernate.search.mapper.pojo.bridge.IdentifierBridge) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Rule(org.junit.Rule) GenericField(org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField) IdentifierBridgeFromDocumentIdentifierContext(org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeFromDocumentIdentifierContext) StubBackendUtils(org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) IdentifierBridgeToDocumentIdentifierContext(org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeToDocumentIdentifierContext) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) Collections(java.util.Collections) IdentifierBinder(org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.IdentifierBinder) TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test)

Example 4 with EntityReference

use of org.hibernate.search.mapper.javabean.common.EntityReference in project hibernate-search by hibernate.

the class DocumentIdDefaultBridgeOverridingIT method projection_entityReference.

@Test
public void projection_entityReference() {
    try (SearchSession session = mapping.createSession()) {
        backendMock.expectSearchReferences(Collections.singletonList(DefaultIdentifierBridgeExpectations.TYPE_WITH_IDENTIFIER_BRIDGE_1_NAME), StubSearchWorkBehavior.of(1L, StubBackendUtils.reference(DefaultIdentifierBridgeExpectations.TYPE_WITH_IDENTIFIER_BRIDGE_1_NAME, getDocumentIdentifierValue())));
        SearchQuery<EntityReference> query = session.search(expectations.getTypeWithIdentifierBridge1()).selectEntityReference().where(f -> f.matchAll()).toQuery();
        assertThat(query.fetchAll().hits()).containsExactly(EntityReferenceImpl.withName(expectations.getTypeWithIdentifierBridge1(), DefaultIdentifierBridgeExpectations.TYPE_WITH_IDENTIFIER_BRIDGE_1_NAME, getEntityIdentifierValue()));
        backendMock.verifyExpectationsMet();
    }
}
Also used : EntityReferenceImpl(org.hibernate.search.mapper.javabean.common.impl.EntityReferenceImpl) DslConverter(org.hibernate.search.engine.backend.types.converter.spi.DslConverter) DefaultIdentifierBridgeExpectations(org.hibernate.search.integrationtest.mapper.pojo.testsupport.types.expectations.DefaultIdentifierBridgeExpectations) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JavaBeanMappingSetupHelper(org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) RunWith(org.junit.runner.RunWith) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) StubSearchWorkBehavior(org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) PropertyTypeDescriptor(org.hibernate.search.integrationtest.mapper.pojo.testsupport.types.PropertyTypeDescriptor) MethodHandles(java.lang.invoke.MethodHandles) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test) ToDocumentValueConvertContextImpl(org.hibernate.search.engine.backend.types.converter.runtime.spi.ToDocumentValueConvertContextImpl) StubIndexModel(org.hibernate.search.util.impl.integrationtest.common.stub.backend.document.model.impl.StubIndexModel) IdentifierBridge(org.hibernate.search.mapper.pojo.bridge.IdentifierBridge) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Rule(org.junit.Rule) IdentifierBridgeFromDocumentIdentifierContext(org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeFromDocumentIdentifierContext) StubBackendUtils(org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils) ToDocumentValueConvertContext(org.hibernate.search.engine.backend.types.converter.runtime.ToDocumentValueConvertContext) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) IdentifierBridgeToDocumentIdentifierContext(org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeToDocumentIdentifierContext) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) Collections(java.util.Collections) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test)

Example 5 with EntityReference

use of org.hibernate.search.mapper.javabean.common.EntityReference in project hibernate-search by hibernate.

the class ProvidedIdIT method indexAndSearch.

@Test
public void indexAndSearch() {
    final String entityAndIndexName = "indexed";
    @Indexed
    class IndexedEntity {
    }
    // Schema
    backendMock.expectSchema(entityAndIndexName, b -> {
    });
    SearchMapping mapping = withBaseConfiguration().withAnnotatedEntityType(IndexedEntity.class, entityAndIndexName).setup();
    backendMock.verifyExpectationsMet();
    // Indexing
    try (SearchSession session = mapping.createSession()) {
        IndexedEntity entity1 = new IndexedEntity();
        session.indexingPlan().add("42", null, entity1);
        backendMock.expectWorks(entityAndIndexName).add("42", b -> {
        });
    }
    backendMock.verifyExpectationsMet();
    // Searching
    try (SearchSession session = mapping.createSession()) {
        backendMock.expectSearchReferences(Collections.singletonList(entityAndIndexName), StubSearchWorkBehavior.of(1L, StubBackendUtils.reference(entityAndIndexName, "42")));
        SearchQuery<EntityReference> query = session.search(IndexedEntity.class).selectEntityReference().where(f -> f.matchAll()).toQuery();
        assertThat(query.fetchAll().hits()).containsExactly(EntityReferenceImpl.withName(IndexedEntity.class, entityAndIndexName, "42"));
    }
    backendMock.verifyExpectationsMet();
}
Also used : EntityReferenceImpl(org.hibernate.search.mapper.javabean.common.impl.EntityReferenceImpl) SearchException(org.hibernate.search.util.common.SearchException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MethodHandles(java.lang.invoke.MethodHandles) JavaBeanMappingSetupHelper(org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) HashMap(java.util.HashMap) Test(org.junit.Test) Indexed(org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed) IdentifierBridge(org.hibernate.search.mapper.pojo.bridge.IdentifierBridge) BeanReference(org.hibernate.search.engine.environment.bean.BeanReference) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) Rule(org.junit.Rule) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) IdentifierBridgeFromDocumentIdentifierContext(org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeFromDocumentIdentifierContext) Map(java.util.Map) StubBackendUtils(org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) IdentifierBridgeToDocumentIdentifierContext(org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeToDocumentIdentifierContext) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) StubSearchWorkBehavior(org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior) Collections(java.util.Collections) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Indexed(org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed) Test(org.junit.Test)

Aggregations

EntityReference (org.hibernate.search.mapper.javabean.common.EntityReference)11 SearchSession (org.hibernate.search.mapper.javabean.session.SearchSession)11 Test (org.junit.Test)11 MethodHandles (java.lang.invoke.MethodHandles)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 SearchQuery (org.hibernate.search.engine.search.query.SearchQuery)9 JavaBeanMappingSetupHelper (org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper)9 EntityReferenceImpl (org.hibernate.search.mapper.javabean.common.impl.EntityReferenceImpl)9 SearchMapping (org.hibernate.search.mapper.javabean.mapping.SearchMapping)9 BackendMock (org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock)9 StubSearchWorkBehavior (org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior)9 Rule (org.junit.Rule)9 Before (org.junit.Before)6 Collections (java.util.Collections)5 List (java.util.List)5 Map (java.util.Map)5 Indexed (org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed)5 SearchException (org.hibernate.search.util.common.SearchException)5 StubBackendUtils (org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils)5 LocalDate (java.time.LocalDate)4