Skip to content

Commit

Permalink
Combine IT tests to reduce resource use
Browse files Browse the repository at this point in the history
Signed-off-by: Junqiu Lei <[email protected]>
  • Loading branch information
junqiu-lei committed Apr 19, 2024
1 parent ac828d9 commit e37b7f6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,17 @@ private void validateTestIndexOnUpgrade(final int numberOfDocs, final String mod
int docCount = getDocCount(getIndexNameForTest());
assertEquals(numberOfDocs, docCount);
loadModel(modelId);
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder("passage_embedding", text, imageText, modelId, 1, null, null);
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(
"passage_embedding",
text,
imageText,
modelId,
1,
null,
null,
null,
null
);
Map<String, Object> response = search(getIndexNameForTest(), neuralQueryBuilder, 1);
assertNotNull(response);
}
Expand Down
101 changes: 43 additions & 58 deletions src/test/java/org/opensearch/neuralsearch/query/NeuralQueryIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ public void testQueryWithBoostAndImageQuery() {
objectToFloat(firstInnerHitMultimodalQuery.get("_score")),
DELTA_FOR_SCORE_ASSERTION
);

// To save test resources, IT tests for radial search are added below.
// Context: https://github.com/opensearch-project/neural-search/pull/697#discussion_r1571549776

// Test radial search max distance query
NeuralQueryBuilder neuralQueryWithMaxDistanceBuilder = new NeuralQueryBuilder(
TEST_KNN_VECTOR_FIELD_NAME_1,
TEST_QUERY_TEXT,
"",
modelId,
null,
100.0f,
null,
null,
null
);

Map<String, Object> searchResponseAsMap = search(TEST_BASIC_INDEX_NAME, neuralQueryWithMaxDistanceBuilder, 1);
Map<String, Object> firstInnerHit = getFirstInnerHit(searchResponseAsMap);

assertEquals("1", firstInnerHit.get("_id"));
float expectedScoreWithMaxDistanceQuery = computeExpectedScore(modelId, testVector, TEST_SPACE_TYPE, TEST_QUERY_TEXT);
assertEquals(expectedScoreWithMaxDistanceQuery, objectToFloat(firstInnerHit.get("_score")), DELTA_FOR_SCORE_ASSERTION);

// Test radial search min score query
NeuralQueryBuilder neuralQueryWithMinScoreBuilder = new NeuralQueryBuilder(
TEST_KNN_VECTOR_FIELD_NAME_1,
TEST_QUERY_TEXT,
"",
modelId,
null,
null,
0.01f,
null,
null
);

searchResponseAsMap = search(TEST_BASIC_INDEX_NAME, neuralQueryWithMinScoreBuilder, 1);
firstInnerHit = getFirstInnerHit(searchResponseAsMap);

assertEquals("1", firstInnerHit.get("_id"));
float expectedScoreWithMinScoreQuery = computeExpectedScore(modelId, testVector, TEST_SPACE_TYPE, TEST_QUERY_TEXT);
assertEquals(expectedScoreWithMinScoreQuery, objectToFloat(firstInnerHit.get("_score")), DELTA_FOR_SCORE_ASSERTION);
} finally {
wipeOfTestResources(TEST_BASIC_INDEX_NAME, null, modelId, null);
}
Expand Down Expand Up @@ -393,64 +436,6 @@ public void testFilterQuery() {
}
}

@SneakyThrows
public void testQueryWithMaxDistance() {
String modelId = null;
try {
initializeIndexIfNotExist(TEST_BASIC_INDEX_NAME);
modelId = prepareModel();
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(
TEST_KNN_VECTOR_FIELD_NAME_1,
TEST_QUERY_TEXT,
"",
modelId,
null,
100.0f,
null,
null,
null
);

Map<String, Object> searchResponseAsMap = search(TEST_BASIC_INDEX_NAME, neuralQueryBuilder, 1);
Map<String, Object> firstInnerHit = getFirstInnerHit(searchResponseAsMap);

assertEquals("1", firstInnerHit.get("_id"));
float expectedScore = computeExpectedScore(modelId, testVector, TEST_SPACE_TYPE, TEST_QUERY_TEXT);
assertEquals(expectedScore, objectToFloat(firstInnerHit.get("_score")), DELTA_FOR_SCORE_ASSERTION);
} finally {
wipeOfTestResources(TEST_BASIC_INDEX_NAME, null, modelId, null);
}
}

@SneakyThrows
public void testQueryWithMinScore() {
String modelId = null;
try {
initializeIndexIfNotExist(TEST_BASIC_INDEX_NAME);
modelId = prepareModel();
NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(
TEST_KNN_VECTOR_FIELD_NAME_1,
TEST_QUERY_TEXT,
"",
modelId,
null,
null,
0.01f,
null,
null
);

Map<String, Object> searchResponseAsMap = search(TEST_BASIC_INDEX_NAME, neuralQueryBuilder, 1);
Map<String, Object> firstInnerHit = getFirstInnerHit(searchResponseAsMap);

assertEquals("1", firstInnerHit.get("_id"));
float expectedScore = computeExpectedScore(modelId, testVector, TEST_SPACE_TYPE, TEST_QUERY_TEXT);
assertEquals(expectedScore, objectToFloat(firstInnerHit.get("_score")), DELTA_FOR_SCORE_ASSERTION);
} finally {
wipeOfTestResources(TEST_BASIC_INDEX_NAME, null, modelId, null);
}
}

@SneakyThrows
private void initializeIndexIfNotExist(String indexName) {
if (TEST_BASIC_INDEX_NAME.equals(indexName) && !indexExists(TEST_BASIC_INDEX_NAME)) {
Expand Down

0 comments on commit e37b7f6

Please sign in to comment.