Skip to content

Commit 86a58d5

Browse files
committed
another test and fix for partial parsed docs
1 parent 299abe1 commit 86a58d5

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public Engine.Create prepareCreate(SourceToParse source, long version, VersionTy
419419
ParsedDocument doc = docMapper.v1().parse(source).setMappingsModified(docMapper);
420420
return new Engine.Create(docMapper.v1(), docMapper.v1().uidMapper().term(doc.uid().stringValue()), doc, version, versionType, origin, startTime, state != IndexShardState.STARTED || canHaveDuplicates, autoGeneratedId);
421421
} catch (Throwable t) {
422-
if (docMapper.v2()) {
422+
if (docMapper.v2() || t instanceof MapperParsingException) {
423423
throw new WriteFailure(t, docMapper.v1().type());
424424
} else {
425425
throw t;

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323
import org.elasticsearch.common.settings.ImmutableSettings;
2424
import org.elasticsearch.common.xcontent.XContentBuilder;
2525
import org.elasticsearch.common.xcontent.XContentFactory;
26-
import org.elasticsearch.index.mapper.DocumentMapper;
27-
import org.elasticsearch.index.mapper.FieldMappers;
28-
import org.elasticsearch.index.mapper.ParsedDocument;
29-
import org.elasticsearch.index.mapper.StrictDynamicMappingException;
26+
import org.elasticsearch.index.mapper.*;
3027
import org.elasticsearch.index.IndexService;
3128
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
3229
import org.junit.Test;
3330

3431
import java.io.IOException;
32+
import java.util.LinkedHashMap;
33+
import java.util.Map;
3534

3635
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
3736
import static org.hamcrest.Matchers.equalTo;
@@ -242,4 +241,41 @@ public boolean apply(java.lang.Object input) {
242241
getMappingsResponse = client().admin().indices().prepareGetMappings("test").get();
243242
assertNotNull(getMappingsResponse.getMappings().get("test").get("type"));
244243
}
244+
245+
@Test
246+
public void testFieldsCreatedProperly() throws IOException, InterruptedException {
247+
XContentBuilder mapping = jsonBuilder().startObject().startObject("doc")
248+
.startObject("properties")
249+
.startObject("z")
250+
.field("type", "long")
251+
.endObject()
252+
.endObject()
253+
.endObject().endObject();
254+
255+
IndexService indexService = createIndex("test", ImmutableSettings.EMPTY, "doc", mapping);
256+
257+
try {
258+
client().prepareIndex().setIndex("test").setType("doc").setSource(jsonBuilder().startObject().field("a", "string").field("z", "string").endObject()).get();
259+
fail();
260+
} catch (MapperParsingException e) {
261+
// this should fail because the field z is of type long
262+
}
263+
//type should be in mapping
264+
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").get();
265+
assertNotNull(getMappingsResponse.getMappings().get("test").get("doc"));
266+
267+
client().prepareIndex().setIndex("test").setType("doc").setSource(jsonBuilder().startObject().field("a", "string").field("z", 0).endObject()).get();
268+
client().admin().indices().prepareRefresh("test").get();
269+
assertThat(client().prepareSearch("test").get().getHits().getTotalHits(), equalTo(1l));
270+
271+
DocumentMapper mapper = indexService.mapperService().documentMapper("doc");
272+
assertNotNull(mapper.mappers().name("a"));
273+
assertNotNull(mapper.mappers().name("z"));
274+
275+
getMappingsResponse = client().admin().indices().prepareGetMappings("test").get();
276+
assertNotNull(getMappingsResponse.getMappings().get("test").get("doc"));
277+
Map<String, Object> mappings = getMappingsResponse.getMappings().get("test").get("doc").getSourceAsMap();
278+
assertNotNull(((LinkedHashMap)mappings.get("properties")).get("a"));
279+
assertNotNull(((LinkedHashMap)mappings.get("properties")).get("z"));
280+
}
245281
}

0 commit comments

Comments
 (0)