Skip to content

Commit

Permalink
Automatically add a sub keyword field to string dynamic mappings. #17188
Browse files Browse the repository at this point in the history


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
    }
  }
}
```
  • Loading branch information
jpountz committed Mar 29, 2016
1 parent 4bd27bc commit 0eedc78
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
Expand Up @@ -614,7 +614,8 @@ private static Mapper.Builder<?,?> createBuilderFromFieldType(final ParseContext
} else if (fieldType instanceof TextFieldType) {
builder = context.root().findTemplateBuilder(context, currentFieldName, "text", "string");
if (builder == null) {
builder = new TextFieldMapper.Builder(currentFieldName);
builder = new TextFieldMapper.Builder(currentFieldName)
.addMultiField(new KeywordFieldMapper.Builder("keyword").ignoreAbove(256));
}
} else if (fieldType instanceof KeywordFieldType) {
builder = context.root().findTemplateBuilder(context, currentFieldName, "keyword", "string");
Expand Down Expand Up @@ -714,7 +715,8 @@ private static Mapper.Builder<?,?> createBuilderFromDynamicValue(final ParseCont
}
Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "string");
if (builder == null) {
builder = new TextFieldMapper.Builder(currentFieldName);
builder = new TextFieldMapper.Builder(currentFieldName)
.addMultiField(new KeywordFieldMapper.Builder("keyword").ignoreAbove(256));
}
return builder;
} else if (token == XContentParser.Token.VALUE_NUMBER) {
Expand Down
Expand Up @@ -42,7 +42,6 @@
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;
import java.util.List;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
Expand Down Expand Up @@ -245,7 +244,17 @@ public void testField() throws Exception {
// original mapping not modified
assertEquals(mapping, serialize(mapper));
// but we have an update
assertEquals("{\"type\":{\"properties\":{\"foo\":{\"type\":\"text\"}}}}", serialize(update));
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("foo")
.field("type", "text")
.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.field("ignore_above", 256)
.endObject()
.endObject()
.endObject()
.endObject().endObject().endObject().string(), serialize(update));
}

public void testIncremental() throws Exception {
Expand All @@ -267,7 +276,14 @@ public void testIncremental() throws Exception {
// but we have an update
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
// foo is NOT in the update
.startObject("bar").field("type", "text").endObject()
.startObject("bar").field("type", "text")
.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.field("ignore_above", 256)
.endObject()
.endObject()
.endObject()
.endObject().endObject().string(), serialize(update));
}

Expand All @@ -287,8 +303,22 @@ public void testIntroduceTwoFields() throws Exception {
assertEquals(mapping, serialize(mapper));
// but we have an update
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("bar").field("type", "text").endObject()
.startObject("foo").field("type", "text").endObject()
.startObject("bar").field("type", "text")
.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.field("ignore_above", 256)
.endObject()
.endObject()
.endObject()
.startObject("foo").field("type", "text")
.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.field("ignore_above", 256)
.endObject()
.endObject()
.endObject()
.endObject().endObject().string(), serialize(update));
}

Expand All @@ -308,7 +338,9 @@ public void testObject() throws Exception {
assertEquals(mapping, serialize(mapper));
// but we have an update
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").endObject().endObject().endObject().endObject().endObject()
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text")
.startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject()
.endObject().endObject().endObject().endObject().endObject().endObject()
.endObject().endObject().endObject().string(), serialize(update));
}

Expand All @@ -328,7 +360,15 @@ public void testArray() throws Exception {
assertEquals(mapping, serialize(mapper));
// but we have an update
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("foo").field("type", "text").endObject()
.startObject("foo")
.field("type", "text")
.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.field("ignore_above", 256)
.endObject()
.endObject()
.endObject()
.endObject().endObject().endObject().string(), serialize(update));
}

Expand All @@ -348,7 +388,9 @@ public void testInnerDynamicMapping() throws Exception {
assertEquals(mapping, serialize(mapper));
// but we have an update
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").endObject().endObject().endObject().endObject().endObject()
.startObject("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").startObject("fields")
.startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject()
.endObject().endObject().endObject().endObject().endObject().endObject()
.endObject().endObject().endObject().string(), serialize(update));
}

Expand All @@ -369,7 +411,14 @@ public void testComplexArray() throws Exception {
assertEquals(mapping, serialize(mapper));
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("foo").startObject("properties")
.startObject("bar").field("type", "text").endObject()
.startObject("bar").field("type", "text")
.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.field("ignore_above", 256)
.endObject()
.endObject()
.endObject()
.startObject("baz").field("type", "long").endObject()
.endObject().endObject()
.endObject().endObject().endObject().string(), serialize(update));
Expand Down
Expand Up @@ -102,7 +102,7 @@ public void testInjectIntoDocDuringParsing() throws Exception {
.endObject()
.bytes());

assertFieldNames(set("a", "b", "b.c", "_uid", "_type", "_version", "_source", "_all"), doc);
assertFieldNames(set("a", "a.keyword", "b", "b.c", "_uid", "_type", "_version", "_source", "_all"), doc);
}

public void testExplicitEnabled() throws Exception {
Expand All @@ -119,7 +119,7 @@ public void testExplicitEnabled() throws Exception {
.endObject()
.bytes());

assertFieldNames(set("field", "_uid", "_type", "_version", "_source", "_all"), doc);
assertFieldNames(set("field", "field.keyword", "_uid", "_type", "_version", "_source", "_all"), doc);
}

public void testDisabled() throws Exception {
Expand Down
Expand Up @@ -564,6 +564,7 @@ public void testForceSourceWithSourceDisabled() throws Exception {
.startObject("properties")
.startObject("field1").field("type", "text").field("store", true).field("index_options", "offsets")
.field("term_vector", "with_positions_offsets").endObject()
.startObject("field2").field("type", "text").endObject()
.endObject().endObject().endObject()));

ensureGreen();
Expand Down

0 comments on commit 0eedc78

Please sign in to comment.