Skip to content

Commit

Permalink
Disallow the new parent join field on indices with multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Jun 7, 2017
1 parent bb0e522 commit 2364841
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -37,6 +36,7 @@
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.StringFieldType;

Expand Down Expand Up @@ -73,12 +73,18 @@ static String getParentIdFieldName(String joinFieldName, String parentName) {
return joinFieldName + "#" + parentName;
}

static void checkPreConditions(Version indexCreatedVersion, ContentPath path, String name) {
if (indexCreatedVersion.before(Version.V_6_0_0_alpha2)) {
throw new IllegalStateException("unable to create join field [" + name +
"] for index created before " + Version.V_6_0_0_alpha2);
private static void checkIndexCompatibility(IndexSettings settings, String name) {
if (settings.getIndexMetaData().isRoutingPartitionedIndex()) {
throw new IllegalStateException("cannot create join field [" + name + "] " +
"for the partitioned index " + "[" + settings.getIndex().getName() + "]");
}
if (settings.isSingleType() == false) {
throw new IllegalStateException("cannot create join field [" + name + "] " +
"on multi-types index [" + settings.getIndex().getName() + "]");
}
}

private static void checkObjectOrNested(ContentPath path, String name) {
if (path.pathAsText(name).contains(".")) {
throw new IllegalArgumentException("join field [" + path.pathAsText(name) + "] " +
"cannot be added inside an object or in a multi-field");
Expand Down Expand Up @@ -127,7 +133,7 @@ public Builder addParent(String parent, Set<String> children) {

@Override
public ParentJoinFieldMapper build(BuilderContext context) {
checkPreConditions(context.indexCreatedVersion(), context.path(), name);
checkObjectOrNested(context.path(), name);
fieldType.setName(name);
final List<ParentIdFieldMapper> parentIdFields = new ArrayList<>();
parentIdFieldBuilders.stream().map((e) -> e.build(context)).forEach(parentIdFields::add);
Expand All @@ -141,10 +147,7 @@ public static class TypeParser implements Mapper.TypeParser {
@Override
public Mapper.Builder<?,?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
final IndexSettings indexSettings = parserContext.mapperService().getIndexSettings();
if (indexSettings.getIndexMetaData().isRoutingPartitionedIndex()) {
throw new IllegalStateException("cannot set join field [" + name + "] for the partitioned index " +
"[" + indexSettings.getIndex().getName() + "]");
}
checkIndexCompatibility(indexSettings, name);

Builder builder = new Builder(name);
for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
Expand Down

0 comments on commit 2364841

Please sign in to comment.