|
| 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.internal; |
| 21 | + |
| 22 | +import com.google.common.collect.UnmodifiableIterator; |
| 23 | +import org.apache.lucene.document.Field; |
| 24 | +import org.apache.lucene.document.FieldType; |
| 25 | +import org.apache.lucene.document.SortedSetDocValuesField; |
| 26 | +import org.apache.lucene.document.XStringField; |
| 27 | +import org.apache.lucene.index.FieldInfo.IndexOptions; |
| 28 | +import org.apache.lucene.index.IndexableField; |
| 29 | +import org.apache.lucene.util.BytesRef; |
| 30 | +import org.elasticsearch.Version; |
| 31 | +import org.elasticsearch.common.Nullable; |
| 32 | +import org.elasticsearch.common.lucene.Lucene; |
| 33 | +import org.elasticsearch.common.settings.Settings; |
| 34 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 35 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 36 | +import org.elasticsearch.index.codec.docvaluesformat.DocValuesFormatProvider; |
| 37 | +import org.elasticsearch.index.codec.postingsformat.PostingsFormatProvider; |
| 38 | +import org.elasticsearch.index.fielddata.FieldDataType; |
| 39 | +import org.elasticsearch.index.mapper.*; |
| 40 | +import org.elasticsearch.index.mapper.core.AbstractFieldMapper; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Iterator; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Map; |
| 47 | + |
| 48 | +import static org.elasticsearch.index.mapper.MapperBuilders.fieldNames; |
| 49 | +import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; |
| 50 | + |
| 51 | +/** |
| 52 | + * A mapper that indexes the field names of a document under <code>_field_names</code>. This mapper is typically useful in order |
| 53 | + * to have fast <code>exists</code> and <code>missing</code> queries/filters. |
| 54 | + * |
| 55 | + * Added in Elasticsearch 1.3. |
| 56 | + */ |
| 57 | +public class FieldNamesFieldMapper extends AbstractFieldMapper<String> implements InternalMapper, RootMapper { |
| 58 | + |
| 59 | + public static final String NAME = "_field_names"; |
| 60 | + |
| 61 | + public static final String CONTENT_TYPE = "_field_names"; |
| 62 | + |
| 63 | + public static class Defaults extends AbstractFieldMapper.Defaults { |
| 64 | + public static final String NAME = FieldNamesFieldMapper.NAME; |
| 65 | + public static final String INDEX_NAME = FieldNamesFieldMapper.NAME; |
| 66 | + |
| 67 | + public static final FieldType FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE); |
| 68 | + public static final FieldType FIELD_TYPE_PRE_1_3_0; |
| 69 | + |
| 70 | + static { |
| 71 | + FIELD_TYPE.setIndexed(true); |
| 72 | + FIELD_TYPE.setTokenized(false); |
| 73 | + FIELD_TYPE.setStored(false); |
| 74 | + FIELD_TYPE.setOmitNorms(true); |
| 75 | + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY); |
| 76 | + FIELD_TYPE.freeze(); |
| 77 | + FIELD_TYPE_PRE_1_3_0 = new FieldType(FIELD_TYPE); |
| 78 | + FIELD_TYPE_PRE_1_3_0.setIndexed(false); |
| 79 | + FIELD_TYPE_PRE_1_3_0.freeze(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static class Builder extends AbstractFieldMapper.Builder<Builder, FieldNamesFieldMapper> { |
| 84 | + |
| 85 | + private boolean indexIsExplicit; |
| 86 | + |
| 87 | + public Builder() { |
| 88 | + super(Defaults.NAME, new FieldType(Defaults.FIELD_TYPE)); |
| 89 | + indexName = Defaults.INDEX_NAME; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Builder index(boolean index) { |
| 94 | + indexIsExplicit = true; |
| 95 | + return super.index(index); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public FieldNamesFieldMapper build(BuilderContext context) { |
| 100 | + if ((context.indexCreatedVersion() == null || context.indexCreatedVersion().before(Version.V_1_3_0)) && !indexIsExplicit) { |
| 101 | + fieldType.setIndexed(false); |
| 102 | + } |
| 103 | + return new FieldNamesFieldMapper(name, indexName, boost, fieldType, postingsProvider, docValuesProvider, fieldDataSettings, context.indexSettings()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static class TypeParser implements Mapper.TypeParser { |
| 108 | + @Override |
| 109 | + public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException { |
| 110 | + FieldNamesFieldMapper.Builder builder = fieldNames(); |
| 111 | + parseField(builder, builder.name, node, parserContext); |
| 112 | + return builder; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private final FieldType defaultFieldType; |
| 117 | + |
| 118 | + private static FieldType defaultFieldType(Settings indexSettings) { |
| 119 | + return indexSettings != null && Version.indexCreated(indexSettings).onOrAfter(Version.V_1_3_0) ? Defaults.FIELD_TYPE : Defaults.FIELD_TYPE_PRE_1_3_0; |
| 120 | + } |
| 121 | + |
| 122 | + public FieldNamesFieldMapper(Settings indexSettings) { |
| 123 | + this(Defaults.NAME, Defaults.INDEX_NAME, indexSettings); |
| 124 | + } |
| 125 | + |
| 126 | + protected FieldNamesFieldMapper(String name, String indexName, Settings indexSettings) { |
| 127 | + this(name, indexName, Defaults.BOOST, new FieldType(defaultFieldType(indexSettings)), null, null, null, indexSettings); |
| 128 | + } |
| 129 | + |
| 130 | + public FieldNamesFieldMapper(String name, String indexName, float boost, FieldType fieldType, PostingsFormatProvider postingsProvider, |
| 131 | + DocValuesFormatProvider docValuesProvider, @Nullable Settings fieldDataSettings, Settings indexSettings) { |
| 132 | + super(new Names(name, indexName, indexName, name), boost, fieldType, null, Lucene.KEYWORD_ANALYZER, |
| 133 | + Lucene.KEYWORD_ANALYZER, postingsProvider, docValuesProvider, null, null, fieldDataSettings, indexSettings); |
| 134 | + this.defaultFieldType = defaultFieldType(indexSettings); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public FieldType defaultFieldType() { |
| 139 | + return defaultFieldType; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public FieldDataType defaultFieldDataType() { |
| 144 | + return new FieldDataType("string"); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String value(Object value) { |
| 149 | + if (value == null) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + return value.toString(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean useTermQueryWithQueryString() { |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void preParse(ParseContext context) throws IOException { |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void postParse(ParseContext context) throws IOException { |
| 166 | + super.parse(context); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void parse(ParseContext context) throws IOException { |
| 171 | + // we parse in post parse |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public boolean includeInObject() { |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + static Iterable<String> extractFieldNames(final String fullPath) { |
| 180 | + return new Iterable<String>() { |
| 181 | + @Override |
| 182 | + public Iterator<String> iterator() { |
| 183 | + return new UnmodifiableIterator<String>() { |
| 184 | + |
| 185 | + int endIndex = nextEndIndex(0); |
| 186 | + |
| 187 | + private int nextEndIndex(int index) { |
| 188 | + while (index < fullPath.length() && fullPath.charAt(index) != '.') { |
| 189 | + index += 1; |
| 190 | + } |
| 191 | + return index; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public boolean hasNext() { |
| 196 | + return endIndex <= fullPath.length(); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public String next() { |
| 201 | + final String result = fullPath.substring(0, endIndex); |
| 202 | + endIndex = nextEndIndex(endIndex + 1); |
| 203 | + return result; |
| 204 | + } |
| 205 | + |
| 206 | + }; |
| 207 | + } |
| 208 | + }; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { |
| 213 | + if (!fieldType.indexed() && !fieldType.stored() && !hasDocValues()) { |
| 214 | + return; |
| 215 | + } |
| 216 | + for (ParseContext.Document document : context.docs()) { |
| 217 | + final List<String> paths = new ArrayList<>(); |
| 218 | + for (IndexableField field : document.getFields()) { |
| 219 | + paths.add(field.name()); |
| 220 | + } |
| 221 | + for (String path : paths) { |
| 222 | + for (String fieldName : extractFieldNames(path)) { |
| 223 | + if (fieldType.indexed() || fieldType.stored()) { |
| 224 | + document.add(new XStringField(names().indexName(), fieldName, fieldType)); |
| 225 | + } |
| 226 | + if (hasDocValues()) { |
| 227 | + document.add(new SortedSetDocValuesField(names().indexName(), new BytesRef(fieldName))); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + protected String contentType() { |
| 236 | + return CONTENT_TYPE; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 241 | + XContentBuilder json = XContentFactory.jsonBuilder(); |
| 242 | + super.toXContent(json, params); |
| 243 | + if (json.string().equals("\"" + NAME + "\"{\"type\":\"" + CONTENT_TYPE + "\"}")) { |
| 244 | + return builder; |
| 245 | + } |
| 246 | + return super.toXContent(builder, params); |
| 247 | + } |
| 248 | +} |
0 commit comments