|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.neuralsearch.bwc; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import org.opensearch.index.query.MatchQueryBuilder; |
| 14 | +import static org.opensearch.neuralsearch.TestUtils.getModelId; |
| 15 | +import static org.opensearch.neuralsearch.TestUtils.NODES_BWC_CLUSTER; |
| 16 | +import static org.opensearch.neuralsearch.TestUtils.PARAM_NAME_WEIGHTS; |
| 17 | +import static org.opensearch.neuralsearch.TestUtils.TEXT_EMBEDDING_PROCESSOR; |
| 18 | +import static org.opensearch.neuralsearch.TestUtils.DEFAULT_NORMALIZATION_METHOD; |
| 19 | +import static org.opensearch.neuralsearch.TestUtils.DEFAULT_COMBINATION_METHOD; |
| 20 | +import org.opensearch.neuralsearch.query.HybridQueryBuilder; |
| 21 | +import org.opensearch.neuralsearch.query.NeuralQueryBuilder; |
| 22 | + |
| 23 | +public class HybridSearchIT extends AbstractRestartUpgradeRestTestCase { |
| 24 | + private static final String PIPELINE_NAME = "nlp-hybrid-pipeline"; |
| 25 | + private static final String PIPELINE1_NAME = "nlp-hybrid-1-pipeline"; |
| 26 | + private static final String SEARCH_PIPELINE_NAME = "nlp-search-pipeline"; |
| 27 | + private static final String SEARCH_PIPELINE1_NAME = "nlp-search-1-pipeline"; |
| 28 | + private static final String TEST_FIELD = "passage_text"; |
| 29 | + private static final String TEXT_1 = "Hello world"; |
| 30 | + private static final String TEXT_2 = "Hi planet"; |
| 31 | + private static final String TEXT_3 = "Hi earth"; |
| 32 | + private static final String TEXT_4 = "Hi amazon"; |
| 33 | + private static final String TEXT_5 = "Hi mars"; |
| 34 | + private static final String TEXT_6 = "Hi opensearch"; |
| 35 | + private static final String QUERY = "Hi world"; |
| 36 | + |
| 37 | + // Test restart-upgrade normalization processor when index with multiple shards |
| 38 | + // Create Text Embedding Processor, Ingestion Pipeline, add document and search pipeline with normalization processor |
| 39 | + // Validate process , pipeline and document count in restart-upgrade scenario |
| 40 | + public void testNormalizationProcessor_whenIndexWithMultipleShards_E2EFlow() throws Exception { |
| 41 | + validateNormalizationProcessor("processor/IndexMappingMultipleShard.json", PIPELINE_NAME, SEARCH_PIPELINE_NAME); |
| 42 | + } |
| 43 | + |
| 44 | + // Test restart-upgrade normalization processor when index with single shard |
| 45 | + // Create Text Embedding Processor, Ingestion Pipeline, add document and search pipeline with normalization processor |
| 46 | + // Validate process , pipeline and document count in restart-upgrade scenario |
| 47 | + public void testNormalizationProcessor_whenIndexWithSingleShard_E2EFlow() throws Exception { |
| 48 | + validateNormalizationProcessor("processor/IndexMappingSingleShard.json", PIPELINE1_NAME, SEARCH_PIPELINE1_NAME); |
| 49 | + } |
| 50 | + |
| 51 | + private void validateNormalizationProcessor(final String fileName, final String pipelineName, final String searchPipelineName) |
| 52 | + throws Exception { |
| 53 | + waitForClusterHealthGreen(NODES_BWC_CLUSTER); |
| 54 | + if (isRunningAgainstOldCluster()) { |
| 55 | + String modelId = uploadTextEmbeddingModel(); |
| 56 | + loadModel(modelId); |
| 57 | + createPipelineProcessor(modelId, pipelineName); |
| 58 | + createIndexWithConfiguration( |
| 59 | + getIndexNameForTest(), |
| 60 | + Files.readString(Path.of(classLoader.getResource(fileName).toURI())), |
| 61 | + pipelineName |
| 62 | + ); |
| 63 | + addDocuments(getIndexNameForTest(), true); |
| 64 | + createSearchPipeline(searchPipelineName); |
| 65 | + } else { |
| 66 | + String modelId = null; |
| 67 | + try { |
| 68 | + modelId = getModelId(getIngestionPipeline(pipelineName), TEXT_EMBEDDING_PROCESSOR); |
| 69 | + loadModel(modelId); |
| 70 | + addDocuments(getIndexNameForTest(), false); |
| 71 | + validateTestIndex(modelId, getIndexNameForTest(), searchPipelineName); |
| 72 | + } finally { |
| 73 | + wipeOfTestResources(getIndexNameForTest(), pipelineName, modelId, searchPipelineName); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void addDocuments(final String indexName, boolean isRunningAgainstOldCluster) throws IOException { |
| 79 | + if (isRunningAgainstOldCluster) { |
| 80 | + addDocument(indexName, "0", TEST_FIELD, TEXT_1, null, null); |
| 81 | + addDocument(indexName, "1", TEST_FIELD, TEXT_2, null, null); |
| 82 | + addDocument(indexName, "2", TEST_FIELD, TEXT_3, null, null); |
| 83 | + addDocument(indexName, "3", TEST_FIELD, TEXT_4, null, null); |
| 84 | + addDocument(indexName, "4", TEST_FIELD, TEXT_5, null, null); |
| 85 | + } else { |
| 86 | + addDocument(indexName, "5", TEST_FIELD, TEXT_6, null, null); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void createSearchPipeline(final String pipelineName) { |
| 91 | + createSearchPipeline( |
| 92 | + pipelineName, |
| 93 | + DEFAULT_NORMALIZATION_METHOD, |
| 94 | + DEFAULT_COMBINATION_METHOD, |
| 95 | + Map.of(PARAM_NAME_WEIGHTS, Arrays.toString(new float[] { 0.3f, 0.7f })) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + private void validateTestIndex(final String modelId, final String index, final String searchPipeline) throws Exception { |
| 100 | + int docCount = getDocCount(index); |
| 101 | + assertEquals(6, docCount); |
| 102 | + HybridQueryBuilder hybridQueryBuilder = getQueryBuilder(modelId); |
| 103 | + Map<String, Object> searchResponseAsMap = search(index, hybridQueryBuilder, null, 1, Map.of("search_pipeline", searchPipeline)); |
| 104 | + assertNotNull(searchResponseAsMap); |
| 105 | + int hits = getHitCount(searchResponseAsMap); |
| 106 | + assertEquals(1, hits); |
| 107 | + List<Double> scoresList = getNormalizationScoreList(searchResponseAsMap); |
| 108 | + for (Double score : scoresList) { |
| 109 | + assertTrue(0 <= score && score <= 2); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private HybridQueryBuilder getQueryBuilder(final String modelId) { |
| 114 | + NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(); |
| 115 | + neuralQueryBuilder.fieldName("passage_embedding"); |
| 116 | + neuralQueryBuilder.modelId(modelId); |
| 117 | + neuralQueryBuilder.queryText(QUERY); |
| 118 | + neuralQueryBuilder.k(5); |
| 119 | + |
| 120 | + MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("text", QUERY); |
| 121 | + |
| 122 | + HybridQueryBuilder hybridQueryBuilder = new HybridQueryBuilder(); |
| 123 | + hybridQueryBuilder.add(matchQueryBuilder); |
| 124 | + hybridQueryBuilder.add(neuralQueryBuilder); |
| 125 | + |
| 126 | + return hybridQueryBuilder; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments