|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.neuralsearch.mapper; |
| 6 | + |
| 7 | +import lombok.NonNull; |
| 8 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 9 | +import org.opensearch.common.settings.Settings; |
| 10 | +import org.opensearch.index.analysis.AnalyzerScope; |
| 11 | +import org.opensearch.index.analysis.IndexAnalyzers; |
| 12 | +import org.opensearch.index.analysis.NamedAnalyzer; |
| 13 | +import org.opensearch.index.mapper.ContentPath; |
| 14 | +import org.opensearch.index.mapper.Mapper; |
| 15 | +import org.opensearch.index.mapper.ParametrizedFieldMapper; |
| 16 | +import org.opensearch.index.mapper.TextFieldMapper; |
| 17 | + |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static java.util.Collections.emptyMap; |
| 22 | +import static java.util.Collections.singletonMap; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | +import static org.opensearch.Version.CURRENT; |
| 25 | +import static org.opensearch.knn.index.KNNSettings.KNN_INDEX; |
| 26 | +import static org.opensearch.neuralsearch.constants.MappingConstants.TYPE; |
| 27 | +import static org.opensearch.neuralsearch.constants.SemanticFieldConstants.MODEL_ID; |
| 28 | +import static org.opensearch.neuralsearch.constants.SemanticFieldConstants.RAW_FIELD_TYPE; |
| 29 | +import static org.opensearch.neuralsearch.constants.SemanticFieldConstants.SEARCH_MODEL_ID; |
| 30 | +import static org.opensearch.neuralsearch.constants.SemanticFieldConstants.SEMANTIC_INFO_FIELD_NAME; |
| 31 | +import static org.opensearch.test.OpenSearchTestCase.settings; |
| 32 | + |
| 33 | +public class SemanticFieldMapperTestUtil { |
| 34 | + public static final String fieldName = "testField"; |
| 35 | + public static final String modelId = "modelId"; |
| 36 | + public static final String searchModelId = "searchModelId"; |
| 37 | + public static final String semanticInfoFieldName = "semanticInfoFieldName"; |
| 38 | + |
| 39 | + public static final SemanticFieldMapper.TypeParser TYPE_PARSER = new SemanticFieldMapper.TypeParser(); |
| 40 | + private static final IndexAnalyzers indexAnalyzers = new IndexAnalyzers( |
| 41 | + singletonMap("default", new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer())), |
| 42 | + emptyMap(), |
| 43 | + emptyMap() |
| 44 | + ); |
| 45 | + |
| 46 | + public static Map<String, Object> createFieldConfig(final String rawFieldType) { |
| 47 | + Map<String, Object> node = new HashMap<>(); |
| 48 | + node.put(TYPE, SemanticFieldMapper.CONTENT_TYPE); |
| 49 | + node.put(RAW_FIELD_TYPE, rawFieldType); |
| 50 | + node.put(MODEL_ID, modelId); |
| 51 | + node.put(SEARCH_MODEL_ID, searchModelId); |
| 52 | + node.put(SEMANTIC_INFO_FIELD_NAME, semanticInfoFieldName); |
| 53 | + return node; |
| 54 | + } |
| 55 | + |
| 56 | + public static void mockParserContext(@NonNull final String rawFieldType, @NonNull final ParametrizedFieldMapper.TypeParser parser, @NonNull final Mapper.TypeParser.ParserContext parserContext) { |
| 57 | + when(parserContext.typeParser(rawFieldType)).thenReturn(parser); |
| 58 | + when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers); |
| 59 | + when(parserContext.indexVersionCreated()).thenReturn(CURRENT); |
| 60 | + } |
| 61 | + |
| 62 | + public static SemanticFieldMapper buildSemanticFieldMapperWithTextAsRawFieldType( |
| 63 | + final @NonNull Mapper.TypeParser.ParserContext parserContext |
| 64 | + ) { |
| 65 | + final Map<String, Object> node = createFieldConfig(TextFieldMapper.CONTENT_TYPE); |
| 66 | + return buildSemanticFieldMapperWithTextAsRawFieldType(node, parserContext); |
| 67 | + } |
| 68 | + |
| 69 | + public static SemanticFieldMapper buildSemanticFieldMapperWithTextAsRawFieldType( |
| 70 | + final @NonNull Map<String, Object> fieldConfig, |
| 71 | + final @NonNull Mapper.TypeParser.ParserContext parserContext |
| 72 | + ) { |
| 73 | + return buildSemanticFieldMapper(fieldConfig, TextFieldMapper.CONTENT_TYPE, TextFieldMapper.PARSER, parserContext); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + public static SemanticFieldMapper buildSemanticFieldMapper( |
| 78 | + final @NonNull String rawFieldType, |
| 79 | + final @NonNull ParametrizedFieldMapper.TypeParser typeParser, |
| 80 | + final @NonNull Mapper.TypeParser.ParserContext parserContext |
| 81 | + ) { |
| 82 | + final Map<String, Object> fieldConfig = createFieldConfig(rawFieldType); |
| 83 | + return buildSemanticFieldMapper(fieldConfig, rawFieldType, typeParser, parserContext); |
| 84 | + } |
| 85 | + |
| 86 | + public static SemanticFieldMapper buildSemanticFieldMapper( |
| 87 | + final @NonNull Map<String, Object> fieldConfig, |
| 88 | + final @NonNull String rawFieldType, |
| 89 | + final @NonNull ParametrizedFieldMapper.TypeParser typeParser, |
| 90 | + final @NonNull Mapper.TypeParser.ParserContext parserContext |
| 91 | + ) { |
| 92 | + mockParserContext(rawFieldType, typeParser, parserContext); |
| 93 | + |
| 94 | + final SemanticFieldMapper.Builder builder = TYPE_PARSER.parse(SemanticFieldMapperTestUtil.fieldName, fieldConfig, parserContext); |
| 95 | + |
| 96 | + final Settings settings = Settings.builder().put(settings(CURRENT).build()).put(KNN_INDEX, true).build(); |
| 97 | + final ParametrizedFieldMapper.BuilderContext builderContext = new ParametrizedFieldMapper.BuilderContext( |
| 98 | + settings, |
| 99 | + new ContentPath() |
| 100 | + ); |
| 101 | + |
| 102 | + return builder.build(builderContext); |
| 103 | + } |
| 104 | +} |
0 commit comments