-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add hybrid search with rescore IT #1066
Merged
heemin32
merged 6 commits into
opensearch-project:main
from
will-hwang:add_hybrid_query_sort_with_rescore
Jan 8, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7674f93
add hybrid search with rescore IT
will-hwang 77a55cb
remove rescore in hybrid search IT
will-hwang 2909bac
remove previous version checks in build file
will-hwang fea66a9
removing version checks only in rolling upgrade tests
will-hwang a0ea3f1
remove newly added tests in restart test
will-hwang 017e2c7
Revert "remove newly added tests in restart test"
will-hwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,7 @@ task testAgainstOldCluster(type: StandaloneRestIntegTestTask) { | |
if (versionsBelow2_15.any { ext.neural_search_bwc_version.startsWith(it) }){ | ||
filter { | ||
excludeTestsMatching "org.opensearch.neuralsearch.bwc.NeuralSparseTwoPhaseProcessorIT.*" | ||
excludeTestsMatching "org.opensearch.neuralsearch.bwc.HybridSearchWithRescoreIT.*" | ||
} | ||
} | ||
|
||
|
@@ -166,6 +167,7 @@ task testAgainstNewCluster(type: StandaloneRestIntegTestTask) { | |
if (versionsBelow2_15.any { ext.neural_search_bwc_version.startsWith(it) }){ | ||
filter { | ||
excludeTestsMatching "org.opensearch.neuralsearch.bwc.NeuralSparseTwoPhaseProcessorIT.*" | ||
excludeTestsMatching "org.opensearch.neuralsearch.bwc.HybridSearchWithRescoreIT.*" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
} | ||
} | ||
|
||
|
117 changes: 117 additions & 0 deletions
117
...tart-upgrade/src/test/java/org/opensearch/neuralsearch/bwc/HybridSearchWithRescoreIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.neuralsearch.bwc; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static org.opensearch.neuralsearch.util.TestUtils.DEFAULT_COMBINATION_METHOD; | ||
import static org.opensearch.neuralsearch.util.TestUtils.DEFAULT_NORMALIZATION_METHOD; | ||
import static org.opensearch.neuralsearch.util.TestUtils.NODES_BWC_CLUSTER; | ||
import static org.opensearch.neuralsearch.util.TestUtils.PARAM_NAME_WEIGHTS; | ||
import static org.opensearch.neuralsearch.util.TestUtils.TEXT_EMBEDDING_PROCESSOR; | ||
import static org.opensearch.neuralsearch.util.TestUtils.getModelId; | ||
|
||
import org.opensearch.index.query.MatchQueryBuilder; | ||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.knn.index.query.rescore.RescoreContext; | ||
import org.opensearch.neuralsearch.query.HybridQueryBuilder; | ||
import org.opensearch.neuralsearch.query.NeuralQueryBuilder; | ||
|
||
public class HybridSearchWithRescoreIT extends AbstractRestartUpgradeRestTestCase { | ||
private static final String PIPELINE_NAME = "nlp-hybrid-with-rescore-pipeline"; | ||
private static final String SEARCH_PIPELINE_NAME = "nlp-search-with_rescore-pipeline"; | ||
private static final String TEST_FIELD = "passage_text"; | ||
private static final String TEXT = "Hello world"; | ||
private static final String TEXT_UPGRADED = "Hi earth"; | ||
private static final String QUERY = "Hi world"; | ||
private static final int NUM_DOCS_PER_ROUND = 1; | ||
private static final String VECTOR_EMBEDDING_FIELD = "passage_embedding"; | ||
protected static final String RESCORE_QUERY = "hi"; | ||
|
||
/** | ||
* Test normalization with hybrid query and rescore. This test is required as rescore will not be compatible with version lower than 2.15 | ||
*/ | ||
public void testHybridQueryWithRescore_whenIndexWithMultipleShards_E2EFlow() throws Exception { | ||
waitForClusterHealthGreen(NODES_BWC_CLUSTER); | ||
|
||
if (isRunningAgainstOldCluster()) { | ||
String modelId = uploadTextEmbeddingModel(); | ||
loadModel(modelId); | ||
createPipelineProcessor(modelId, PIPELINE_NAME); | ||
createIndexWithConfiguration( | ||
getIndexNameForTest(), | ||
Files.readString(Path.of(classLoader.getResource("processor/IndexMappingMultipleShard.json").toURI())), | ||
PIPELINE_NAME | ||
); | ||
addDocument(getIndexNameForTest(), "0", TEST_FIELD, TEXT, null, null); | ||
createSearchPipeline( | ||
SEARCH_PIPELINE_NAME, | ||
DEFAULT_NORMALIZATION_METHOD, | ||
DEFAULT_COMBINATION_METHOD, | ||
Map.of(PARAM_NAME_WEIGHTS, Arrays.toString(new float[] { 0.3f, 0.7f })) | ||
); | ||
} else { | ||
String modelId = null; | ||
try { | ||
modelId = getModelId(getIngestionPipeline(PIPELINE_NAME), TEXT_EMBEDDING_PROCESSOR); | ||
loadModel(modelId); | ||
addDocument(getIndexNameForTest(), "1", TEST_FIELD, TEXT_UPGRADED, null, null); | ||
HybridQueryBuilder hybridQueryBuilder = getQueryBuilder(modelId, null, null); | ||
QueryBuilder rescorer = QueryBuilders.matchQuery(TEST_FIELD, RESCORE_QUERY).boost(0.3f); | ||
validateTestIndex(getIndexNameForTest(), hybridQueryBuilder, rescorer); | ||
hybridQueryBuilder = getQueryBuilder(modelId, Map.of("ef_search", 100), RescoreContext.getDefault()); | ||
validateTestIndex(getIndexNameForTest(), hybridQueryBuilder, rescorer); | ||
} finally { | ||
wipeOfTestResources(getIndexNameForTest(), PIPELINE_NAME, modelId, null); | ||
} | ||
} | ||
} | ||
|
||
private void validateTestIndex(final String index, HybridQueryBuilder queryBuilder, QueryBuilder rescorer) { | ||
int docCount = getDocCount(index); | ||
assertEquals(2, docCount); | ||
Map<String, Object> searchResponseAsMap = search(index, queryBuilder, rescorer, 1, Map.of("search_pipeline", SEARCH_PIPELINE_NAME)); | ||
assertNotNull(searchResponseAsMap); | ||
int hits = getHitCount(searchResponseAsMap); | ||
assertEquals(1, hits); | ||
List<Double> scoresList = getNormalizationScoreList(searchResponseAsMap); | ||
for (Double score : scoresList) { | ||
assertTrue(0 <= score && score <= 2); | ||
} | ||
} | ||
|
||
private HybridQueryBuilder getQueryBuilder( | ||
final String modelId, | ||
final Map<String, ?> methodParameters, | ||
final RescoreContext rescoreContextForNeuralQuery | ||
) { | ||
NeuralQueryBuilder neuralQueryBuilder = NeuralQueryBuilder.builder() | ||
.fieldName(VECTOR_EMBEDDING_FIELD) | ||
.modelId(modelId) | ||
.queryText(QUERY) | ||
.k(5) | ||
.build(); | ||
if (methodParameters != null) { | ||
neuralQueryBuilder.methodParameters(methodParameters); | ||
} | ||
if (Objects.nonNull(rescoreContextForNeuralQuery)) { | ||
neuralQueryBuilder.rescoreContext(rescoreContextForNeuralQuery); | ||
} | ||
|
||
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("text", QUERY); | ||
|
||
HybridQueryBuilder hybridQueryBuilder = new HybridQueryBuilder(); | ||
hybridQueryBuilder.add(matchQueryBuilder); | ||
hybridQueryBuilder.add(neuralQueryBuilder); | ||
|
||
return hybridQueryBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this check is not needed in main, latest 2.x which is 2.18 already supports rescore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to keep it so that the code looks same between
main
and2.x