|
| 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.analysis.standard.StandardAnalyzer; |
| 23 | +import org.apache.lucene.index.IndexReader; |
| 24 | +import org.apache.lucene.index.IndexWriterConfig; |
| 25 | +import org.apache.lucene.index.RandomIndexWriter; |
| 26 | +import org.apache.lucene.store.Directory; |
| 27 | +import org.elasticsearch.Version; |
| 28 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 29 | +import org.elasticsearch.common.CheckedConsumer; |
| 30 | +import org.elasticsearch.common.bytes.BytesReference; |
| 31 | +import org.elasticsearch.common.compress.CompressedXContent; |
| 32 | +import org.elasticsearch.common.settings.Settings; |
| 33 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 34 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 35 | +import org.elasticsearch.common.xcontent.XContentType; |
| 36 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 37 | +import org.elasticsearch.index.IndexSettings; |
| 38 | +import org.elasticsearch.index.analysis.AnalyzerScope; |
| 39 | +import org.elasticsearch.index.analysis.IndexAnalyzers; |
| 40 | +import org.elasticsearch.index.analysis.NamedAnalyzer; |
| 41 | +import org.elasticsearch.index.query.QueryShardContext; |
| 42 | +import org.elasticsearch.index.similarity.SimilarityService; |
| 43 | +import org.elasticsearch.indices.IndicesModule; |
| 44 | +import org.elasticsearch.indices.mapper.MapperRegistry; |
| 45 | +import org.elasticsearch.plugins.MapperPlugin; |
| 46 | +import org.elasticsearch.plugins.Plugin; |
| 47 | +import org.elasticsearch.script.ScriptService; |
| 48 | +import org.elasticsearch.test.ESTestCase; |
| 49 | + |
| 50 | +import java.io.IOException; |
| 51 | +import java.util.Collection; |
| 52 | +import java.util.Map; |
| 53 | + |
| 54 | +import static java.util.Collections.emptyList; |
| 55 | +import static java.util.Collections.emptyMap; |
| 56 | +import static java.util.stream.Collectors.toList; |
| 57 | +import static org.mockito.Matchers.anyObject; |
| 58 | +import static org.mockito.Matchers.anyString; |
| 59 | +import static org.mockito.Mockito.mock; |
| 60 | +import static org.mockito.Mockito.when; |
| 61 | + |
| 62 | +public abstract class MapperServiceTestCase extends ESTestCase { |
| 63 | + |
| 64 | + protected static final Settings SETTINGS = Settings.builder().put("index.version.created", Version.CURRENT).build(); |
| 65 | + |
| 66 | + protected Collection<? extends Plugin> getPlugins() { |
| 67 | + return emptyList(); |
| 68 | + } |
| 69 | + |
| 70 | + protected Settings getIndexSettings() { |
| 71 | + return SETTINGS; |
| 72 | + } |
| 73 | + |
| 74 | + protected IndexAnalyzers createIndexAnalyzers(IndexSettings indexSettings) { |
| 75 | + return new IndexAnalyzers( |
| 76 | + Map.of("default", new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer())), |
| 77 | + Map.of(), |
| 78 | + Map.of() |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + protected final String randomIndexOptions() { |
| 83 | + return randomFrom("docs", "freqs", "positions", "offsets"); |
| 84 | + } |
| 85 | + |
| 86 | + protected final DocumentMapper createDocumentMapper(XContentBuilder mappings) throws IOException { |
| 87 | + return createMapperService(mappings).documentMapper(); |
| 88 | + } |
| 89 | + |
| 90 | + protected final MapperService createMapperService(XContentBuilder mappings) throws IOException { |
| 91 | + return createMapperService(getIndexSettings(), mappings); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Create a {@link MapperService} like we would for an index. |
| 96 | + */ |
| 97 | + protected final MapperService createMapperService(Settings settings, XContentBuilder mapping) throws IOException { |
| 98 | + IndexMetadata meta = IndexMetadata.builder("index") |
| 99 | + .settings(Settings.builder().put("index.version.created", Version.CURRENT)) |
| 100 | + .numberOfReplicas(0) |
| 101 | + .numberOfShards(1) |
| 102 | + .build(); |
| 103 | + IndexSettings indexSettings = new IndexSettings(meta, settings); |
| 104 | + MapperRegistry mapperRegistry = new IndicesModule( |
| 105 | + getPlugins().stream().filter(p -> p instanceof MapperPlugin).map(p -> (MapperPlugin) p).collect(toList()) |
| 106 | + ).getMapperRegistry(); |
| 107 | + ScriptService scriptService = new ScriptService(settings, emptyMap(), emptyMap()); |
| 108 | + SimilarityService similarityService = new SimilarityService(indexSettings, scriptService, Map.of()); |
| 109 | + MapperService mapperService = new MapperService( |
| 110 | + indexSettings, |
| 111 | + createIndexAnalyzers(indexSettings), |
| 112 | + xContentRegistry(), |
| 113 | + similarityService, |
| 114 | + mapperRegistry, |
| 115 | + () -> { throw new UnsupportedOperationException(); }, |
| 116 | + () -> true |
| 117 | + ); |
| 118 | + merge(mapperService, mapping); |
| 119 | + return mapperService; |
| 120 | + } |
| 121 | + |
| 122 | + protected final void withLuceneIndex( |
| 123 | + MapperService mapperService, |
| 124 | + CheckedConsumer<RandomIndexWriter, IOException> builder, |
| 125 | + CheckedConsumer<IndexReader, IOException> test |
| 126 | + ) throws IOException { |
| 127 | + try ( |
| 128 | + Directory dir = newDirectory(); |
| 129 | + RandomIndexWriter iw = new RandomIndexWriter(random(), dir, new IndexWriterConfig(mapperService.indexAnalyzer())) |
| 130 | + ) { |
| 131 | + builder.accept(iw); |
| 132 | + try (IndexReader reader = iw.getReader()) { |
| 133 | + test.accept(reader); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + protected final SourceToParse source(CheckedConsumer<XContentBuilder, IOException> build) throws IOException { |
| 139 | + XContentBuilder builder = JsonXContent.contentBuilder().startObject(); |
| 140 | + build.accept(builder); |
| 141 | + builder.endObject(); |
| 142 | + return new SourceToParse("test", "1", BytesReference.bytes(builder), XContentType.JSON); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Merge a new mapping into the one in the provided {@link MapperService}. |
| 147 | + */ |
| 148 | + protected final void merge(MapperService mapperService, XContentBuilder mapping) throws IOException { |
| 149 | + mapperService.merge(null, new CompressedXContent(BytesReference.bytes(mapping)), MapperService.MergeReason.MAPPING_UPDATE); |
| 150 | + } |
| 151 | + |
| 152 | + protected final XContentBuilder mapping(CheckedConsumer<XContentBuilder, IOException> buildFields) throws IOException { |
| 153 | + XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties"); |
| 154 | + buildFields.accept(builder); |
| 155 | + return builder.endObject().endObject().endObject(); |
| 156 | + } |
| 157 | + |
| 158 | + protected final XContentBuilder fieldMapping(CheckedConsumer<XContentBuilder, IOException> buildField) throws IOException { |
| 159 | + return mapping(b -> { |
| 160 | + b.startObject("field"); |
| 161 | + buildField.accept(b); |
| 162 | + b.endObject(); |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + QueryShardContext createQueryShardContext(MapperService mapperService) { |
| 167 | + QueryShardContext queryShardContext = mock(QueryShardContext.class); |
| 168 | + when(queryShardContext.getMapperService()).thenReturn(mapperService); |
| 169 | + when(queryShardContext.fieldMapper(anyString())).thenAnswer(inv -> mapperService.fieldType(inv.getArguments()[0].toString())); |
| 170 | + when(queryShardContext.getIndexAnalyzers()).thenReturn(mapperService.getIndexAnalyzers()); |
| 171 | + when(queryShardContext.getSearchQuoteAnalyzer(anyObject())).thenCallRealMethod(); |
| 172 | + when(queryShardContext.getSearchAnalyzer(anyObject())).thenCallRealMethod(); |
| 173 | + when(queryShardContext.getIndexSettings()).thenReturn(mapperService.getIndexSettings()); |
| 174 | + when(queryShardContext.simpleMatchToIndexNames(anyObject())).thenAnswer( |
| 175 | + inv -> mapperService.simpleMatchToFullName(inv.getArguments()[0].toString()) |
| 176 | + ); |
| 177 | + return queryShardContext; |
| 178 | + } |
| 179 | +} |
0 commit comments