|
9 | 9 | package org.elasticsearch.index.mapper.extras;
|
10 | 10 |
|
11 | 11 | import org.apache.lucene.analysis.TokenStream;
|
| 12 | +import org.apache.lucene.document.FieldType; |
12 | 13 | import org.apache.lucene.index.Term;
|
13 | 14 | import org.apache.lucene.queries.intervals.Intervals;
|
14 | 15 | import org.apache.lucene.queries.intervals.IntervalsSource;
|
|
27 | 28 | import org.apache.lucene.tests.analysis.Token;
|
28 | 29 | import org.apache.lucene.util.BytesRef;
|
29 | 30 | import org.elasticsearch.ElasticsearchException;
|
| 31 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
30 | 32 | import org.elasticsearch.common.lucene.BytesRefs;
|
| 33 | +import org.elasticsearch.common.lucene.Lucene; |
31 | 34 | import org.elasticsearch.common.lucene.search.AutomatonQueries;
|
32 | 35 | import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery;
|
| 36 | +import org.elasticsearch.common.settings.Settings; |
33 | 37 | import org.elasticsearch.common.unit.Fuzziness;
|
| 38 | +import org.elasticsearch.index.IndexMode; |
| 39 | +import org.elasticsearch.index.IndexSettings; |
| 40 | +import org.elasticsearch.index.IndexVersion; |
| 41 | +import org.elasticsearch.index.analysis.NamedAnalyzer; |
| 42 | +import org.elasticsearch.index.mapper.BlockLoader; |
| 43 | +import org.elasticsearch.index.mapper.FieldNamesFieldMapper; |
34 | 44 | import org.elasticsearch.index.mapper.FieldTypeTestCase;
|
| 45 | +import org.elasticsearch.index.mapper.KeywordFieldMapper; |
35 | 46 | import org.elasticsearch.index.mapper.MappedFieldType;
|
| 47 | +import org.elasticsearch.index.mapper.MappingParserContext; |
| 48 | +import org.elasticsearch.index.mapper.TextFieldMapper; |
| 49 | +import org.elasticsearch.index.mapper.TextSearchInfo; |
36 | 50 | import org.elasticsearch.index.mapper.extras.MatchOnlyTextFieldMapper.MatchOnlyTextFieldType;
|
| 51 | +import org.elasticsearch.script.ScriptCompiler; |
37 | 52 | import org.hamcrest.Matchers;
|
38 | 53 |
|
39 | 54 | import java.io.IOException;
|
40 | 55 | import java.util.ArrayList;
|
41 | 56 | import java.util.Arrays;
|
| 57 | +import java.util.Collections; |
42 | 58 | import java.util.List;
|
43 | 59 |
|
| 60 | +import static org.mockito.Mockito.doReturn; |
| 61 | +import static org.mockito.Mockito.mock; |
| 62 | + |
44 | 63 | public class MatchOnlyTextFieldTypeTests extends FieldTypeTestCase {
|
45 | 64 |
|
46 | 65 | public void testTermQuery() {
|
@@ -205,4 +224,149 @@ public void testRangeIntervals() {
|
205 | 224 | ((SourceIntervalsSource) rangeIntervals).getIntervalsSource()
|
206 | 225 | );
|
207 | 226 | }
|
| 227 | + |
| 228 | + public void test_block_loader_uses_stored_fields_for_loading_when_synthetic_source_delegate_is_absent() { |
| 229 | + // given |
| 230 | + MatchOnlyTextFieldMapper.MatchOnlyTextFieldType ft = new MatchOnlyTextFieldMapper.MatchOnlyTextFieldType( |
| 231 | + "parent", |
| 232 | + new TextSearchInfo(TextFieldMapper.Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER), |
| 233 | + mock(NamedAnalyzer.class), |
| 234 | + true, |
| 235 | + Collections.emptyMap(), |
| 236 | + false, |
| 237 | + false, |
| 238 | + null |
| 239 | + ); |
| 240 | + |
| 241 | + // when |
| 242 | + BlockLoader blockLoader = ft.blockLoader(mock(MappedFieldType.BlockLoaderContext.class)); |
| 243 | + |
| 244 | + // then |
| 245 | + // verify that we delegate block loading to the synthetic source delegate |
| 246 | + assertThat(blockLoader, Matchers.instanceOf(MatchOnlyTextFieldType.BytesFromMixedStringsBytesRefBlockLoader.class)); |
| 247 | + } |
| 248 | + |
| 249 | + public void test_block_loader_uses_synthetic_source_delegate_when_ignore_above_is_not_set() { |
| 250 | + // given |
| 251 | + KeywordFieldMapper.KeywordFieldType syntheticSourceDelegate = new KeywordFieldMapper.KeywordFieldType( |
| 252 | + "child", |
| 253 | + true, |
| 254 | + true, |
| 255 | + Collections.emptyMap() |
| 256 | + ); |
| 257 | + |
| 258 | + MatchOnlyTextFieldMapper.MatchOnlyTextFieldType ft = new MatchOnlyTextFieldMapper.MatchOnlyTextFieldType( |
| 259 | + "parent", |
| 260 | + new TextSearchInfo(TextFieldMapper.Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER), |
| 261 | + mock(NamedAnalyzer.class), |
| 262 | + true, |
| 263 | + Collections.emptyMap(), |
| 264 | + false, |
| 265 | + false, |
| 266 | + syntheticSourceDelegate |
| 267 | + ); |
| 268 | + |
| 269 | + // when |
| 270 | + BlockLoader blockLoader = ft.blockLoader(mock(MappedFieldType.BlockLoaderContext.class)); |
| 271 | + |
| 272 | + // then |
| 273 | + // verify that we delegate block loading to the synthetic source delegate |
| 274 | + assertThat(blockLoader, Matchers.instanceOf(BlockLoader.Delegating.class)); |
| 275 | + } |
| 276 | + |
| 277 | + public void test_block_loader_does_not_use_synthetic_source_delegate_when_ignore_above_is_set() { |
| 278 | + // given |
| 279 | + Settings settings = Settings.builder() |
| 280 | + .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) |
| 281 | + .put(IndexSettings.MODE.getKey(), IndexMode.STANDARD) |
| 282 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 283 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) |
| 284 | + .build(); |
| 285 | + IndexSettings indexSettings = new IndexSettings(IndexMetadata.builder("index").settings(settings).build(), settings); |
| 286 | + MappingParserContext mappingParserContext = mock(MappingParserContext.class); |
| 287 | + doReturn(settings).when(mappingParserContext).getSettings(); |
| 288 | + doReturn(indexSettings).when(mappingParserContext).getIndexSettings(); |
| 289 | + doReturn(mock(ScriptCompiler.class)).when(mappingParserContext).scriptCompiler(); |
| 290 | + |
| 291 | + KeywordFieldMapper.Builder builder = new KeywordFieldMapper.Builder("child", mappingParserContext); |
| 292 | + builder.ignoreAbove(123); |
| 293 | + |
| 294 | + KeywordFieldMapper.KeywordFieldType syntheticSourceDelegate = new KeywordFieldMapper.KeywordFieldType( |
| 295 | + "child", |
| 296 | + mock(FieldType.class), |
| 297 | + mock(NamedAnalyzer.class), |
| 298 | + mock(NamedAnalyzer.class), |
| 299 | + mock(NamedAnalyzer.class), |
| 300 | + builder, |
| 301 | + true |
| 302 | + ); |
| 303 | + |
| 304 | + MatchOnlyTextFieldMapper.MatchOnlyTextFieldType ft = new MatchOnlyTextFieldMapper.MatchOnlyTextFieldType( |
| 305 | + "parent", |
| 306 | + new TextSearchInfo(TextFieldMapper.Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER), |
| 307 | + mock(NamedAnalyzer.class), |
| 308 | + true, |
| 309 | + Collections.emptyMap(), |
| 310 | + false, |
| 311 | + false, |
| 312 | + syntheticSourceDelegate |
| 313 | + ); |
| 314 | + |
| 315 | + // when |
| 316 | + MappedFieldType.BlockLoaderContext blContext = mock(MappedFieldType.BlockLoaderContext.class); |
| 317 | + doReturn(FieldNamesFieldMapper.FieldNamesFieldType.get(false)).when(blContext).fieldNames(); |
| 318 | + BlockLoader blockLoader = ft.blockLoader(blContext); |
| 319 | + |
| 320 | + // then |
| 321 | + // verify that we don't delegate anything |
| 322 | + assertThat(blockLoader, Matchers.not(Matchers.instanceOf(BlockLoader.Delegating.class))); |
| 323 | + } |
| 324 | + |
| 325 | + public void test_block_loader_does_not_use_synthetic_source_delegate_when_ignore_above_is_set_at_index_level() { |
| 326 | + // given |
| 327 | + Settings settings = Settings.builder() |
| 328 | + .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) |
| 329 | + .put(IndexSettings.MODE.getKey(), IndexMode.STANDARD) |
| 330 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 331 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) |
| 332 | + .put(IndexSettings.IGNORE_ABOVE_SETTING.getKey(), 123) |
| 333 | + .build(); |
| 334 | + IndexSettings indexSettings = new IndexSettings(IndexMetadata.builder("index").settings(settings).build(), settings); |
| 335 | + MappingParserContext mappingParserContext = mock(MappingParserContext.class); |
| 336 | + doReturn(settings).when(mappingParserContext).getSettings(); |
| 337 | + doReturn(indexSettings).when(mappingParserContext).getIndexSettings(); |
| 338 | + doReturn(mock(ScriptCompiler.class)).when(mappingParserContext).scriptCompiler(); |
| 339 | + |
| 340 | + KeywordFieldMapper.Builder builder = new KeywordFieldMapper.Builder("child", mappingParserContext); |
| 341 | + |
| 342 | + KeywordFieldMapper.KeywordFieldType syntheticSourceDelegate = new KeywordFieldMapper.KeywordFieldType( |
| 343 | + "child", |
| 344 | + mock(FieldType.class), |
| 345 | + mock(NamedAnalyzer.class), |
| 346 | + mock(NamedAnalyzer.class), |
| 347 | + mock(NamedAnalyzer.class), |
| 348 | + builder, |
| 349 | + true |
| 350 | + ); |
| 351 | + |
| 352 | + MatchOnlyTextFieldMapper.MatchOnlyTextFieldType ft = new MatchOnlyTextFieldMapper.MatchOnlyTextFieldType( |
| 353 | + "parent", |
| 354 | + new TextSearchInfo(TextFieldMapper.Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER), |
| 355 | + mock(NamedAnalyzer.class), |
| 356 | + true, |
| 357 | + Collections.emptyMap(), |
| 358 | + false, |
| 359 | + false, |
| 360 | + syntheticSourceDelegate |
| 361 | + ); |
| 362 | + |
| 363 | + // when |
| 364 | + MappedFieldType.BlockLoaderContext blContext = mock(MappedFieldType.BlockLoaderContext.class); |
| 365 | + doReturn(FieldNamesFieldMapper.FieldNamesFieldType.get(false)).when(blContext).fieldNames(); |
| 366 | + BlockLoader blockLoader = ft.blockLoader(blContext); |
| 367 | + |
| 368 | + // then |
| 369 | + // verify that we don't delegate anything |
| 370 | + assertThat(blockLoader, Matchers.not(Matchers.instanceOf(BlockLoader.Delegating.class))); |
| 371 | + } |
208 | 372 | }
|
0 commit comments