Skip to content

Commit 67905c8

Browse files
authored
Rename index_prefix to index_prefixes (elastic#30932)
This commit also adds index_prefixes tests to TextFieldMapperTests to ensure that cloning and wire-serialization work correctly
1 parent a0af0e7 commit 67905c8

File tree

5 files changed

+63
-26
lines changed

5 files changed

+63
-26
lines changed

docs/reference/mapping/types/text.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The following parameters are accepted by `text` fields:
8989
What information should be stored in the index, for search and highlighting purposes.
9090
Defaults to `positions`.
9191

92-
<<index-prefix-config,`index_prefix`>>::
92+
<<index-prefix-config,`index_prefixes`>>::
9393

9494
If enabled, term prefixes of between 2 and 5 characters are indexed into a
9595
separate field. This allows prefix searches to run more efficiently, at
@@ -138,7 +138,7 @@ The following parameters are accepted by `text` fields:
138138
[[index-prefix-config]]
139139
==== Index Prefix configuration
140140

141-
Text fields may also index term prefixes to speed up prefix searches. The `index_prefix`
141+
Text fields may also index term prefixes to speed up prefix searches. The `index_prefixes`
142142
parameter is configured as below. Either or both of `min_chars` and `max_chars` may be excluded.
143143
Both values are treated as inclusive
144144

@@ -151,7 +151,7 @@ PUT my_index
151151
"properties": {
152152
"full_name": {
153153
"type": "text",
154-
"index_prefix" : {
154+
"index_prefixes" : {
155155
"min_chars" : 1, <1>
156156
"max_chars" : 10 <2>
157157
}

rest-api-spec/src/main/resources/rest-api-spec/test/search/190_index_prefix_search.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"search with index prefixes":
33
- skip:
44
version: " - 6.99.99"
5-
reason: index_prefix is only available as of 6.3.0
5+
reason: index_prefixes is only available as of 6.3.0
66
- do:
77
indices.create:
88
index: test
@@ -12,7 +12,7 @@
1212
properties:
1313
text:
1414
type: text
15-
index_prefix:
15+
index_prefixes:
1616
min_chars: 1
1717
max_chars: 10
1818

server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public TextFieldMapper build(BuilderContext context) {
156156
PrefixFieldMapper prefixMapper = null;
157157
if (prefixFieldType != null) {
158158
if (fieldType().isSearchable() == false) {
159-
throw new IllegalArgumentException("Cannot set index_prefix on unindexed field [" + name() + "]");
159+
throw new IllegalArgumentException("Cannot set index_prefixes on unindexed field [" + name() + "]");
160160
}
161161
if (fieldType.indexOptions() == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) {
162162
prefixFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
@@ -203,7 +203,7 @@ public Mapper.Builder parse(String fieldName, Map<String, Object> node, ParserCo
203203
builder.fielddataFrequencyFilter(minFrequency, maxFrequency, minSegmentSize);
204204
DocumentMapperParser.checkNoRemainingFields(propName, frequencyFilter, parserContext.indexVersionCreated());
205205
iterator.remove();
206-
} else if (propName.equals("index_prefix")) {
206+
} else if (propName.equals("index_prefixes")) {
207207
Map<?, ?> indexPrefix = (Map<?, ?>) propNode;
208208
int minChars = XContentMapValues.nodeIntegerValue(indexPrefix.remove("min_chars"),
209209
Defaults.INDEX_PREFIX_MIN_CHARS);
@@ -243,7 +243,7 @@ protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComp
243243
}
244244
}
245245

246-
private static final class PrefixFieldType extends StringFieldType {
246+
static final class PrefixFieldType extends StringFieldType {
247247

248248
final int minChars;
249249
final int maxChars;
@@ -268,14 +268,14 @@ boolean accept(int length) {
268268
}
269269

270270
void doXContent(XContentBuilder builder) throws IOException {
271-
builder.startObject("index_prefix");
271+
builder.startObject("index_prefixes");
272272
builder.field("min_chars", minChars);
273273
builder.field("max_chars", maxChars);
274274
builder.endObject();
275275
}
276276

277277
@Override
278-
public MappedFieldType clone() {
278+
public PrefixFieldType clone() {
279279
return new PrefixFieldType(name(), minChars, maxChars);
280280
}
281281

@@ -305,6 +305,22 @@ public void checkCompatibility(MappedFieldType other, List<String> conflicts) {
305305
public Query existsQuery(QueryShardContext context) {
306306
throw new UnsupportedOperationException();
307307
}
308+
309+
@Override
310+
public boolean equals(Object o) {
311+
if (this == o) return true;
312+
if (o == null || getClass() != o.getClass()) return false;
313+
if (!super.equals(o)) return false;
314+
PrefixFieldType that = (PrefixFieldType) o;
315+
return minChars == that.minChars &&
316+
maxChars == that.maxChars;
317+
}
318+
319+
@Override
320+
public int hashCode() {
321+
322+
return Objects.hash(super.hashCode(), minChars, maxChars);
323+
}
308324
}
309325

310326
private static final class PrefixFieldMapper extends FieldMapper {
@@ -355,6 +371,9 @@ protected TextFieldType(TextFieldType ref) {
355371
this.fielddataMinFrequency = ref.fielddataMinFrequency;
356372
this.fielddataMaxFrequency = ref.fielddataMaxFrequency;
357373
this.fielddataMinSegmentSize = ref.fielddataMinSegmentSize;
374+
if (ref.prefixFieldType != null) {
375+
this.prefixFieldType = ref.prefixFieldType.clone();
376+
}
358377
}
359378

360379
public TextFieldType clone() {
@@ -368,14 +387,15 @@ public boolean equals(Object o) {
368387
}
369388
TextFieldType that = (TextFieldType) o;
370389
return fielddata == that.fielddata
390+
&& Objects.equals(prefixFieldType, that.prefixFieldType)
371391
&& fielddataMinFrequency == that.fielddataMinFrequency
372392
&& fielddataMaxFrequency == that.fielddataMaxFrequency
373393
&& fielddataMinSegmentSize == that.fielddataMinSegmentSize;
374394
}
375395

376396
@Override
377397
public int hashCode() {
378-
return Objects.hash(super.hashCode(), fielddata,
398+
return Objects.hash(super.hashCode(), fielddata, prefixFieldType,
379399
fielddataMinFrequency, fielddataMaxFrequency, fielddataMinSegmentSize);
380400
}
381401

@@ -420,6 +440,10 @@ void setPrefixFieldType(PrefixFieldType prefixFieldType) {
420440
this.prefixFieldType = prefixFieldType;
421441
}
422442

443+
public PrefixFieldType getPrefixFieldType() {
444+
return this.prefixFieldType;
445+
}
446+
423447
@Override
424448
public String typeName() {
425449
return CONTENT_TYPE;

server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public void testIndexPrefixIndexTypes() throws IOException {
607607
.startObject("properties").startObject("field")
608608
.field("type", "text")
609609
.field("analyzer", "english")
610-
.startObject("index_prefix").endObject()
610+
.startObject("index_prefixes").endObject()
611611
.field("index_options", "offsets")
612612
.endObject().endObject().endObject().endObject());
613613

@@ -623,7 +623,7 @@ public void testIndexPrefixIndexTypes() throws IOException {
623623
.startObject("properties").startObject("field")
624624
.field("type", "text")
625625
.field("analyzer", "english")
626-
.startObject("index_prefix").endObject()
626+
.startObject("index_prefixes").endObject()
627627
.field("index_options", "positions")
628628
.endObject().endObject().endObject().endObject());
629629

@@ -640,7 +640,7 @@ public void testIndexPrefixIndexTypes() throws IOException {
640640
.startObject("properties").startObject("field")
641641
.field("type", "text")
642642
.field("analyzer", "english")
643-
.startObject("index_prefix").endObject()
643+
.startObject("index_prefixes").endObject()
644644
.field("term_vector", "with_positions_offsets")
645645
.endObject().endObject().endObject().endObject());
646646

@@ -657,7 +657,7 @@ public void testIndexPrefixIndexTypes() throws IOException {
657657
.startObject("properties").startObject("field")
658658
.field("type", "text")
659659
.field("analyzer", "english")
660-
.startObject("index_prefix").endObject()
660+
.startObject("index_prefixes").endObject()
661661
.field("term_vector", "with_positions")
662662
.endObject().endObject().endObject().endObject());
663663

@@ -682,7 +682,7 @@ public void testIndexPrefixMapping() throws IOException {
682682
.startObject("properties").startObject("field")
683683
.field("type", "text")
684684
.field("analyzer", "english")
685-
.startObject("index_prefix")
685+
.startObject("index_prefixes")
686686
.field("min_chars", 1)
687687
.field("max_chars", 10)
688688
.endObject()
@@ -716,7 +716,7 @@ public void testIndexPrefixMapping() throws IOException {
716716
.startObject("properties").startObject("field")
717717
.field("type", "text")
718718
.field("analyzer", "english")
719-
.startObject("index_prefix").endObject()
719+
.startObject("index_prefixes").endObject()
720720
.endObject().endObject()
721721
.endObject().endObject());
722722
CompressedXContent json = new CompressedXContent(mapping);
@@ -741,7 +741,7 @@ public void testIndexPrefixMapping() throws IOException {
741741
.startObject("properties").startObject("field")
742742
.field("type", "text")
743743
.field("analyzer", "english")
744-
.startObject("index_prefix")
744+
.startObject("index_prefixes")
745745
.field("min_chars", 1)
746746
.field("max_chars", 10)
747747
.endObject()
@@ -760,7 +760,7 @@ public void testIndexPrefixMapping() throws IOException {
760760
.startObject("properties").startObject("field")
761761
.field("type", "text")
762762
.field("analyzer", "english")
763-
.startObject("index_prefix")
763+
.startObject("index_prefixes")
764764
.field("min_chars", 1)
765765
.field("max_chars", 10)
766766
.endObject()
@@ -783,7 +783,7 @@ public void testIndexPrefixMapping() throws IOException {
783783
.startObject("properties").startObject("field")
784784
.field("type", "text")
785785
.field("analyzer", "english")
786-
.startObject("index_prefix")
786+
.startObject("index_prefixes")
787787
.field("min_chars", 11)
788788
.field("max_chars", 10)
789789
.endObject()
@@ -800,7 +800,7 @@ public void testIndexPrefixMapping() throws IOException {
800800
.startObject("properties").startObject("field")
801801
.field("type", "text")
802802
.field("analyzer", "english")
803-
.startObject("index_prefix")
803+
.startObject("index_prefixes")
804804
.field("min_chars", 0)
805805
.field("max_chars", 10)
806806
.endObject()
@@ -817,7 +817,7 @@ public void testIndexPrefixMapping() throws IOException {
817817
.startObject("properties").startObject("field")
818818
.field("type", "text")
819819
.field("analyzer", "english")
820-
.startObject("index_prefix")
820+
.startObject("index_prefixes")
821821
.field("min_chars", 1)
822822
.field("max_chars", 25)
823823
.endObject()
@@ -834,27 +834,27 @@ public void testIndexPrefixMapping() throws IOException {
834834
.startObject("properties").startObject("field")
835835
.field("type", "text")
836836
.field("analyzer", "english")
837-
.field("index_prefix", (String) null)
837+
.field("index_prefixes", (String) null)
838838
.endObject().endObject()
839839
.endObject().endObject());
840840
MapperParsingException e = expectThrows(MapperParsingException.class,
841841
() -> parser.parse("type", new CompressedXContent(badConfigMapping))
842842
);
843-
assertThat(e.getMessage(), containsString("[index_prefix] must not have a [null] value"));
843+
assertThat(e.getMessage(), containsString("[index_prefixes] must not have a [null] value"));
844844
}
845845

846846
{
847847
String badConfigMapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
848848
.startObject("properties").startObject("field")
849849
.field("type", "text")
850850
.field("index", "false")
851-
.startObject("index_prefix").endObject()
851+
.startObject("index_prefixes").endObject()
852852
.endObject().endObject()
853853
.endObject().endObject());
854854
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
855855
() -> parser.parse("type", new CompressedXContent(badConfigMapping))
856856
);
857-
assertThat(e.getMessage(), containsString("Cannot set index_prefix on unindexed field [field]"));
857+
assertThat(e.getMessage(), containsString("Cannot set index_prefixes on unindexed field [field]"));
858858
}
859859
}
860860
}

server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ public void modify(MappedFieldType ft) {
7171
tft.setFielddataMinSegmentSize(1000);
7272
}
7373
});
74+
addModifier(new Modifier("index_prefixes", true) {
75+
@Override
76+
public void modify(MappedFieldType ft) {
77+
TextFieldMapper.TextFieldType tft = (TextFieldMapper.TextFieldType)ft;
78+
TextFieldMapper.PrefixFieldType pft = tft.getPrefixFieldType();
79+
if (pft == null) {
80+
tft.setPrefixFieldType(new TextFieldMapper.PrefixFieldType(ft.name(), 3, 3));
81+
}
82+
else {
83+
tft.setPrefixFieldType(null);
84+
}
85+
}
86+
});
7487
}
7588

7689
public void testTermQuery() {

0 commit comments

Comments
 (0)