Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ private static double objectToDouble(Object value) {

public static final class NumberFieldType extends SimpleMappedFieldType {

NumberType type;
private final NumberType type;

public NumberFieldType(NumberType type) {
super();
Expand All @@ -856,7 +856,7 @@ public NumberFieldType(NumberType type) {
setOmitNorms(true);
}

NumberFieldType(NumberFieldType other) {
private NumberFieldType(NumberFieldType other) {
super(other);
this.type = other.type;
}
Expand Down Expand Up @@ -936,6 +936,20 @@ public DocValueFormat docValueFormat(String format, DateTimeZone timeZone) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.index.mapper;

import com.carrotsearch.randomizedtesting.generators.RandomPicks;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.HalfFloatPoint;
Expand All @@ -37,10 +36,11 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.core.internal.io.IOUtils;
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.NumberFieldMapper.NumberType;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType;
import org.hamcrest.Matchers;
import org.junit.Before;

Expand Down Expand Up @@ -68,6 +68,17 @@ protected MappedFieldType createDefaultFieldType() {
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted, this could be tested with EqualsHashCodeTestUtils.checkEqualsAndHashCode for testing the methods and mutation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. I opted not to change this here as it made the test a bit messier, and the parent class already does more rigorous equality testing (FieldTypeTestCase#testEquals).

}

public void testIsFieldWithinQuery() throws IOException {
MappedFieldType ft = createDefaultFieldType();
// current impl ignores args and should always return INTERSECTS
Expand Down