Skip to content

Commit

Permalink
MappedFieldType no longer requires equals/hashCode/clone (elastic#59212)
Browse files Browse the repository at this point in the history
With the removal of mapping types and the immutability of FieldTypeLookup in elastic#58162, we no longer
have any cause to compare MappedFieldType instances. This means that we can remove all equals
and hashCode implementations, and in addition we no longer need the clone implementations which
were required for equals/hashcode testing. This greatly simplifies implementing new MappedFieldTypes,
which will be particularly useful for the runtime fields project.
  • Loading branch information
romseygeek committed Jul 9, 2020
1 parent 5448339 commit f4caadd
Show file tree
Hide file tree
Showing 88 changed files with 71 additions and 1,334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,6 @@ public RankFeatureFieldType(String name, Map<String, String> meta, boolean posit
setIndexAnalyzer(Lucene.KEYWORD_ANALYZER);
}

protected RankFeatureFieldType(RankFeatureFieldType ref) {
super(ref);
this.positiveScoreImpact = ref.positiveScoreImpact;
}

public RankFeatureFieldType clone() {
return new RankFeatureFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@ private RankFeatureMetaFieldType() {
super(NAME, false, false, TextSearchInfo.NONE, Collections.emptyMap());
}

protected RankFeatureMetaFieldType(RankFeatureMetaFieldType ref) {
super(ref);
}

@Override
public RankFeatureMetaFieldType clone() {
return new RankFeatureMetaFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ public RankFeaturesFieldType(String name, Map<String, String> meta) {
setIndexAnalyzer(Lucene.KEYWORD_ANALYZER);
}

protected RankFeaturesFieldType(RankFeaturesFieldType ref) {
super(ref);
}

public RankFeaturesFieldType clone() {
return new RankFeaturesFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,10 @@ public ScaledFloatFieldType(String name, double scalingFactor) {
this(name, true, true, Collections.emptyMap(), scalingFactor);
}

ScaledFloatFieldType(ScaledFloatFieldType other) {
super(other);
this.scalingFactor = other.scalingFactor;
}

public double getScalingFactor() {
return scalingFactor;
}

@Override
public MappedFieldType clone() {
return new ScaledFloatFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down Expand Up @@ -310,19 +300,6 @@ public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
}
}

@Override
public boolean equals(Object o) {
if (super.equals(o) == false) {
return false;
}
return scalingFactor == ((ScaledFloatFieldType) o).scalingFactor;
}

@Override
public int hashCode() {
return 31 * super.hashCode() + Double.hashCode(scalingFactor);
}

/**
* Parses input value and multiplies it with the scaling factor.
* Uses the round-trip of creating a {@link BigDecimal} from the stringified {@code double}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,6 @@ static class SearchAsYouTypeFieldType extends StringFieldType {
new TextSearchInfo(fieldType, similarity, searchAnalyzer, searchQuoteAnalyzer), meta);
}

SearchAsYouTypeFieldType(SearchAsYouTypeFieldType other) {
super(other);

if (other.prefixField != null) {
this.prefixField = other.prefixField.clone();
}
if (other.shingleFields != null) {
this.shingleFields = new ShingleFieldType[other.shingleFields.length];
for (int i = 0; i < this.shingleFields.length; i++) {
if (other.shingleFields[i] != null) {
this.shingleFields[i] = other.shingleFields[i].clone();
}
}
}
}

public void setPrefixField(PrefixFieldType prefixField) {
this.prefixField = prefixField;
}
Expand All @@ -276,11 +260,6 @@ public void setShingleFields(ShingleFieldType[] shingleFields) {
this.shingleFields = shingleFields;
}

@Override
public MappedFieldType clone() {
return new SearchAsYouTypeFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down Expand Up @@ -361,27 +340,6 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
return spanMulti;
}
}

@Override
public boolean equals(Object otherObject) {
if (this == otherObject) {
return true;
}
if (otherObject == null || getClass() != otherObject.getClass()) {
return false;
}
if (!super.equals(otherObject)) {
return false;
}
final SearchAsYouTypeFieldType other = (SearchAsYouTypeFieldType) otherObject;
return Objects.equals(prefixField, other.prefixField) &&
Arrays.equals(shingleFields, other.shingleFields);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), prefixField, Arrays.hashCode(shingleFields));
}
}

/**
Expand All @@ -401,13 +359,6 @@ static final class PrefixFieldType extends StringFieldType {
this.parentField = parentField;
}

PrefixFieldType(PrefixFieldType other) {
super(other);
this.minChars = other.minChars;
this.maxChars = other.maxChars;
this.parentField = other.parentField;
}

boolean termLengthWithinBounds(int length) {
return length >= minChars - 1 && length <= maxChars;
}
Expand All @@ -431,11 +382,6 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, Quer
.build();
}

@Override
public PrefixFieldType clone() {
return new PrefixFieldType(this);
}

@Override
public String typeName() {
return "prefix";
Expand All @@ -450,27 +396,6 @@ public String toString() {
public Query existsQuery(QueryShardContext context) {
throw new UnsupportedOperationException();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
PrefixFieldType that = (PrefixFieldType) o;
return minChars == that.minChars &&
maxChars == that.maxChars;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), minChars, maxChars);
}
}

static final class PrefixFieldMapper extends FieldMapper {
Expand Down Expand Up @@ -552,23 +477,10 @@ static class ShingleFieldType extends StringFieldType {
this.shingleSize = shingleSize;
}

ShingleFieldType(ShingleFieldType other) {
super(other);
this.shingleSize = other.shingleSize;
if (other.prefixFieldType != null) {
this.prefixFieldType = other.prefixFieldType.clone();
}
}

void setPrefixFieldType(PrefixFieldType prefixFieldType) {
this.prefixFieldType = prefixFieldType;
}

@Override
public ShingleFieldType clone() {
return new ShingleFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down Expand Up @@ -629,27 +541,6 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
return spanMulti;
}
}

@Override
public boolean equals(Object otherObject) {
if (this == otherObject) {
return true;
}
if (otherObject == null || getClass() != otherObject.getClass()) {
return false;
}
if (!super.equals(otherObject)) {
return false;
}
final ShingleFieldType other = (ShingleFieldType) otherObject;
return shingleSize == other.shingleSize
&& Objects.equals(prefixFieldType, other.prefixFieldType);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), shingleSize, prefixFieldType);
}
}

private final int maxShingleSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@
package org.elasticsearch.index.mapper;

import java.util.Collections;
import java.util.Map;

public class RankFeatureFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {

@Override
protected MappedFieldType createDefaultFieldType(String name, Map<String, String> meta) {
return new RankFeatureFieldMapper.RankFeatureFieldType(name, meta, true);
}
public class RankFeatureFieldTypeTests extends FieldTypeTestCase {

public void testIsAggregatable() {
MappedFieldType fieldType = new RankFeatureFieldMapper.RankFeatureFieldType("field", Collections.emptyMap(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@
package org.elasticsearch.index.mapper;

import java.util.Collections;
import java.util.Map;

public class RankFeaturesFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {

@Override
protected MappedFieldType createDefaultFieldType(String name, Map<String, String> meta) {
return new RankFeaturesFieldMapper.RankFeaturesFieldType(name, meta);
}
public class RankFeaturesFieldTypeTests extends FieldTypeTestCase {

public void testIsAggregatable() {
MappedFieldType fieldType = createDefaultFieldType("field", Collections.emptyMap());
MappedFieldType fieldType = new RankFeaturesFieldMapper.RankFeaturesFieldType("field", Collections.emptyMap());
assertFalse(fieldType.isAggregatable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,9 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

public class ScaledFloatFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {
public class ScaledFloatFieldTypeTests extends FieldTypeTestCase {

@Override
protected MappedFieldType createDefaultFieldType(String name, Map<String, String> meta) {
return new ScaledFloatFieldMapper.ScaledFloatFieldType(name, true, true, meta, 100);
}

public void testTermQuery() {
ScaledFloatFieldMapper.ScaledFloatFieldType ft
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.ShingleFieldType;

import java.util.Collections;
import java.util.Map;

import static java.util.Arrays.asList;
import static org.apache.lucene.search.MultiTermQuery.CONSTANT_SCORE_REWRITE;
import static org.hamcrest.Matchers.equalTo;

public class SearchAsYouTypeFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {
public class SearchAsYouTypeFieldTypeTests extends FieldTypeTestCase {

private static final String NAME = "a_field";
private static final FieldType UNSEARCHABLE = new FieldType();
Expand All @@ -50,10 +49,9 @@ public class SearchAsYouTypeFieldTypeTests extends FieldTypeTestCase<MappedField
UNSEARCHABLE.freeze();
}

@Override
protected SearchAsYouTypeFieldType createDefaultFieldType(String name, Map<String, String> meta) {
final SearchAsYouTypeFieldType fieldType
= new SearchAsYouTypeFieldType(name, Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER, meta);
protected SearchAsYouTypeFieldType createFieldType() {
final SearchAsYouTypeFieldType fieldType = new SearchAsYouTypeFieldType(NAME, Defaults.FIELD_TYPE, null,
Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER, Collections.emptyMap());
fieldType.setPrefixField(new PrefixFieldType(NAME, TextSearchInfo.SIMPLE_MATCH_ONLY, Defaults.MIN_GRAM, Defaults.MAX_GRAM));
fieldType.setShingleFields(new ShingleFieldType[] {
new ShingleFieldType(fieldType.name(), 2, TextSearchInfo.SIMPLE_MATCH_ONLY)
Expand All @@ -62,7 +60,7 @@ protected SearchAsYouTypeFieldType createDefaultFieldType(String name, Map<Strin
}

public void testTermQuery() {
final MappedFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap());
final MappedFieldType fieldType = createFieldType();

assertThat(fieldType.termQuery("foo", null), equalTo(new TermQuery(new Term(NAME, "foo"))));

Expand All @@ -73,7 +71,7 @@ public void testTermQuery() {
}

public void testTermsQuery() {
final MappedFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap());
final MappedFieldType fieldType = createFieldType();

assertThat(fieldType.termsQuery(asList("foo", "bar"), null),
equalTo(new TermInSetQuery(NAME, asList(new BytesRef("foo"), new BytesRef("bar")))));
Expand All @@ -86,7 +84,7 @@ public void testTermsQuery() {
}

public void testPrefixQuery() {
final SearchAsYouTypeFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap());
final SearchAsYouTypeFieldType fieldType = createFieldType();

// this term should be a length that can be rewriteable to a term query on the prefix field
final String withinBoundsTerm = "foo";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ public static class MetaJoinFieldType extends StringFieldType {
this.joinField = joinField;
}

protected MetaJoinFieldType(MetaJoinFieldType ref) {
super(ref);
this.joinField = ref.joinField;
}

public MetaJoinFieldType clone() {
return new MetaJoinFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ public static final class ParentIdFieldType extends StringFieldType {
setEagerGlobalOrdinals(eagerGlobalOrdinals);
}

protected ParentIdFieldType(ParentIdFieldType ref) {
super(ref);
}

public ParentIdFieldType clone() {
return new ParentIdFieldType(this);
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down
Loading

0 comments on commit f4caadd

Please sign in to comment.