-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Gaievski <[email protected]>
- Loading branch information
1 parent
2ae2df8
commit 88b65d9
Showing
4 changed files
with
154 additions
and
13 deletions.
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
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
110 changes: 110 additions & 0 deletions
110
src/test/java/org/opensearch/neuralsearch/processor/TextImageEmbeddingProcessorIT.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,110 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.neuralsearch.processor; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import org.apache.http.HttpHeaders; | ||
import org.apache.http.message.BasicHeader; | ||
import org.apache.http.util.EntityUtils; | ||
import org.junit.After; | ||
import org.opensearch.client.Response; | ||
import org.opensearch.common.xcontent.XContentHelper; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.neuralsearch.common.BaseNeuralSearchIT; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
/** | ||
* Testing text_and_image_embedding ingest processor. We can only test text in integ tests, none of pre-built models | ||
* supports both text and image. | ||
*/ | ||
public class TextImageEmbeddingProcessorIT extends BaseNeuralSearchIT { | ||
|
||
private static final String INDEX_NAME = "text_image_embedding_index"; | ||
private static final String PIPELINE_NAME = "ingest-pipeline"; | ||
|
||
@After | ||
@SneakyThrows | ||
public void tearDown() { | ||
super.tearDown(); | ||
findDeployedModels().forEach(this::deleteModel); | ||
} | ||
|
||
public void testEmbeddingProcessor_whenIngestingDocumentWithSourceMatchingTextMapping_thenSuccessful() throws Exception { | ||
String modelId = uploadModel(); | ||
loadModel(modelId); | ||
createPipelineProcessor(modelId, PIPELINE_NAME, ProcessorType.TEXT_IMAGE_EMBEDDING); | ||
createTextImageEmbeddingIndex(); | ||
ingestDocumentWithTextMappedToEmbeddingField(); | ||
assertEquals(1, getDocCount(INDEX_NAME)); | ||
} | ||
|
||
public void testEmbeddingProcessor_whenIngestingDocumentWithSourceWithoutMatchingInMapping_thenSuccessful() throws Exception { | ||
String modelId = uploadModel(); | ||
loadModel(modelId); | ||
createPipelineProcessor(modelId, PIPELINE_NAME, ProcessorType.TEXT_IMAGE_EMBEDDING); | ||
createTextImageEmbeddingIndex(); | ||
ingestDocumentWithoutMappedFields(); | ||
assertEquals(1, getDocCount(INDEX_NAME)); | ||
} | ||
|
||
private String uploadModel() throws Exception { | ||
String requestBody = Files.readString(Path.of(classLoader.getResource("processor/UploadModelRequestBody.json").toURI())); | ||
return uploadModel(requestBody); | ||
} | ||
|
||
private void createTextImageEmbeddingIndex() throws Exception { | ||
createIndexWithConfiguration( | ||
INDEX_NAME, | ||
Files.readString(Path.of(classLoader.getResource("processor/IndexMappings.json").toURI())), | ||
PIPELINE_NAME | ||
); | ||
} | ||
|
||
private void ingestDocumentWithTextMappedToEmbeddingField() throws Exception { | ||
String ingestDocumentBody = "{\n" | ||
+ " \"title\": \"This is a good day\",\n" | ||
+ " \"description\": \"daily logging\",\n" | ||
+ " \"passage_text\": \"A very nice day today\",\n" | ||
+ " \"favorites\": {\n" | ||
+ " \"game\": \"overwatch\",\n" | ||
+ " \"movie\": null\n" | ||
+ " }\n" | ||
+ "}\n"; | ||
ingestDocument(ingestDocumentBody); | ||
} | ||
|
||
private void ingestDocumentWithoutMappedFields() throws Exception { | ||
String ingestDocumentBody = "{\n" | ||
+ " \"title\": \"This is a good day\",\n" | ||
+ " \"description\": \"daily logging\",\n" | ||
+ " \"some_random_field\": \"Today is a sunny weather\"\n" | ||
+ "}\n"; | ||
ingestDocument(ingestDocumentBody); | ||
} | ||
|
||
private void ingestDocument(final String ingestDocument) throws Exception { | ||
Response response = makeRequest( | ||
client(), | ||
"POST", | ||
INDEX_NAME + "/_doc?refresh", | ||
null, | ||
toHttpEntity(ingestDocument), | ||
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana")) | ||
); | ||
Map<String, Object> map = XContentHelper.convertToMap( | ||
XContentType.JSON.xContent(), | ||
EntityUtils.toString(response.getEntity()), | ||
false | ||
); | ||
assertEquals("created", map.get("result")); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/resources/processor/PipelineForTextImageEmbeddingProcessorConfiguration.json
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,15 @@ | ||
{ | ||
"description": "text image embedding pipeline", | ||
"processors": [ | ||
{ | ||
"text_image_embedding": { | ||
"model_id": "%s", | ||
"embedding": "passage_embedding", | ||
"field_map": { | ||
"text": "passage_text", | ||
"image": "passage_image" | ||
} | ||
} | ||
} | ||
] | ||
} |