|
| 1 | +/* |
| 2 | + * Copyright © 2024 MarkLogic Corporation. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.spark.writer.embedding; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.JsonNode; |
| 7 | +import com.fasterxml.jackson.databind.node.JsonNodeType; |
| 8 | +import com.marklogic.junit5.XmlNode; |
| 9 | +import com.marklogic.spark.AbstractIntegrationTest; |
| 10 | +import com.marklogic.spark.Options; |
| 11 | +import org.apache.spark.sql.DataFrameWriter; |
| 12 | +import org.apache.spark.sql.Row; |
| 13 | +import org.apache.spark.sql.SaveMode; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +/** |
| 22 | + * Verifies that when split text from text documents and then adding embeddings to the sidecar docs, the user doesn't |
| 23 | + * need to specify the location of the chunks. The connector is expected to determine the location based on whether the |
| 24 | + * sidecar docs are JSON or XML. |
| 25 | + */ |
| 26 | +class AddEmbeddingsFromTextTest extends AbstractIntegrationTest { |
| 27 | + |
| 28 | + private static final String TEST_EMBEDDING_FUNCTION_CLASS = "com.marklogic.spark.writer.embedding.MinilmEmbeddingModelFunction"; |
| 29 | + |
| 30 | + @Test |
| 31 | + void jsonSidecarDocuments() { |
| 32 | + prepareToWriteChunks() |
| 33 | + .mode(SaveMode.Append) |
| 34 | + .save(); |
| 35 | + |
| 36 | + List<String> uris = getUrisInCollection("text-chunks", 4); |
| 37 | + for (String uri : uris) { |
| 38 | + assertTrue(uri.endsWith(".json")); |
| 39 | + JsonNode doc = readJsonDocument(uri); |
| 40 | + assertEquals(JsonNodeType.ARRAY, doc.get("chunks").getNodeType()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void xmlSidecarDocuments() { |
| 46 | + prepareToWriteChunks() |
| 47 | + .option(Options.WRITE_SPLITTER_SIDECAR_DOCUMENT_TYPE, "xml") |
| 48 | + .mode(SaveMode.Append) |
| 49 | + .save(); |
| 50 | + |
| 51 | + List<String> uris = getUrisInCollection("text-chunks", 4); |
| 52 | + for (String uri : uris) { |
| 53 | + assertTrue(uri.endsWith(".xml")); |
| 54 | + XmlNode doc = readXmlDocument(uri); |
| 55 | + doc.assertElementCount("/node()/chunks/chunk", 1); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private DataFrameWriter<Row> prepareToWriteChunks() { |
| 60 | + return newSparkSession().read().format(CONNECTOR_IDENTIFIER) |
| 61 | + .option(Options.CLIENT_URI, makeClientUri()) |
| 62 | + .option(Options.READ_DOCUMENTS_URIS, "/marklogic-docs/java-client-intro.txt") |
| 63 | + .load() |
| 64 | + .write().format(CONNECTOR_IDENTIFIER) |
| 65 | + .option(Options.CLIENT_URI, makeClientUri()) |
| 66 | + .option(Options.WRITE_PERMISSIONS, DEFAULT_PERMISSIONS) |
| 67 | + .option(Options.WRITE_URI_PREFIX, "/test") |
| 68 | + .option(Options.WRITE_SPLITTER_TEXT, true) |
| 69 | + .option(Options.WRITE_SPLITTER_MAX_CHUNK_SIZE, 500) |
| 70 | + .option(Options.WRITE_SPLITTER_SIDECAR_MAX_CHUNKS, 1) |
| 71 | + .option(Options.WRITE_SPLITTER_SIDECAR_COLLECTIONS, "text-chunks") |
| 72 | + .option(Options.WRITE_EMBEDDER_MODEL_FUNCTION_CLASS_NAME, TEST_EMBEDDING_FUNCTION_CLASS); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +} |
0 commit comments