|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.mapper; |
| 21 | + |
| 22 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 23 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * A mapper for field aliases. |
| 32 | + * |
| 33 | + * A field alias has no concrete field mappings of its own, but instead points to another field by |
| 34 | + * its path. Once defined, an alias can be used in place of the concrete field name in search requests. |
| 35 | + */ |
| 36 | +public final class FieldAliasMapper extends Mapper { |
| 37 | + public static final String CONTENT_TYPE = "alias"; |
| 38 | + |
| 39 | + public static class Names { |
| 40 | + public static final String PATH = "path"; |
| 41 | + } |
| 42 | + |
| 43 | + private final String name; |
| 44 | + private final String path; |
| 45 | + |
| 46 | + public FieldAliasMapper(String simpleName, |
| 47 | + String name, |
| 48 | + String path) { |
| 49 | + super(simpleName); |
| 50 | + this.name = name; |
| 51 | + this.path = path; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String name() { |
| 56 | + return name; |
| 57 | + } |
| 58 | + |
| 59 | + public String path() { |
| 60 | + return path; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Mapper merge(Mapper mergeWith) { |
| 65 | + if (!(mergeWith instanceof FieldAliasMapper)) { |
| 66 | + throw new IllegalArgumentException("Cannot merge a field alias mapping [" |
| 67 | + + name() + "] with a mapping that is not for a field alias."); |
| 68 | + } |
| 69 | + return mergeWith; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Mapper updateFieldType(Map<String, MappedFieldType> fullNameToFieldType) { |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Iterator<Mapper> iterator() { |
| 79 | + return Collections.emptyIterator(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 84 | + return builder.startObject(simpleName()) |
| 85 | + .field("type", CONTENT_TYPE) |
| 86 | + .field(Names.PATH, path) |
| 87 | + .endObject(); |
| 88 | + } |
| 89 | + |
| 90 | + public static class TypeParser implements Mapper.TypeParser { |
| 91 | + @Override |
| 92 | + public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) |
| 93 | + throws MapperParsingException { |
| 94 | + FieldAliasMapper.Builder builder = new FieldAliasMapper.Builder(name); |
| 95 | + Object pathField = node.remove(Names.PATH); |
| 96 | + String path = XContentMapValues.nodeStringValue(pathField, null); |
| 97 | + if (path == null) { |
| 98 | + throw new MapperParsingException("The [path] property must be specified for field [" + name + "]."); |
| 99 | + } |
| 100 | + return builder.path(path); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static class Builder extends Mapper.Builder<FieldAliasMapper.Builder, FieldAliasMapper> { |
| 105 | + private String name; |
| 106 | + private String path; |
| 107 | + |
| 108 | + protected Builder(String name) { |
| 109 | + super(name); |
| 110 | + this.name = name; |
| 111 | + } |
| 112 | + |
| 113 | + public String name() { |
| 114 | + return this.name; |
| 115 | + } |
| 116 | + |
| 117 | + public Builder path(String path) { |
| 118 | + this.path = path; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + public FieldAliasMapper build(BuilderContext context) { |
| 123 | + String fullName = context.path().pathAsText(name); |
| 124 | + return new FieldAliasMapper(name, fullName, path); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments