Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
value = context.parser().textOrNull();
}

if (value == null && fieldType().nullValue() == null) {
return;
}

final int tokenCount;
if (value == null) {
tokenCount = (Integer) fieldType().nullValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
Expand Down Expand Up @@ -144,4 +146,55 @@ public void testEmptyName() throws IOException {
);
assertThat(e.getMessage(), containsString("name cannot be empty string"));
}

public void testParseNullValue() throws Exception {
DocumentMapper mapper = createIndexWithTokenCountField();
ParseContext.Document doc = parseDocument(mapper, createDocument(null));
assertNull(doc.getField("test.tc"));
}

public void testParseEmptyValue() throws Exception {
DocumentMapper mapper = createIndexWithTokenCountField();
ParseContext.Document doc = parseDocument(mapper, createDocument(""));
assertEquals(0, doc.getField("test.tc").numericValue());
}

public void testParseNotNullValue() throws Exception {
DocumentMapper mapper = createIndexWithTokenCountField();
ParseContext.Document doc = parseDocument(mapper, createDocument("three tokens string"));
assertEquals(3, doc.getField("test.tc").numericValue());
}

private DocumentMapper createIndexWithTokenCountField() throws IOException {
final String content = XContentFactory.jsonBuilder().startObject()
.startObject("person")
.startObject("properties")
.startObject("test")
.field("type", "text")
.startObject("fields")
.startObject("tc")
.field("type", "token_count")
.field("analyzer", "standard")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject().endObject().string();

return createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(content));
}

private SourceToParse createDocument(String fieldValue) throws Exception {
BytesReference request = XContentFactory.jsonBuilder()
.startObject()
.field("test", fieldValue)
.endObject().bytes();

return SourceToParse.source("test", "person", "1", request, XContentType.JSON);
}

private ParseContext.Document parseDocument(DocumentMapper mapper, SourceToParse request) {
return mapper.parse(request)
.docs().stream().findFirst().orElseThrow(() -> new IllegalStateException("Test object not parsed"));
}
}