Skip to content

Commit f9ba6b7

Browse files
committed
HSEARCH-1786 Avoid replacing the NOT_SET flag in projections when processing metatada recursively to resolve projections
1 parent e48d44c commit f9ba6b7

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

engine/src/main/java/org/hibernate/search/engine/impl/DocumentBuilderHelper.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public static Object[] getDocumentFields(ExtendedSearchIntegrator extendedIntegr
107107
}
108108
}
109109

110-
final TypeMetadata metadata = builderIndexedEntity.getMetadata();
111-
processFieldsForProjection( metadata, fields, result, document, conversionContext );
110+
processFieldsForProjection( builderIndexedEntity, fields, result, document, conversionContext );
112111
return result;
113112
}
114113

@@ -138,7 +137,28 @@ public static void populateResult(String fieldName,
138137
}
139138
}
140139

141-
private static void processFieldsForProjection(TypeMetadata typeMetadata, String[] fields, Object[] result, Document document, ConversionContext contextualBridge) {
140+
private static void processFieldsForProjection(DocumentBuilderIndexedEntity builderIndexedEntity, String[] fields, Object[] result, Document document, ConversionContext conversionContext) {
141+
final TypeMetadata metadata = builderIndexedEntity.getMetadata();
142+
143+
//First try setting each projected field considering mapping metadata to apply (inverse) field bridges:
144+
processMetadataRecursivelyForProjections( metadata, fields, result, document, conversionContext );
145+
146+
//If we still didn't know the value using any bridge, return the raw value or string:
147+
//Important: make sure this happens as last step of projections! See also HSEARCH-1786
148+
for ( int index = 0; index < result.length; index++ ) {
149+
if ( result[index] == NOT_SET ) {
150+
result[index] = null; // make sure we never return NOT_SET
151+
if ( document != null ) {
152+
IndexableField field = document.getField( fields[index] );
153+
if ( field != null ) {
154+
result[index] = extractObjectFromFieldable( field );
155+
}
156+
}
157+
}
158+
}
159+
}
160+
161+
private static void processMetadataRecursivelyForProjections(TypeMetadata typeMetadata, String[] fields, Object[] result, Document document, ConversionContext contextualBridge) {
142162
//process base fields
143163
for ( PropertyMetadata propertyMetadata : typeMetadata.getAllPropertyMetadata() ) {
144164
for ( DocumentFieldMetadata fieldMetadata : propertyMetadata.getFieldMetadata() ) {
@@ -170,7 +190,7 @@ private static void processFieldsForProjection(TypeMetadata typeMetadata, String
170190
if ( embeddedTypeMetadata.getEmbeddedContainer() == EmbeddedTypeMetadata.Container.OBJECT ) {
171191
contextualBridge.pushProperty( embeddedTypeMetadata.getEmbeddedFieldName() );
172192
try {
173-
processFieldsForProjection(
193+
processMetadataRecursivelyForProjections(
174194
embeddedTypeMetadata, fields, result, document, contextualBridge
175195
);
176196
}
@@ -195,19 +215,6 @@ private static void processFieldsForProjection(TypeMetadata typeMetadata, String
195215
);
196216
}
197217
}
198-
199-
//If we still didn't know the value using any bridge, return the raw value or string:
200-
for ( int index = 0; index < result.length; index++ ) {
201-
if ( result[index] == NOT_SET ) {
202-
result[index] = null; // make sure we never return NOT_SET
203-
if ( document != null ) {
204-
IndexableField field = document.getField( fields[index] );
205-
if ( field != null ) {
206-
result[index] = extractObjectFromFieldable( field );
207-
}
208-
}
209-
}
210-
}
211218
}
212219

213220
/**

0 commit comments

Comments
 (0)