|
| 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.apache.lucene.document.Field; |
| 23 | +import org.apache.lucene.index.IndexOptions; |
| 24 | +import org.apache.lucene.index.IndexableField; |
| 25 | +import org.apache.lucene.search.Query; |
| 26 | +import org.apache.lucene.search.TermRangeQuery; |
| 27 | +import org.elasticsearch.common.lucene.Lucene; |
| 28 | +import org.elasticsearch.common.settings.Settings; |
| 29 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 30 | +import org.elasticsearch.index.query.QueryShardContext; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +/** |
| 37 | + * A field mapper that records fields that have been ignored because they were malformed. |
| 38 | + */ |
| 39 | +public final class IgnoredFieldMapper extends MetadataFieldMapper { |
| 40 | + |
| 41 | + public static final String NAME = "_ignored"; |
| 42 | + |
| 43 | + public static final String CONTENT_TYPE = "_ignored"; |
| 44 | + |
| 45 | + public static class Defaults { |
| 46 | + public static final String NAME = IgnoredFieldMapper.NAME; |
| 47 | + |
| 48 | + public static final MappedFieldType FIELD_TYPE = new IgnoredFieldType(); |
| 49 | + |
| 50 | + static { |
| 51 | + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); |
| 52 | + FIELD_TYPE.setTokenized(false); |
| 53 | + FIELD_TYPE.setStored(true); |
| 54 | + FIELD_TYPE.setOmitNorms(true); |
| 55 | + FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); |
| 56 | + FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); |
| 57 | + FIELD_TYPE.setName(NAME); |
| 58 | + FIELD_TYPE.freeze(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static class Builder extends MetadataFieldMapper.Builder<Builder, IgnoredFieldMapper> { |
| 63 | + |
| 64 | + public Builder(MappedFieldType existing) { |
| 65 | + super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.FIELD_TYPE); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public IgnoredFieldMapper build(BuilderContext context) { |
| 70 | + return new IgnoredFieldMapper(context.indexSettings()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static class TypeParser implements MetadataFieldMapper.TypeParser { |
| 75 | + @Override |
| 76 | + public MetadataFieldMapper.Builder<?,?> parse(String name, Map<String, Object> node, |
| 77 | + ParserContext parserContext) throws MapperParsingException { |
| 78 | + return new Builder(parserContext.mapperService().fullName(NAME)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext context) { |
| 83 | + final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); |
| 84 | + return new IgnoredFieldMapper(indexSettings); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static final class IgnoredFieldType extends TermBasedFieldType { |
| 89 | + |
| 90 | + public IgnoredFieldType() { |
| 91 | + } |
| 92 | + |
| 93 | + protected IgnoredFieldType(IgnoredFieldType ref) { |
| 94 | + super(ref); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public IgnoredFieldType clone() { |
| 99 | + return new IgnoredFieldType(this); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String typeName() { |
| 104 | + return CONTENT_TYPE; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Query existsQuery(QueryShardContext context) { |
| 109 | + // This query is not performance sensitive, it only helps assess |
| 110 | + // quality of the data, so we may use a slow query. It shouldn't |
| 111 | + // be too slow in practice since the number of unique terms in this |
| 112 | + // field is bounded by the number of fields in the mappings. |
| 113 | + return new TermRangeQuery(name(), null, null, true, true); |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + private IgnoredFieldMapper(Settings indexSettings) { |
| 119 | + super(NAME, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE, indexSettings); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void preParse(ParseContext context) throws IOException { |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void postParse(ParseContext context) throws IOException { |
| 128 | + super.parse(context); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Mapper parse(ParseContext context) throws IOException { |
| 133 | + // done in post-parse |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { |
| 139 | + for (String field : context.getIgnoredFields()) { |
| 140 | + context.doc().add(new Field(NAME, field, fieldType())); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + protected String contentType() { |
| 146 | + return CONTENT_TYPE; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 151 | + return builder; |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments