|
24 | 24 | import org.apache.lucene.analysis.MockTokenizer; |
25 | 25 | import org.apache.lucene.analysis.Token; |
26 | 26 | import org.apache.lucene.analysis.TokenStream; |
| 27 | +import org.elasticsearch.common.bytes.BytesReference; |
27 | 28 | import org.elasticsearch.common.compress.CompressedXContent; |
28 | 29 | import org.elasticsearch.common.xcontent.XContentFactory; |
| 30 | +import org.elasticsearch.common.xcontent.XContentType; |
29 | 31 | import org.elasticsearch.index.IndexService; |
30 | 32 | import org.elasticsearch.plugins.Plugin; |
31 | 33 | import org.elasticsearch.test.ESSingleNodeTestCase; |
@@ -144,4 +146,55 @@ public void testEmptyName() throws IOException { |
144 | 146 | ); |
145 | 147 | assertThat(e.getMessage(), containsString("name cannot be empty string")); |
146 | 148 | } |
| 149 | + |
| 150 | + public void testParseNullValue() throws Exception { |
| 151 | + DocumentMapper mapper = createIndexWithTokenCountField(); |
| 152 | + ParseContext.Document doc = parseDocument(mapper, createDocument(null)); |
| 153 | + assertNull(doc.getField("test.tc")); |
| 154 | + } |
| 155 | + |
| 156 | + public void testParseEmptyValue() throws Exception { |
| 157 | + DocumentMapper mapper = createIndexWithTokenCountField(); |
| 158 | + ParseContext.Document doc = parseDocument(mapper, createDocument("")); |
| 159 | + assertEquals(0, doc.getField("test.tc").numericValue()); |
| 160 | + } |
| 161 | + |
| 162 | + public void testParseNotNullValue() throws Exception { |
| 163 | + DocumentMapper mapper = createIndexWithTokenCountField(); |
| 164 | + ParseContext.Document doc = parseDocument(mapper, createDocument("three tokens string")); |
| 165 | + assertEquals(3, doc.getField("test.tc").numericValue()); |
| 166 | + } |
| 167 | + |
| 168 | + private DocumentMapper createIndexWithTokenCountField() throws IOException { |
| 169 | + final String content = XContentFactory.jsonBuilder().startObject() |
| 170 | + .startObject("person") |
| 171 | + .startObject("properties") |
| 172 | + .startObject("test") |
| 173 | + .field("type", "text") |
| 174 | + .startObject("fields") |
| 175 | + .startObject("tc") |
| 176 | + .field("type", "token_count") |
| 177 | + .field("analyzer", "standard") |
| 178 | + .endObject() |
| 179 | + .endObject() |
| 180 | + .endObject() |
| 181 | + .endObject() |
| 182 | + .endObject().endObject().string(); |
| 183 | + |
| 184 | + return createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(content)); |
| 185 | + } |
| 186 | + |
| 187 | + private SourceToParse createDocument(String fieldValue) throws Exception { |
| 188 | + BytesReference request = XContentFactory.jsonBuilder() |
| 189 | + .startObject() |
| 190 | + .field("test", fieldValue) |
| 191 | + .endObject().bytes(); |
| 192 | + |
| 193 | + return SourceToParse.source("test", "person", "1", request, XContentType.JSON); |
| 194 | + } |
| 195 | + |
| 196 | + private ParseContext.Document parseDocument(DocumentMapper mapper, SourceToParse request) { |
| 197 | + return mapper.parse(request) |
| 198 | + .docs().stream().findFirst().orElseThrow(() -> new IllegalStateException("Test object not parsed")); |
| 199 | + } |
147 | 200 | } |
0 commit comments