Skip to content

Commit 0eedc78

Browse files
committed
Automatically add a sub keyword field to string dynamic mappings. #17188
If you add a string field to a document, it will have the following default mapping: ``` { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } ```
1 parent 4bd27bc commit 0eedc78

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ private static Mapper.Builder<?,?> createBuilderFromFieldType(final ParseContext
614614
} else if (fieldType instanceof TextFieldType) {
615615
builder = context.root().findTemplateBuilder(context, currentFieldName, "text", "string");
616616
if (builder == null) {
617-
builder = new TextFieldMapper.Builder(currentFieldName);
617+
builder = new TextFieldMapper.Builder(currentFieldName)
618+
.addMultiField(new KeywordFieldMapper.Builder("keyword").ignoreAbove(256));
618619
}
619620
} else if (fieldType instanceof KeywordFieldType) {
620621
builder = context.root().findTemplateBuilder(context, currentFieldName, "keyword", "string");
@@ -714,7 +715,8 @@ private static Mapper.Builder<?,?> createBuilderFromDynamicValue(final ParseCont
714715
}
715716
Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "string");
716717
if (builder == null) {
717-
builder = new TextFieldMapper.Builder(currentFieldName);
718+
builder = new TextFieldMapper.Builder(currentFieldName)
719+
.addMultiField(new KeywordFieldMapper.Builder("keyword").ignoreAbove(256));
718720
}
719721
return builder;
720722
} else if (token == XContentParser.Token.VALUE_NUMBER) {

core/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.elasticsearch.test.ESSingleNodeTestCase;
4343

4444
import java.io.IOException;
45-
import java.util.List;
4645

4746
import static java.util.Collections.emptyMap;
4847
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@@ -245,7 +244,17 @@ public void testField() throws Exception {
245244
// original mapping not modified
246245
assertEquals(mapping, serialize(mapper));
247246
// but we have an update
248-
assertEquals("{\"type\":{\"properties\":{\"foo\":{\"type\":\"text\"}}}}", serialize(update));
247+
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
248+
.startObject("foo")
249+
.field("type", "text")
250+
.startObject("fields")
251+
.startObject("keyword")
252+
.field("type", "keyword")
253+
.field("ignore_above", 256)
254+
.endObject()
255+
.endObject()
256+
.endObject()
257+
.endObject().endObject().endObject().string(), serialize(update));
249258
}
250259

251260
public void testIncremental() throws Exception {
@@ -267,7 +276,14 @@ public void testIncremental() throws Exception {
267276
// but we have an update
268277
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
269278
// foo is NOT in the update
270-
.startObject("bar").field("type", "text").endObject()
279+
.startObject("bar").field("type", "text")
280+
.startObject("fields")
281+
.startObject("keyword")
282+
.field("type", "keyword")
283+
.field("ignore_above", 256)
284+
.endObject()
285+
.endObject()
286+
.endObject()
271287
.endObject().endObject().string(), serialize(update));
272288
}
273289

@@ -287,8 +303,22 @@ public void testIntroduceTwoFields() throws Exception {
287303
assertEquals(mapping, serialize(mapper));
288304
// but we have an update
289305
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
290-
.startObject("bar").field("type", "text").endObject()
291-
.startObject("foo").field("type", "text").endObject()
306+
.startObject("bar").field("type", "text")
307+
.startObject("fields")
308+
.startObject("keyword")
309+
.field("type", "keyword")
310+
.field("ignore_above", 256)
311+
.endObject()
312+
.endObject()
313+
.endObject()
314+
.startObject("foo").field("type", "text")
315+
.startObject("fields")
316+
.startObject("keyword")
317+
.field("type", "keyword")
318+
.field("ignore_above", 256)
319+
.endObject()
320+
.endObject()
321+
.endObject()
292322
.endObject().endObject().string(), serialize(update));
293323
}
294324

@@ -308,7 +338,9 @@ public void testObject() throws Exception {
308338
assertEquals(mapping, serialize(mapper));
309339
// but we have an update
310340
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
311-
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").endObject().endObject().endObject().endObject().endObject()
341+
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text")
342+
.startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject()
343+
.endObject().endObject().endObject().endObject().endObject().endObject()
312344
.endObject().endObject().endObject().string(), serialize(update));
313345
}
314346

@@ -328,7 +360,15 @@ public void testArray() throws Exception {
328360
assertEquals(mapping, serialize(mapper));
329361
// but we have an update
330362
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
331-
.startObject("foo").field("type", "text").endObject()
363+
.startObject("foo")
364+
.field("type", "text")
365+
.startObject("fields")
366+
.startObject("keyword")
367+
.field("type", "keyword")
368+
.field("ignore_above", 256)
369+
.endObject()
370+
.endObject()
371+
.endObject()
332372
.endObject().endObject().endObject().string(), serialize(update));
333373
}
334374

@@ -348,7 +388,9 @@ public void testInnerDynamicMapping() throws Exception {
348388
assertEquals(mapping, serialize(mapper));
349389
// but we have an update
350390
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
351-
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").endObject().endObject().endObject().endObject().endObject()
391+
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").startObject("fields")
392+
.startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject()
393+
.endObject().endObject().endObject().endObject().endObject().endObject()
352394
.endObject().endObject().endObject().string(), serialize(update));
353395
}
354396

@@ -369,7 +411,14 @@ public void testComplexArray() throws Exception {
369411
assertEquals(mapping, serialize(mapper));
370412
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
371413
.startObject("foo").startObject("properties")
372-
.startObject("bar").field("type", "text").endObject()
414+
.startObject("bar").field("type", "text")
415+
.startObject("fields")
416+
.startObject("keyword")
417+
.field("type", "keyword")
418+
.field("ignore_above", 256)
419+
.endObject()
420+
.endObject()
421+
.endObject()
373422
.startObject("baz").field("type", "long").endObject()
374423
.endObject().endObject()
375424
.endObject().endObject().endObject().string(), serialize(update));

core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void testInjectIntoDocDuringParsing() throws Exception {
102102
.endObject()
103103
.bytes());
104104

105-
assertFieldNames(set("a", "b", "b.c", "_uid", "_type", "_version", "_source", "_all"), doc);
105+
assertFieldNames(set("a", "a.keyword", "b", "b.c", "_uid", "_type", "_version", "_source", "_all"), doc);
106106
}
107107

108108
public void testExplicitEnabled() throws Exception {
@@ -119,7 +119,7 @@ public void testExplicitEnabled() throws Exception {
119119
.endObject()
120120
.bytes());
121121

122-
assertFieldNames(set("field", "_uid", "_type", "_version", "_source", "_all"), doc);
122+
assertFieldNames(set("field", "field.keyword", "_uid", "_type", "_version", "_source", "_all"), doc);
123123
}
124124

125125
public void testDisabled() throws Exception {

core/src/test/java/org/elasticsearch/search/highlight/HighlighterSearchIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ public void testForceSourceWithSourceDisabled() throws Exception {
564564
.startObject("properties")
565565
.startObject("field1").field("type", "text").field("store", true).field("index_options", "offsets")
566566
.field("term_vector", "with_positions_offsets").endObject()
567+
.startObject("field2").field("type", "text").endObject()
567568
.endObject().endObject().endObject()));
568569

569570
ensureGreen();

0 commit comments

Comments
 (0)