Skip to content

Commit

Permalink
add ut it
Browse files Browse the repository at this point in the history
Signed-off-by: zhichao-aws <[email protected]>
  • Loading branch information
zhichao-aws committed Feb 29, 2024
1 parent 146e990 commit c40c80d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.opensearch.neuralsearch.TestUtils.createRandomVector;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.hc.core5.http.io.entity.EntityUtils;
Expand All @@ -26,13 +27,16 @@
import com.google.common.primitives.Floats;

import lombok.SneakyThrows;
import org.opensearch.neuralsearch.query.NeuralSparseQueryBuilder;

public class NeuralQueryEnricherProcessorIT extends BaseNeuralSearchIT {

private static final String index = "my-nlp-index";
private static final String sparseIndex = "my-nlp-index-sparse";
private static final String search_pipeline = "search-pipeline";
private static final String ingest_pipeline = "nlp-pipeline";
private static final String TEST_KNN_VECTOR_FIELD_NAME_1 = "test-knn-vector-1";
private static final String TEST_RANK_FEATURES_FIELD_NAME_1 = "test-rank-features-1";
private final float[] testVector = createRandomVector(TEST_DIMENSION);

@Before
Expand Down Expand Up @@ -61,6 +65,25 @@ public void testNeuralQueryEnricherProcessor_whenNoModelIdPassed_thenSuccess() {
}
}

@SneakyThrows
public void testNeuralQueryEnricherProcessor_whenNoModelIdPassedInNeuralSparseQuery_thenSuccess() {
String modelId = null;
try {
initializeIndexIfNotExist(sparseIndex);
modelId = prepareSparseEncodingModel();
createSearchRequestProcessor(modelId, search_pipeline);
createPipelineProcessor(modelId, ingest_pipeline, ProcessorType.TEXT_EMBEDDING);
updateIndexSettings(sparseIndex, Settings.builder().put("index.search.default_pipeline", search_pipeline));
NeuralSparseQueryBuilder neuralSparseQueryBuilder = new NeuralSparseQueryBuilder();
neuralSparseQueryBuilder.fieldName(TEST_RANK_FEATURES_FIELD_NAME_1);
neuralSparseQueryBuilder.queryText("hello");
Map<String, Object> response = search(sparseIndex, neuralSparseQueryBuilder, 2);
assertFalse(response.isEmpty());
} finally {
wipeOfTestResources(sparseIndex, ingest_pipeline, modelId, search_pipeline);
}
}

@SneakyThrows
public void testNeuralQueryEnricherProcessor_whenGetEmptyQueryBody_thenSuccess() {
String modelId = null;
Expand Down Expand Up @@ -120,5 +143,11 @@ private void initializeIndexIfNotExist(String indexName) {
);
assertEquals(1, getDocCount(indexName));
}

if (sparseIndex.equals(indexName) && !indexExists(indexName)) {
prepareSparseEncodingIndex(indexName, List.of(TEST_RANK_FEATURES_FIELD_NAME_1));
addSparseEncodingDoc(indexName, "1", List.of(TEST_RANK_FEATURES_FIELD_NAME_1), List.of(Map.of("hi", 1.0f, "hello", 1.1f)));
assertEquals(1, getDocCount(indexName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public void testAccept_whenNeuralQueryBuilderWithoutFieldModelId_thenSetFieldMod
assertEquals("bdcvjkcdjvkddcjxdjsc", neuralQueryBuilder.modelId());
}

public void testAccept_whenNeuralSparseQueryBuilderWithModelId_theDoNothing() {
String modelId1 = "bdcvjkcdjvkddcjxdjsc";
String modelId2 = "45dfsnfoiqwrjcjxdjsc";
NeuralSparseQueryBuilder neuralSparseQueryBuilder = new NeuralSparseQueryBuilder();
neuralSparseQueryBuilder.fieldName("passage_text");
neuralSparseQueryBuilder.modelId(modelId1);

NeuralSearchQueryVisitor neuralSearchQueryVisitor = new NeuralSearchQueryVisitor(modelId2, null);
neuralSearchQueryVisitor.accept(neuralSparseQueryBuilder);

assertEquals(modelId1, neuralSparseQueryBuilder.modelId());
}

public void testAccept_whenNeuralSparseQueryBuilderWithoutModelId_thenSetModelId() {
String modelId = "bdcvjkcdjvkddcjxdjsc";
NeuralSparseQueryBuilder neuralSparseQueryBuilder = new NeuralSparseQueryBuilder();
Expand Down

0 comments on commit c40c80d

Please sign in to comment.