|
| 1 | +/* |
| 2 | + * Licensed to Elastic Search and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. Elastic Search licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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.json; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import org.elasticsearch.index.mapper.FieldMapper; |
| 24 | +import org.elasticsearch.index.mapper.FieldMapperListener; |
| 25 | +import org.elasticsearch.index.mapper.MergeMappingException; |
| 26 | +import org.elasticsearch.util.json.JsonBuilder; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static com.google.common.collect.Lists.*; |
| 34 | +import static org.elasticsearch.util.MapBuilder.*; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author kimchy (shay.banon) |
| 38 | + */ |
| 39 | +public class JsonMultiFieldMapper implements JsonMapper { |
| 40 | + |
| 41 | + public static final String JSON_TYPE = "multi_field"; |
| 42 | + |
| 43 | + public static class Defaults { |
| 44 | + public static final JsonPath.Type PATH_TYPE = JsonPath.Type.FULL; |
| 45 | + } |
| 46 | + |
| 47 | + public static class Builder extends JsonMapper.Builder<Builder, JsonMultiFieldMapper> { |
| 48 | + |
| 49 | + private JsonPath.Type pathType = Defaults.PATH_TYPE; |
| 50 | + |
| 51 | + private final List<JsonMapper.Builder> mappersBuilders = newArrayList(); |
| 52 | + |
| 53 | + private JsonMapper.Builder defaultMapperBuilder; |
| 54 | + |
| 55 | + public Builder(String name) { |
| 56 | + super(name); |
| 57 | + this.builder = this; |
| 58 | + } |
| 59 | + |
| 60 | + public Builder pathType(JsonPath.Type pathType) { |
| 61 | + this.pathType = pathType; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + public Builder add(JsonMapper.Builder builder) { |
| 66 | + if (builder.name.equals(name)) { |
| 67 | + defaultMapperBuilder = builder; |
| 68 | + } else { |
| 69 | + mappersBuilders.add(builder); |
| 70 | + } |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + @Override public JsonMultiFieldMapper build(BuilderContext context) { |
| 75 | + JsonPath.Type origPathType = context.path().pathType(); |
| 76 | + context.path().pathType(pathType); |
| 77 | + |
| 78 | + JsonMapper defaultMapper = null; |
| 79 | + if (defaultMapperBuilder != null) { |
| 80 | + defaultMapper = defaultMapperBuilder.build(context); |
| 81 | + } |
| 82 | + |
| 83 | + context.path().add(name); |
| 84 | + Map<String, JsonMapper> mappers = new HashMap<String, JsonMapper>(); |
| 85 | + for (JsonMapper.Builder builder : mappersBuilders) { |
| 86 | + JsonMapper mapper = builder.build(context); |
| 87 | + mappers.put(mapper.name(), mapper); |
| 88 | + } |
| 89 | + context.path().remove(); |
| 90 | + |
| 91 | + context.path().pathType(origPathType); |
| 92 | + |
| 93 | + return new JsonMultiFieldMapper(name, pathType, mappers, defaultMapper); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private final String name; |
| 98 | + |
| 99 | + private final JsonPath.Type pathType; |
| 100 | + |
| 101 | + private final Object mutex = new Object(); |
| 102 | + |
| 103 | + private volatile ImmutableMap<String, JsonMapper> mappers = ImmutableMap.of(); |
| 104 | + |
| 105 | + private volatile JsonMapper defaultMapper; |
| 106 | + |
| 107 | + public JsonMultiFieldMapper(String name, JsonPath.Type pathType, JsonMapper defaultMapper) { |
| 108 | + this(name, pathType, new HashMap<String, JsonMapper>(), defaultMapper); |
| 109 | + } |
| 110 | + |
| 111 | + public JsonMultiFieldMapper(String name, JsonPath.Type pathType, Map<String, JsonMapper> mappers, JsonMapper defaultMapper) { |
| 112 | + this.name = name; |
| 113 | + this.pathType = pathType; |
| 114 | + this.mappers = ImmutableMap.copyOf(mappers); |
| 115 | + this.defaultMapper = defaultMapper; |
| 116 | + } |
| 117 | + |
| 118 | + @Override public String name() { |
| 119 | + return this.name; |
| 120 | + } |
| 121 | + |
| 122 | + public JsonPath.Type pathType() { |
| 123 | + return pathType; |
| 124 | + } |
| 125 | + |
| 126 | + public JsonMapper defaultMapper() { |
| 127 | + return this.defaultMapper; |
| 128 | + } |
| 129 | + |
| 130 | + public ImmutableMap<String, JsonMapper> mappers() { |
| 131 | + return this.mappers; |
| 132 | + } |
| 133 | + |
| 134 | + @Override public void parse(JsonParseContext jsonContext) throws IOException { |
| 135 | + JsonPath.Type origPathType = jsonContext.path().pathType(); |
| 136 | + jsonContext.path().pathType(pathType); |
| 137 | + |
| 138 | + // do the default mapper without adding the path |
| 139 | + if (defaultMapper != null) { |
| 140 | + defaultMapper.parse(jsonContext); |
| 141 | + } |
| 142 | + |
| 143 | + jsonContext.path().add(name); |
| 144 | + for (JsonMapper mapper : mappers.values()) { |
| 145 | + mapper.parse(jsonContext); |
| 146 | + } |
| 147 | + jsonContext.path().remove(); |
| 148 | + |
| 149 | + jsonContext.path().pathType(origPathType); |
| 150 | + } |
| 151 | + |
| 152 | + @Override public void merge(JsonMapper mergeWith, JsonMergeContext mergeContext) throws MergeMappingException { |
| 153 | + if (!(mergeWith instanceof JsonMultiFieldMapper)) { |
| 154 | + mergeContext.addConflict("Can't merge a non multi_field mapping [" + mergeWith.name() + "] with a multi_field mapping [" + name() + "]"); |
| 155 | + return; |
| 156 | + } |
| 157 | + JsonMultiFieldMapper mergeWithMultiField = (JsonMultiFieldMapper) mergeWith; |
| 158 | + synchronized (mutex) { |
| 159 | + // merge the default mapper |
| 160 | + if (defaultMapper == null) { |
| 161 | + if (mergeWithMultiField.defaultMapper != null) { |
| 162 | + if (!mergeContext.mergeFlags().simulate()) { |
| 163 | + defaultMapper = mergeWithMultiField.defaultMapper; |
| 164 | + mergeContext.docMapper().addFieldMapper((FieldMapper) defaultMapper); |
| 165 | + } |
| 166 | + } |
| 167 | + } else { |
| 168 | + if (mergeWithMultiField.defaultMapper != null) { |
| 169 | + defaultMapper.merge(mergeWithMultiField.defaultMapper, mergeContext); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // merge all the other mappers |
| 174 | + for (JsonMapper mergeWithMapper : mergeWithMultiField.mappers.values()) { |
| 175 | + JsonMapper mergeIntoMapper = mappers.get(mergeWithMapper.name()); |
| 176 | + if (mergeIntoMapper == null) { |
| 177 | + // no mapping, simply add it if not simulating |
| 178 | + if (!mergeContext.mergeFlags().simulate()) { |
| 179 | + mappers = newMapBuilder(mappers).put(mergeWithMapper.name(), mergeWithMapper).immutableMap(); |
| 180 | + if (mergeWithMapper instanceof JsonFieldMapper) { |
| 181 | + mergeContext.docMapper().addFieldMapper((FieldMapper) mergeWithMapper); |
| 182 | + } |
| 183 | + } |
| 184 | + } else { |
| 185 | + mergeIntoMapper.merge(mergeWithMapper, mergeContext); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Override public void traverse(FieldMapperListener fieldMapperListener) { |
| 192 | + for (JsonMapper mapper : mappers.values()) { |
| 193 | + mapper.traverse(fieldMapperListener); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Override public void toJson(JsonBuilder builder, Params params) throws IOException { |
| 198 | + builder.startObject(name); |
| 199 | + builder.field("type", JSON_TYPE); |
| 200 | + builder.field("pathType", pathType.name().toLowerCase()); |
| 201 | + |
| 202 | + builder.startObject("fields"); |
| 203 | + if (defaultMapper != null) { |
| 204 | + defaultMapper.toJson(builder, params); |
| 205 | + } |
| 206 | + for (JsonMapper mapper : mappers.values()) { |
| 207 | + mapper.toJson(builder, params); |
| 208 | + } |
| 209 | + builder.endObject(); |
| 210 | + |
| 211 | + builder.endObject(); |
| 212 | + } |
| 213 | +} |
0 commit comments