Skip to content

Commit 04281f9

Browse files
committed
HSEARCH-5334 Better handle advancing to the same parent doc
1 parent a8d7974 commit 04281f9

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/docvalues/impl/DoubleMultiValuesToSingleValuesSource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ protected NumericDoubleValues select(SortedNumericDoubleDocValues values, ChildD
138138

139139
int lastSeenParentDoc = -1;
140140
double lastEmittedValue = -1;
141+
boolean result = false;
141142

142143
@Override
143144
public double doubleValue() {
@@ -148,16 +149,18 @@ public double doubleValue() {
148149
public boolean advanceExact(int parentDoc) throws IOException {
149150
assert parentDoc >= lastSeenParentDoc : "can only evaluate current and upcoming parent docs";
150151
if ( parentDoc == lastSeenParentDoc ) {
151-
return true;
152+
return result;
152153
}
153154

155+
lastSeenParentDoc = parentDoc;
154156
if ( !childDocsWithValues.advanceExactParent( parentDoc ) ) {
155157
// No child of this parent has a value
158+
result = false;
156159
return false;
157160
}
158161

159-
lastSeenParentDoc = parentDoc;
160162
lastEmittedValue = mode.pick( values, childDocsWithValues );
163+
result = true;
161164
return true;
162165
}
163166

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/docvalues/impl/LongMultiValuesToSingleValuesSource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ protected LongValues select(SortedNumericDocValues values, ChildDocIds childDocs
160160
return new LongValues() {
161161
int lastSeenParentDoc = -1;
162162
long lastEmittedValue = -1;
163+
boolean result = false;
163164

164165
@Override
165166
public long longValue() {
@@ -170,16 +171,18 @@ public long longValue() {
170171
public boolean advanceExact(int parentDoc) throws IOException {
171172
assert parentDoc >= lastSeenParentDoc : "can only evaluate current and upcoming parent docs";
172173
if ( parentDoc == lastSeenParentDoc ) {
173-
return true;
174+
return result;
174175
}
175176

177+
lastSeenParentDoc = parentDoc;
176178
if ( !childDocsWithValues.advanceExactParent( parentDoc ) ) {
177179
// No child of this parent has a value
180+
result = false;
178181
return false;
179182
}
180183

181-
lastSeenParentDoc = parentDoc;
182184
lastEmittedValue = mode.pick( values, childDocsWithValues );
185+
result = true;
183186
return true;
184187
}
185188
};

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/docvalues/impl/TextMultiValuesToSingleValuesSource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ protected SortedDocValues select(SortedSetDocValues values, ChildDocIds childDoc
120120
return new SortedSetDocValuesToSortedDocValuesWrapper( values ) {
121121
int lastSeenParentDoc = -1;
122122
int lastEmittedOrd = -1;
123+
boolean result = false;
123124

124125
@Override
125126
public int ordValue() {
@@ -135,16 +136,18 @@ public int docID() {
135136
public boolean advanceExact(int parentDoc) throws IOException {
136137
assert parentDoc >= lastSeenParentDoc : "can only evaluate current and upcoming parent docs";
137138
if ( parentDoc == lastSeenParentDoc ) {
138-
return true;
139+
return result;
139140
}
141+
lastSeenParentDoc = parentDoc;
140142

141143
if ( !childDocsWithValues.advanceExactParent( parentDoc ) ) {
142144
// No child of this parent has a value
145+
result = false;
143146
return false;
144147
}
145148

146-
lastSeenParentDoc = parentDoc;
147149
lastEmittedOrd = (int) mode.pick( values, childDocsWithValues );
150+
result = true;
148151
return true;
149152
}
150153
};

integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/search/sort/FieldSortBaseIT.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1010
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits;
1111
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery;
12+
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult;
1213
import static org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMapperUtils.documentProvider;
1314
import static org.junit.Assume.assumeFalse;
1415
import static org.junit.Assume.assumeTrue;
@@ -528,6 +529,22 @@ public void missingValue_multipleOptionsSameTime() {
528529
dataSet.emptyDoc1Id );
529530
}
530531

532+
@Test
533+
public void missingValue_singleElement() {
534+
assumeTestParametersWork();
535+
536+
DataSet<F> dataSet;
537+
SearchQuery<DocumentReference> query;
538+
539+
String fieldPath = getFieldPath();
540+
541+
// Explicit order with missing().last()
542+
dataSet = dataSetForAsc;
543+
query = matchNonEmptyAndEmpty1Query( dataSet, f -> f.field( fieldPath ).asc().missing().lowest() );
544+
assertThatResult( query.fetch( 0, 1 ) )
545+
.hasDocRefHitsExactOrder( index.typeName(), dataSet.emptyDoc1Id );
546+
}
547+
531548
private SearchQuery<DocumentReference> matchNonEmptyQuery(DataSet<F> dataSet,
532549
Function<? super SearchSortFactory, ? extends FieldSortOptionsStep<?, ?>> sortContributor) {
533550
return matchNonEmptyQuery( dataSet, sortContributor, index.createScope() );

0 commit comments

Comments
 (0)