In NumberFieldType equals and hashCode, make sure that NumberType is taken into account. (#31514)

This commit is contained in:
Julie Tibshirani 2018-06-21 16:53:20 -07:00 committed by GitHub
parent 99f503e3be
commit 3b7225e9d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -846,7 +846,7 @@ public class NumberFieldMapper extends FieldMapper {
public static final class NumberFieldType extends SimpleMappedFieldType { public static final class NumberFieldType extends SimpleMappedFieldType {
NumberType type; private final NumberType type;
public NumberFieldType(NumberType type) { public NumberFieldType(NumberType type) {
super(); super();
@ -856,7 +856,7 @@ public class NumberFieldMapper extends FieldMapper {
setOmitNorms(true); setOmitNorms(true);
} }
NumberFieldType(NumberFieldType other) { private NumberFieldType(NumberFieldType other) {
super(other); super(other);
this.type = other.type; this.type = other.type;
} }
@ -936,6 +936,20 @@ public class NumberFieldMapper extends FieldMapper {
return new DocValueFormat.Decimal(format); return new DocValueFormat.Decimal(format);
} }
} }
@Override
public boolean equals(Object o) {
if (super.equals(o) == false) {
return false;
}
NumberFieldType that = (NumberFieldType) o;
return type == that.type;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), type);
}
} }
private Explicit<Boolean> ignoreMalformed; private Explicit<Boolean> ignoreMalformed;

View File

@ -20,7 +20,6 @@
package org.elasticsearch.index.mapper; package org.elasticsearch.index.mapper;
import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.FloatPoint; import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.HalfFloatPoint; import org.apache.lucene.document.HalfFloatPoint;
@ -37,10 +36,11 @@ import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.core.internal.io.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.index.mapper.MappedFieldType.Relation; import org.elasticsearch.index.mapper.MappedFieldType.Relation;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Before; import org.junit.Before;
@ -68,6 +68,17 @@ public class NumberFieldTypeTests extends FieldTypeTestCase {
return new NumberFieldMapper.NumberFieldType(type); return new NumberFieldMapper.NumberFieldType(type);
} }
public void testEqualsWithDifferentNumberTypes() {
NumberType type = randomFrom(NumberType.values());
NumberFieldType fieldType = new NumberFieldType(type);
NumberType otherType = randomValueOtherThan(type,
() -> randomFrom(NumberType.values()));
NumberFieldType otherFieldType = new NumberFieldType(otherType);
assertNotEquals(fieldType, otherFieldType);
}
public void testIsFieldWithinQuery() throws IOException { public void testIsFieldWithinQuery() throws IOException {
MappedFieldType ft = createDefaultFieldType(); MappedFieldType ft = createDefaultFieldType();
// current impl ignores args and should always return INTERSECTS // current impl ignores args and should always return INTERSECTS