Skip to content

Commit

Permalink
Prohibit indexing a document with parent for a type that doesn't have…
Browse files Browse the repository at this point in the history
… a `_parent` field configured.

Closes elastic#3848
  • Loading branch information
martijnvg committed Oct 15, 2013
1 parent 4d19239 commit 30447c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Expand Up @@ -578,6 +578,14 @@ public void process(MetaData metaData, String aliasOrIndex, @Nullable MappingMet
if (mappingMd.routing().required() && routing == null) {
throw new RoutingMissingException(index, type, id);
}

if (parent != null && !mappingMd.hasParentMapping()) {
throw new ElasticSearchIllegalArgumentException("Can't specify parent if no parent field has been configured");
}
} else {
if (parent != null) {
throw new ElasticSearchIllegalArgumentException("Can't specify parent if no parent field has been configured");
}
}

// generate id if not already provided and id generation is allowed
Expand Down
Expand Up @@ -258,13 +258,15 @@ public int hashCode() {
private Id id;
private Routing routing;
private Timestamp timestamp;
private boolean hasParentMapping;

public MappingMetaData(DocumentMapper docMapper) {
this.type = docMapper.type();
this.source = docMapper.mappingSource();
this.id = new Id(docMapper.idFieldMapper().path());
this.routing = new Routing(docMapper.routingFieldMapper().required(), docMapper.routingFieldMapper().path());
this.timestamp = new Timestamp(docMapper.timestampFieldMapper().enabled(), docMapper.timestampFieldMapper().path(), docMapper.timestampFieldMapper().dateTimeFormatter().format());
this.hasParentMapping = docMapper.parentFieldMapper() != null;
}

public MappingMetaData(CompressedString mapping) throws IOException {
Expand Down Expand Up @@ -374,6 +376,10 @@ public CompressedString source() {
return this.source;
}

public boolean hasParentMapping() {
return hasParentMapping;
}

/**
* Converts the serialized compressed form of the mappings into a parsed map.
*/
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.search.child;

import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.explain.ExplainResponse;
Expand Down Expand Up @@ -1832,4 +1833,31 @@ public void testHasChildQueryOnlyReturnsSingleChildType() {
assertHitCount(searchResponse, 0l);
}

@Test
public void indexChildDocWithNoParentMapping() throws ElasticSearchException, IOException {
client().admin().indices().prepareCreate("test")
.setSettings(
ImmutableSettings.settingsBuilder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
).execute().actionGet();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
client().admin().indices().preparePutMapping("test").setType("child1").setSource(jsonBuilder().startObject().startObject("type")
.endObject().endObject()).execute().actionGet();

client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").execute().actionGet();
try {
client().prepareIndex("test", "child1", "c1").setParent("p1").setSource("c_field", "blue").execute().actionGet();
fail();
} catch (ElasticSearchIllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Can't specify parent if no parent field has been configured"));
}
try {
client().prepareIndex("test", "child2", "c2").setParent("p1").setSource("c_field", "blue").execute().actionGet();
fail();
} catch (ElasticSearchIllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Can't specify parent if no parent field has been configured"));
}
}

}

0 comments on commit 30447c1

Please sign in to comment.