|
| 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 org.apache.lucene.analysis.Analyzer; |
| 23 | +import org.apache.lucene.document.Field; |
| 24 | +import org.apache.lucene.document.Fieldable; |
| 25 | +import org.elasticsearch.index.analysis.NamedAnalyzer; |
| 26 | +import org.elasticsearch.index.mapper.AllFieldMapper; |
| 27 | +import org.elasticsearch.index.mapper.MergeMappingException; |
| 28 | +import org.elasticsearch.util.json.JsonBuilder; |
| 29 | +import org.elasticsearch.util.lucene.Lucene; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +import static org.elasticsearch.util.lucene.all.AllTokenFilter.*; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author kimchy (shay.banon) |
| 37 | + */ |
| 38 | +public class JsonAllFieldMapper extends JsonFieldMapper<Void> implements AllFieldMapper { |
| 39 | + |
| 40 | + public static final String JSON_TYPE = "allField"; |
| 41 | + |
| 42 | + public static class Defaults extends JsonFieldMapper.Defaults { |
| 43 | + public static final String NAME = AllFieldMapper.NAME; |
| 44 | + public static final String INDEX_NAME = AllFieldMapper.NAME; |
| 45 | + public static final boolean ENABLED = true; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public static class Builder extends JsonFieldMapper.Builder<Builder, JsonAllFieldMapper> { |
| 50 | + |
| 51 | + private boolean enabled = Defaults.ENABLED; |
| 52 | + |
| 53 | + public Builder() { |
| 54 | + super(Defaults.NAME); |
| 55 | + builder = this; |
| 56 | + indexName = Defaults.INDEX_NAME; |
| 57 | + } |
| 58 | + |
| 59 | + public Builder enabled(boolean enabled) { |
| 60 | + this.enabled = enabled; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + @Override public Builder termVector(Field.TermVector termVector) { |
| 65 | + return super.termVector(termVector); |
| 66 | + } |
| 67 | + |
| 68 | + @Override protected Builder indexAnalyzer(NamedAnalyzer indexAnalyzer) { |
| 69 | + return super.indexAnalyzer(indexAnalyzer); |
| 70 | + } |
| 71 | + |
| 72 | + @Override protected Builder searchAnalyzer(NamedAnalyzer searchAnalyzer) { |
| 73 | + return super.searchAnalyzer(searchAnalyzer); |
| 74 | + } |
| 75 | + |
| 76 | + @Override public JsonAllFieldMapper build(BuilderContext context) { |
| 77 | + return new JsonAllFieldMapper(name, termVector, omitNorms, omitTermFreqAndPositions, |
| 78 | + indexAnalyzer, searchAnalyzer, enabled); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private boolean enabled; |
| 84 | + |
| 85 | + public JsonAllFieldMapper() { |
| 86 | + this(Defaults.NAME, Defaults.TERM_VECTOR, Defaults.OMIT_NORMS, Defaults.OMIT_TERM_FREQ_AND_POSITIONS, null, null, Defaults.ENABLED); |
| 87 | + } |
| 88 | + |
| 89 | + protected JsonAllFieldMapper(String name, Field.TermVector termVector, boolean omitNorms, boolean omitTermFreqAndPositions, |
| 90 | + NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, boolean enabled) { |
| 91 | + super(new Names(name, name, name, name), Field.Index.ANALYZED, Field.Store.NO, termVector, 1.0f, omitNorms, omitTermFreqAndPositions, |
| 92 | + indexAnalyzer, searchAnalyzer); |
| 93 | + this.enabled = enabled; |
| 94 | + } |
| 95 | + |
| 96 | + public boolean enabled() { |
| 97 | + return this.enabled; |
| 98 | + } |
| 99 | + |
| 100 | + @Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException { |
| 101 | + if (!enabled) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + Analyzer analyzer = indexAnalyzer(); |
| 105 | + if (analyzer == null) { |
| 106 | + analyzer = jsonContext.docMapper().indexAnalyzer(); |
| 107 | + if (analyzer == null) { |
| 108 | + analyzer = Lucene.STANDARD_ANALYZER; |
| 109 | + } |
| 110 | + } |
| 111 | + return new Field(names.indexName(), allTokenStream(names.indexName(), jsonContext.allEntries().finishTexts(), analyzer), termVector); |
| 112 | + } |
| 113 | + |
| 114 | + @Override public Void value(Fieldable field) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + @Override public String valueAsString(Fieldable field) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + @Override public Object valueForSearch(Fieldable field) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + @Override public String indexedValue(String value) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + @Override public String indexedValue(Void value) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + @Override protected String jsonType() { |
| 135 | + return JSON_TYPE; |
| 136 | + } |
| 137 | + |
| 138 | + @Override public void toJson(JsonBuilder builder, Params params) throws IOException { |
| 139 | + builder.startObject(JSON_TYPE); |
| 140 | + builder.field("enabled", enabled); |
| 141 | + builder.field("termVector", termVector.name().toLowerCase()); |
| 142 | + if (indexAnalyzer != null && !indexAnalyzer.name().startsWith("_")) { |
| 143 | + builder.field("indexAnalyzer", indexAnalyzer.name()); |
| 144 | + } |
| 145 | + if (searchAnalyzer != null && !searchAnalyzer.name().startsWith("_")) { |
| 146 | + builder.field("searchAnalyzer", searchAnalyzer.name()); |
| 147 | + } |
| 148 | + builder.endObject(); |
| 149 | + } |
| 150 | + |
| 151 | + @Override public void merge(JsonMapper mergeWith, JsonMergeContext mergeContext) throws MergeMappingException { |
| 152 | + // do nothing here, no merging, but also no exception |
| 153 | + } |
| 154 | +} |
0 commit comments