Skip to content

Commit f4caadd

Browse files
committed
MappedFieldType no longer requires equals/hashCode/clone (#59212)
With the removal of mapping types and the immutability of FieldTypeLookup in #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.
1 parent 5448339 commit f4caadd

File tree

88 files changed

+71
-1334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+71
-1334
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureFieldMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ public RankFeatureFieldType(String name, Map<String, String> meta, boolean posit
103103
setIndexAnalyzer(Lucene.KEYWORD_ANALYZER);
104104
}
105105

106-
protected RankFeatureFieldType(RankFeatureFieldType ref) {
107-
super(ref);
108-
this.positiveScoreImpact = ref.positiveScoreImpact;
109-
}
110-
111-
public RankFeatureFieldType clone() {
112-
return new RankFeatureFieldType(this);
113-
}
114-
115106
@Override
116107
public String typeName() {
117108
return CONTENT_TYPE;

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,6 @@ private RankFeatureMetaFieldType() {
9292
super(NAME, false, false, TextSearchInfo.NONE, Collections.emptyMap());
9393
}
9494

95-
protected RankFeatureMetaFieldType(RankFeatureMetaFieldType ref) {
96-
super(ref);
97-
}
98-
99-
@Override
100-
public RankFeatureMetaFieldType clone() {
101-
return new RankFeatureMetaFieldType(this);
102-
}
103-
10495
@Override
10596
public String typeName() {
10697
return CONTENT_TYPE;

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ public RankFeaturesFieldType(String name, Map<String, String> meta) {
8080
setIndexAnalyzer(Lucene.KEYWORD_ANALYZER);
8181
}
8282

83-
protected RankFeaturesFieldType(RankFeaturesFieldType ref) {
84-
super(ref);
85-
}
86-
87-
public RankFeaturesFieldType clone() {
88-
return new RankFeaturesFieldType(this);
89-
}
90-
9183
@Override
9284
public String typeName() {
9385
return CONTENT_TYPE;

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,10 @@ public ScaledFloatFieldType(String name, double scalingFactor) {
193193
this(name, true, true, Collections.emptyMap(), scalingFactor);
194194
}
195195

196-
ScaledFloatFieldType(ScaledFloatFieldType other) {
197-
super(other);
198-
this.scalingFactor = other.scalingFactor;
199-
}
200-
201196
public double getScalingFactor() {
202197
return scalingFactor;
203198
}
204199

205-
@Override
206-
public MappedFieldType clone() {
207-
return new ScaledFloatFieldType(this);
208-
}
209-
210200
@Override
211201
public String typeName() {
212202
return CONTENT_TYPE;
@@ -310,19 +300,6 @@ public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
310300
}
311301
}
312302

313-
@Override
314-
public boolean equals(Object o) {
315-
if (super.equals(o) == false) {
316-
return false;
317-
}
318-
return scalingFactor == ((ScaledFloatFieldType) o).scalingFactor;
319-
}
320-
321-
@Override
322-
public int hashCode() {
323-
return 31 * super.hashCode() + Double.hashCode(scalingFactor);
324-
}
325-
326303
/**
327304
* Parses input value and multiplies it with the scaling factor.
328305
* Uses the round-trip of creating a {@link BigDecimal} from the stringified {@code double}

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,6 @@ static class SearchAsYouTypeFieldType extends StringFieldType {
252252
new TextSearchInfo(fieldType, similarity, searchAnalyzer, searchQuoteAnalyzer), meta);
253253
}
254254

255-
SearchAsYouTypeFieldType(SearchAsYouTypeFieldType other) {
256-
super(other);
257-
258-
if (other.prefixField != null) {
259-
this.prefixField = other.prefixField.clone();
260-
}
261-
if (other.shingleFields != null) {
262-
this.shingleFields = new ShingleFieldType[other.shingleFields.length];
263-
for (int i = 0; i < this.shingleFields.length; i++) {
264-
if (other.shingleFields[i] != null) {
265-
this.shingleFields[i] = other.shingleFields[i].clone();
266-
}
267-
}
268-
}
269-
}
270-
271255
public void setPrefixField(PrefixFieldType prefixField) {
272256
this.prefixField = prefixField;
273257
}
@@ -276,11 +260,6 @@ public void setShingleFields(ShingleFieldType[] shingleFields) {
276260
this.shingleFields = shingleFields;
277261
}
278262

279-
@Override
280-
public MappedFieldType clone() {
281-
return new SearchAsYouTypeFieldType(this);
282-
}
283-
284263
@Override
285264
public String typeName() {
286265
return CONTENT_TYPE;
@@ -361,27 +340,6 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
361340
return spanMulti;
362341
}
363342
}
364-
365-
@Override
366-
public boolean equals(Object otherObject) {
367-
if (this == otherObject) {
368-
return true;
369-
}
370-
if (otherObject == null || getClass() != otherObject.getClass()) {
371-
return false;
372-
}
373-
if (!super.equals(otherObject)) {
374-
return false;
375-
}
376-
final SearchAsYouTypeFieldType other = (SearchAsYouTypeFieldType) otherObject;
377-
return Objects.equals(prefixField, other.prefixField) &&
378-
Arrays.equals(shingleFields, other.shingleFields);
379-
}
380-
381-
@Override
382-
public int hashCode() {
383-
return Objects.hash(super.hashCode(), prefixField, Arrays.hashCode(shingleFields));
384-
}
385343
}
386344

387345
/**
@@ -401,13 +359,6 @@ static final class PrefixFieldType extends StringFieldType {
401359
this.parentField = parentField;
402360
}
403361

404-
PrefixFieldType(PrefixFieldType other) {
405-
super(other);
406-
this.minChars = other.minChars;
407-
this.maxChars = other.maxChars;
408-
this.parentField = other.parentField;
409-
}
410-
411362
boolean termLengthWithinBounds(int length) {
412363
return length >= minChars - 1 && length <= maxChars;
413364
}
@@ -431,11 +382,6 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, Quer
431382
.build();
432383
}
433384

434-
@Override
435-
public PrefixFieldType clone() {
436-
return new PrefixFieldType(this);
437-
}
438-
439385
@Override
440386
public String typeName() {
441387
return "prefix";
@@ -450,27 +396,6 @@ public String toString() {
450396
public Query existsQuery(QueryShardContext context) {
451397
throw new UnsupportedOperationException();
452398
}
453-
454-
@Override
455-
public boolean equals(Object o) {
456-
if (this == o) {
457-
return true;
458-
}
459-
if (o == null || getClass() != o.getClass()) {
460-
return false;
461-
}
462-
if (!super.equals(o)) {
463-
return false;
464-
}
465-
PrefixFieldType that = (PrefixFieldType) o;
466-
return minChars == that.minChars &&
467-
maxChars == that.maxChars;
468-
}
469-
470-
@Override
471-
public int hashCode() {
472-
return Objects.hash(super.hashCode(), minChars, maxChars);
473-
}
474399
}
475400

476401
static final class PrefixFieldMapper extends FieldMapper {
@@ -552,23 +477,10 @@ static class ShingleFieldType extends StringFieldType {
552477
this.shingleSize = shingleSize;
553478
}
554479

555-
ShingleFieldType(ShingleFieldType other) {
556-
super(other);
557-
this.shingleSize = other.shingleSize;
558-
if (other.prefixFieldType != null) {
559-
this.prefixFieldType = other.prefixFieldType.clone();
560-
}
561-
}
562-
563480
void setPrefixFieldType(PrefixFieldType prefixFieldType) {
564481
this.prefixFieldType = prefixFieldType;
565482
}
566483

567-
@Override
568-
public ShingleFieldType clone() {
569-
return new ShingleFieldType(this);
570-
}
571-
572484
@Override
573485
public String typeName() {
574486
return CONTENT_TYPE;
@@ -629,27 +541,6 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
629541
return spanMulti;
630542
}
631543
}
632-
633-
@Override
634-
public boolean equals(Object otherObject) {
635-
if (this == otherObject) {
636-
return true;
637-
}
638-
if (otherObject == null || getClass() != otherObject.getClass()) {
639-
return false;
640-
}
641-
if (!super.equals(otherObject)) {
642-
return false;
643-
}
644-
final ShingleFieldType other = (ShingleFieldType) otherObject;
645-
return shingleSize == other.shingleSize
646-
&& Objects.equals(prefixFieldType, other.prefixFieldType);
647-
}
648-
649-
@Override
650-
public int hashCode() {
651-
return Objects.hash(super.hashCode(), shingleSize, prefixFieldType);
652-
}
653544
}
654545

655546
private final int maxShingleSize;

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldTypeTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@
2020
package org.elasticsearch.index.mapper;
2121

2222
import java.util.Collections;
23-
import java.util.Map;
2423

25-
public class RankFeatureFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {
26-
27-
@Override
28-
protected MappedFieldType createDefaultFieldType(String name, Map<String, String> meta) {
29-
return new RankFeatureFieldMapper.RankFeatureFieldType(name, meta, true);
30-
}
24+
public class RankFeatureFieldTypeTests extends FieldTypeTestCase {
3125

3226
public void testIsAggregatable() {
3327
MappedFieldType fieldType = new RankFeatureFieldMapper.RankFeatureFieldType("field", Collections.emptyMap(), true);

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldTypeTests.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,11 @@
2020
package org.elasticsearch.index.mapper;
2121

2222
import java.util.Collections;
23-
import java.util.Map;
2423

25-
public class RankFeaturesFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {
26-
27-
@Override
28-
protected MappedFieldType createDefaultFieldType(String name, Map<String, String> meta) {
29-
return new RankFeaturesFieldMapper.RankFeaturesFieldType(name, meta);
30-
}
24+
public class RankFeaturesFieldTypeTests extends FieldTypeTestCase {
3125

3226
public void testIsAggregatable() {
33-
MappedFieldType fieldType = createDefaultFieldType("field", Collections.emptyMap());
27+
MappedFieldType fieldType = new RankFeaturesFieldMapper.RankFeaturesFieldType("field", Collections.emptyMap());
3428
assertFalse(fieldType.isAggregatable());
3529
}
3630
}

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,9 @@
4040

4141
import java.io.IOException;
4242
import java.util.Arrays;
43-
import java.util.Map;
4443

45-
public class ScaledFloatFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {
44+
public class ScaledFloatFieldTypeTests extends FieldTypeTestCase {
4645

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

5247
public void testTermQuery() {
5348
ScaledFloatFieldMapper.ScaledFloatFieldType ft

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@
3535
import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.ShingleFieldType;
3636

3737
import java.util.Collections;
38-
import java.util.Map;
3938

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

44-
public class SearchAsYouTypeFieldTypeTests extends FieldTypeTestCase<MappedFieldType> {
43+
public class SearchAsYouTypeFieldTypeTests extends FieldTypeTestCase {
4544

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

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

6462
public void testTermQuery() {
65-
final MappedFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap());
63+
final MappedFieldType fieldType = createFieldType();
6664

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

@@ -73,7 +71,7 @@ public void testTermQuery() {
7371
}
7472

7573
public void testTermsQuery() {
76-
final MappedFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap());
74+
final MappedFieldType fieldType = createFieldType();
7775

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

8886
public void testPrefixQuery() {
89-
final SearchAsYouTypeFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap());
87+
final SearchAsYouTypeFieldType fieldType = createFieldType();
9088

9189
// this term should be a length that can be rewriteable to a term query on the prefix field
9290
final String withinBoundsTerm = "foo";

modules/parent-join/src/main/java/org/elasticsearch/join/mapper/MetaJoinFieldMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,6 @@ public static class MetaJoinFieldType extends StringFieldType {
8282
this.joinField = joinField;
8383
}
8484

85-
protected MetaJoinFieldType(MetaJoinFieldType ref) {
86-
super(ref);
87-
this.joinField = ref.joinField;
88-
}
89-
90-
public MetaJoinFieldType clone() {
91-
return new MetaJoinFieldType(this);
92-
}
93-
9485
@Override
9586
public String typeName() {
9687
return CONTENT_TYPE;

0 commit comments

Comments
 (0)