Fix cacheability of custom LongValuesSource in TermsSetQueryBuilder (#65367) (#65389)

This change fixes the equals and hashCode methods of the custom FieldValuesSource
that is used internally to extract the value from a doc value field.
Using the field data instance to check equality prevented the query to be cached in
previous versions. Switching to the field name should make the query eligible for
caching again.
This commit is contained in:
Jim Ferenczi 2020-11-23 22:21:01 +01:00 committed by GitHub
parent 1a13a0b10f
commit 359b89a19b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 12 deletions

View File

@ -346,10 +346,12 @@ public final class TermsSetQueryBuilder extends AbstractQueryBuilder<TermsSetQue
// doc values, because that is what is being used in NumberFieldMapper.
static class FieldValuesSource extends LongValuesSource {
private final IndexNumericFieldData field;
private final String fieldName;
private final IndexNumericFieldData fieldData;
FieldValuesSource(IndexNumericFieldData field) {
this.field = field;
FieldValuesSource(IndexNumericFieldData fieldData) {
this.fieldData = fieldData;
this.fieldName = fieldData.getFieldName();
}
@Override
@ -357,22 +359,22 @@ public final class TermsSetQueryBuilder extends AbstractQueryBuilder<TermsSetQue
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FieldValuesSource that = (FieldValuesSource) o;
return Objects.equals(field, that.field);
return Objects.equals(fieldName, that.fieldName);
}
@Override
public String toString() {
return "long(" + field + ")";
return "long(" + fieldName + ")";
}
@Override
public int hashCode() {
return Objects.hash(field);
return Objects.hash(fieldName);
}
@Override
public LongValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
SortedNumericDocValues values = field.load(ctx).getLongValues();
SortedNumericDocValues values = fieldData.load(ctx).getLongValues();
return new LongValues() {
long current = -1;

View File

@ -148,10 +148,6 @@ public class TermsSetQueryBuilderTests extends AbstractQueryTestCase<TermsSetQue
assertFalse("query should be cacheable: " + queryBuilder.toString(), context.isCacheable());
}
@Override
protected boolean builderGeneratesCacheableQueries() {
return false;
}
@Override
public TermsSetQueryBuilder mutateInstance(final TermsSetQueryBuilder instance) throws IOException {

View File

@ -47,6 +47,11 @@ public class WrapperQueryBuilderTests extends AbstractQueryTestCase<WrapperQuery
return false;
}
@Override
protected boolean builderGeneratesCacheableQueries() {
return false;
}
@Override
protected WrapperQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder wrappedQuery = RandomQueryBuilder.createQuery(random());

View File

@ -450,8 +450,10 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
assertLuceneQuery(secondQuery, secondLuceneQuery, context);
if (builderGeneratesCacheableQueries()) {
assertEquals("two equivalent query builders lead to different lucene queries hashcode",
secondLuceneQuery.hashCode(), firstLuceneQuery.hashCode());
assertEquals("two equivalent query builders lead to different lucene queries",
rewrite(secondLuceneQuery), rewrite(firstLuceneQuery));
rewrite(secondLuceneQuery), rewrite(firstLuceneQuery));
}
if (supportsBoost() && firstLuceneQuery instanceof MatchNoDocsQuery == false) {