Skip to content

Commit

Permalink
HSEARCH-4843 Use EntityReference in the stub mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed May 1, 2023
1 parent 9f85f63 commit 635edd6
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.hibernate.search.engine.backend.types.Aggregable;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.engine.common.spi.SearchIntegration;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.search.aggregation.AggregationKey;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void queryContext() {
StubMappingScope scope = mainIndex.createScope();

// Put intermediary contexts into variables to check they have the right type
ElasticsearchSearchQuerySelectStep<DocumentReference, DocumentReference, StubLoadingOptionsStep> context1 =
ElasticsearchSearchQuerySelectStep<EntityReference, DocumentReference, StubLoadingOptionsStep> context1 =
scope.query().extension( ElasticsearchExtension.get() );
ElasticsearchSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> context2 = context1.select(
f -> f.composite()
Expand All @@ -131,7 +132,7 @@ public void queryContext() {
.hasTotalHitCount( 6 );

// Also check (at compile time) the context type for other asXXX() methods, since we need to override each method explicitly
ElasticsearchSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> selectEntityReferenceContext =
ElasticsearchSearchQueryWhereStep<EntityReference, StubLoadingOptionsStep> selectEntityReferenceContext =
scope.query().extension( ElasticsearchExtension.get() ).selectEntityReference();
ElasticsearchSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> selectEntityContext =
scope.query().extension( ElasticsearchExtension.get() ).selectEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.backend.index.IndexManager;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.engine.common.spi.SearchIntegration;
import org.hibernate.search.engine.search.aggregation.AggregationKey;
import org.hibernate.search.engine.search.common.ValueConvert;
Expand Down Expand Up @@ -118,7 +119,7 @@ public void queryContext() {
StubMappingScope scope = mainIndex.createScope();

// Put intermediary contexts into variables to check they have the right type
LuceneSearchQuerySelectStep<DocumentReference, DocumentReference, StubLoadingOptionsStep> context1 =
LuceneSearchQuerySelectStep<EntityReference, DocumentReference, StubLoadingOptionsStep> context1 =
scope.query().extension( LuceneExtension.get() );
LuceneSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> context2 = context1.select(
f -> f.composite()
Expand All @@ -142,7 +143,7 @@ public void queryContext() {
.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 =
LuceneSearchQueryWhereStep<EntityReference, StubLoadingOptionsStep> selectEntityReferenceContext =
scope.query().extension( LuceneExtension.get() ).selectEntityReference();
LuceneSearchQueryWhereStep<DocumentReference, StubLoadingOptionsStep> selectEntityContext =
scope.query().extension( LuceneExtension.get() ).selectEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;

import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.types.Projectable;
Expand Down Expand Up @@ -725,36 +724,16 @@ public void decimalScale_doesNotAffectProjections_bigInteger() {
projection( index, new BigInteger( "73911111" ) );
}

private void matchGreaterThan(StubMappedIndex index, BigDecimal value) {
SearchQuery<DocumentReference> query = index.createScope()
.query().selectEntityReference()
.where( p -> p.range().field( "scaled" ).greaterThan( value ) )
.toQuery();
assertThatQuery( query ).hasDocRefHitsAnyOrder( index.typeName(), "1" );
}

private void matchGreaterThan(StubMappedIndex index, BigInteger value) {
SearchQuery<DocumentReference> query = index.createScope()
.query().selectEntityReference()
.where( p -> p.range().field( "scaled" ).greaterThan( value ) )
.toQuery();
assertThatQuery( query ).hasDocRefHitsAnyOrder( index.typeName(), "1" );
}

public void doNotMatchGreaterThan(StubMappedIndex index, BigDecimal value) {
SearchQuery<DocumentReference> query = index.createScope()
.query().selectEntityReference()
.where( p -> p.range().field( "scaled" ).greaterThan( value ) )
.toQuery();
assertThatQuery( query ).hasNoHits();
private void matchGreaterThan(StubMappedIndex index, Object value) {
assertThatQuery( index.query()
.where( p -> p.range().field( "scaled" ).greaterThan( value ) ) )
.hasDocRefHitsAnyOrder( index.typeName(), "1" );
}

public void doNotMatchGreaterThan(StubMappedIndex index, BigInteger value) {
SearchQuery<DocumentReference> query = index.createScope()
.query().selectEntityReference()
.where( p -> p.range().field( "scaled" ).greaterThan( value ) )
.toQuery();
assertThatQuery( query ).hasNoHits();
public void doNotMatchGreaterThan(StubMappedIndex index, Object value) {
assertThatQuery( index.query()
.where( p -> p.range().field( "scaled" ).greaterThan( value ) ) )
.hasNoHits();
}

public void projection(StubMappedIndex index, BigDecimal value) {
Expand All @@ -773,20 +752,10 @@ public void projection(StubMappedIndex index, BigInteger value) {
assertThatQuery( query ).hasHitsExactOrder( value );
}

private void match(StubMappedIndex index, BigDecimal matching, String match1, String match2) {
SearchQuery<DocumentReference> query = index.createScope()
.query().selectEntityReference()
.where( p -> p.match().field( "scaled" ).matching( matching ) )
.toQuery();
assertThatQuery( query ).hasDocRefHitsAnyOrder( index.typeName(), match1, match2 );
}

private void match(StubMappedIndex index, BigInteger matching, String match1, String match2) {
SearchQuery<DocumentReference> query = index.createScope()
.query().selectEntityReference()
.where( p -> p.match().field( "scaled" ).matching( matching ) )
.toQuery();
assertThatQuery( query ).hasDocRefHitsAnyOrder( index.typeName(), match1, match2 );
private void match(StubMappedIndex index, Object matching, String match1, String match2) {
assertThatQuery( index.query()
.where( p -> p.match().field( "scaled" ).matching( matching ) ) )
.hasDocRefHitsAnyOrder( index.typeName(), match1, match2 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;
import org.hibernate.search.engine.search.query.dsl.SearchQueryWhereStep;
import org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubEntity;
import org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubTransformedReference;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.integrationtest.backend.tck.testsupport.types.AnalyzedStringFieldTypeDescriptor;
import org.hibernate.search.integrationtest.backend.tck.testsupport.types.KeywordStringFieldTypeDescriptor;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.SimpleFieldModel;
Expand Down Expand Up @@ -102,15 +102,15 @@ public void entityLoading() {
StubEntity doc1LoadedEntity = new StubEntity( doc1Reference );
StubEntity doc2LoadedEntity = new StubEntity( doc2Reference );

SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

when( mainTypeContextMock.loadingAvailable() ).thenReturn( true );

mainIndex.mapping().with()
.typeContext( mainIndex.typeName(), mainTypeContextMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
mainIndex.createGenericScope( loadingContextMock );
SearchQuery<StubEntity> query = select( scope.query() )
.where( f -> f.matchAll() )
Expand Down Expand Up @@ -147,15 +147,15 @@ public void entityLoading_timeout() {
StubEntity doc1LoadedEntity = new StubEntity( doc1Reference );
StubEntity doc2LoadedEntity = new StubEntity( doc2Reference );

SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

when( mainTypeContextMock.loadingAvailable() ).thenReturn( true );

mainIndex.mapping().with()
.typeContext( mainIndex.typeName(), mainTypeContextMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
mainIndex.createGenericScope( loadingContextMock );
SearchQuery<StubEntity> query = select( scope.query() )
.where( f -> f.matchAll() )
Expand Down Expand Up @@ -252,15 +252,15 @@ public void entityLoading_failed_skipHit() {
DocumentReference doc2Reference = reference( mainIndex.typeName(), DOCUMENT_2_ID );
StubEntity doc2LoadedObject = new StubEntity( doc2Reference );

SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

when( mainTypeContextMock.loadingAvailable() ).thenReturn( true );

mainIndex.mapping().with()
.typeContext( mainIndex.typeName(), mainTypeContextMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
mainIndex.createGenericScope( loadingContextMock );
SearchQuery<StubEntity> query = select( scope.query() )
.where( f -> f.matchAll() )
Expand Down Expand Up @@ -329,7 +329,7 @@ public void projectionRegistryFallback_withLoadingAvailable_doesNotCheckProjecti
StubEntity doc2LoadedEntity = new StubEntity( doc2Reference );

ProjectionRegistry projectionRegistryMock = Mockito.mock( ProjectionRegistry.class );
SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

when( mainTypeContextMock.loadingAvailable() ).thenReturn( true );
Expand All @@ -338,7 +338,7 @@ public void projectionRegistryFallback_withLoadingAvailable_doesNotCheckProjecti
.typeContext( mainIndex.typeName(), mainTypeContextMock )
.projectionRegistry( projectionRegistryMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
mainIndex.createGenericScope( loadingContextMock );

expectHitMapping(
Expand Down Expand Up @@ -366,7 +366,7 @@ public void projectionRegistryFallback_noLoadingAvailable_withProjectionRegistry
DocumentReference doc2Reference = reference( mainIndex.typeName(), DOCUMENT_2_ID );

ProjectionRegistry projectionRegistryMock = Mockito.mock( ProjectionRegistry.class );
SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

when( mainTypeContextMock.loadingAvailable() ).thenReturn( false );
Expand All @@ -386,15 +386,15 @@ public void projectionRegistryFallback_noLoadingAvailable_withProjectionRegistry
.typeContext( mainIndex.typeName(), mainTypeContextMock )
.projectionRegistry( projectionRegistryMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
mainIndex.createGenericScope( loadingContextMock );

SearchQuery<StubEntity> query = select( scope.query( loadingContextMock ) )
.where( f -> f.matchAll() )
.toQuery();

@SuppressWarnings("unchecked")
ProjectionHitMapper<StubTransformedReference, StubEntity> projectionHitMapperMock =
ProjectionHitMapper<EntityReference, StubEntity> projectionHitMapperMock =
Mockito.mock( ProjectionHitMapper.class );
when( loadingContextMock.createProjectionHitMapper() )
.thenReturn( projectionHitMapperMock );
Expand Down Expand Up @@ -429,7 +429,7 @@ public void projectionRegistryFallback_multiType() {
ProjectionMappedTypeContext type3ContextMock = Mockito.mock( ProjectionMappedTypeContext.class );
ProjectionMappedTypeContext type4ContextMock = Mockito.mock( ProjectionMappedTypeContext.class );
ProjectionRegistry projectionRegistryMock = Mockito.mock( ProjectionRegistry.class );
SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

when( type1ContextMock.name() ).thenReturn( multiIndex1.typeName() );
Expand Down Expand Up @@ -473,7 +473,7 @@ public void projectionRegistryFallback_multiType() {
.typeContext( multiIndex4.typeName(), type4ContextMock )
.projectionRegistry( projectionRegistryMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
multiIndex1.createGenericScope( loadingContextMock, multiIndex2, multiIndex3, multiIndex4 );

SearchQuery<StubEntity> query = select( scope.query( loadingContextMock ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;
import org.hibernate.search.engine.search.query.dsl.SearchQueryWhereStep;
import org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubEntity;
import org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubTransformedReference;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.BulkIndexer;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.GenericStubMappingScope;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -58,46 +57,31 @@ protected AbstractEntityReferenceProjectionIT(StubMappedIndex mainIndex) {
SearchQuerySelectStep<?, R, E, LOS, ?, ?> step);

@Test
public void noReferenceTransformer() {
StubMappingScope scope = mainIndex.createScope();

SearchQuery<DocumentReference> query = select( scope.query() )
.where( f -> f.matchAll() )
.toQuery();
assertThatQuery( query )
.hasDocRefHitsAnyOrder( mainIndex.typeName(), DOCUMENT_1_ID, DOCUMENT_2_ID );

// check the same for the scroll API
assertThatHits( hitsUsingScroll( query ) )
.hasDocRefHitsAnyOrder( mainIndex.typeName(), DOCUMENT_1_ID, DOCUMENT_2_ID );
}

@Test
public void referenceTransformer() {
public void test() {
DocumentReference doc1Reference = reference( mainIndex.typeName(), DOCUMENT_1_ID );
DocumentReference doc2Reference = reference( mainIndex.typeName(), DOCUMENT_2_ID );
StubTransformedReference doc1TransformedReference = new StubTransformedReference( doc1Reference );
StubTransformedReference doc2TransformedReference = new StubTransformedReference( doc2Reference );
EntityReference doc1EntityReference = StubEntity.reference( doc1Reference );
EntityReference doc2EntityReference = StubEntity.reference( doc2Reference );

SearchLoadingContext<StubTransformedReference, StubEntity> loadingContextMock =
SearchLoadingContext<EntityReference, StubEntity> loadingContextMock =
mock( SearchLoadingContext.class );

mainIndex.mapping().with()
.typeContext( mainIndex.typeName(), mainTypeContextMock )
.run( () -> {
GenericStubMappingScope<StubTransformedReference, StubEntity> scope =
GenericStubMappingScope<EntityReference, StubEntity> scope =
mainIndex.createGenericScope( loadingContextMock );
SearchQuery<StubTransformedReference> referencesQuery = select( scope.query() )
SearchQuery<EntityReference> referencesQuery = select( scope.query() )
.where( f -> f.matchAll() )
.toQuery();

expectHitMapping(
loadingContextMock,
c -> c
.entityReference( doc1Reference, doc1TransformedReference )
.entityReference( doc2Reference, doc2TransformedReference )
.entityReference( doc1Reference, doc1EntityReference )
.entityReference( doc2Reference, doc2EntityReference )
);
assertThatQuery( referencesQuery ).hasHitsAnyOrder( doc1TransformedReference, doc2TransformedReference );
assertThatQuery( referencesQuery ).hasHitsAnyOrder( doc1EntityReference, doc2EntityReference );
// Check in particular that the backend gets the projection hit mapper from the loading context,
// which must happen every time we execute the query,
// so that the mapper can run state checks (session is still open, ...).
Expand All @@ -107,10 +91,10 @@ public void referenceTransformer() {
expectHitMapping(
loadingContextMock,
c -> c
.entityReference( doc1Reference, doc1TransformedReference )
.entityReference( doc2Reference, doc2TransformedReference )
.entityReference( doc1Reference, doc1EntityReference )
.entityReference( doc2Reference, doc2EntityReference )
);
assertThatHits( hitsUsingScroll( referencesQuery ) ).hasHitsAnyOrder( doc1TransformedReference, doc2TransformedReference );
assertThatHits( hitsUsingScroll( referencesQuery ) ).hasHitsAnyOrder( doc1EntityReference, doc2EntityReference );
verify( loadingContextMock ).createProjectionHitMapper();
} );
}
Expand Down

0 comments on commit 635edd6

Please sign in to comment.