You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2026/03/31 13:24:45 UTC
(jackrabbit-oak) branch OAK-12060 updated: OAK-12060: Regression test for knn elastic query (#2790)
This is an automated email from the ASF dual-hosted git repository.
thomasm pushed a commit to branch OAK-12060
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/OAK-12060 by this push:
new 8b4d556b93 OAK-12060: Regression test for knn elastic query (#2790)
8b4d556b93 is described below
commit 8b4d556b931a5b1d2fb8eef542f8b38f342cf5e6
Author: Marvin <95...@users.noreply.github.com>
AuthorDate: Tue Mar 31 15:24:37 2026 +0200
OAK-12060: Regression test for knn elastic query (#2790)
Co-authored-by: marvinw <ma...@adobe.com>
---
.../inference/ElasticInferenceUsingConfigTest.java | 112 +++++++++++++++++++++
.../expected/semantic_search_with_facets.json | 102 +++++++++++++++++++
2 files changed, 214 insertions(+)
diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/ElasticInferenceUsingConfigTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/ElasticInferenceUsingConfigTest.java
index 13b0c3f1b5..d509271dd1 100644
--- a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/ElasticInferenceUsingConfigTest.java
+++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/ElasticInferenceUsingConfigTest.java
@@ -79,6 +79,7 @@ import java.util.stream.Stream;
import static org.apache.jackrabbit.oak.plugins.index.elastic.query.inference.InferenceConstants.ENRICHER_CONFIG;
import static org.apache.jackrabbit.oak.plugins.index.elastic.query.inference.InferenceConstants.TYPE;
+import static org.apache.jackrabbit.oak.plugins.index.elastic.query.inference.InferenceConstants.VECTOR_SPACES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -1088,4 +1089,115 @@ public class ElasticInferenceUsingConfigTest extends ElasticAbstractQueryTest {
});
}
+
+ @Test
+ public void testElasticKnnQueryForRegression() throws Exception {
+ String jcrIndexName = UUID.randomUUID().toString();
+ String inferenceServiceUrl = "http://localhost:" + wireMock.port() + "/v1/embeddings";
+ String inferenceModelConfigName = "inferenceConfig1";
+ String inferenceModelName = "paraphrase-multilingual-MiniLM-L12-v2";
+ double similarityThreshold = 0.5;
+
+ createInferenceConfig(jcrIndexName, true, defaultEnricherConfig, inferenceModelConfigName,
+ inferenceModelName, inferenceServiceUrl, similarityThreshold, 1L, true, true);
+ setupEnricherStatus(defaultEnricherStatusMapping, defaultEnricherStatusData);
+
+ IndexDefinitionBuilder builder = createIndex();
+ builder.includedPaths("/content/assets");
+ IndexDefinitionBuilder.IndexRule indexRule = builder.indexRule("nt:base");
+ indexRule.property("jcr:content/metadata/dc:format").propertyIndex().analyzed().nodeScopeIndex().facets();
+ indexRule.property("jcr:content/metadata/status").propertyIndex().analyzed().nodeScopeIndex().facets();
+ builder.getBuilderTree().addChild("facets").setProperty("secure", "insecure");
+
+ Tree index = setIndex(jcrIndexName, builder);
+ root.commit();
+
+ Tree content = root.getTree("/").addChild("content").addChild("assets");
+ Tree asset = content.addChild("photo1.jpg");
+ Tree metadata = asset.addChild("jcr:content").addChild("metadata");
+ metadata.setProperty("dc:format", "image/jpeg");
+ metadata.setProperty("status", "approved");
+ root.commit();
+
+ List<Float> embeddings = List.of(0.1f, 0.2f, 0.3f);
+ createDocumentWithEmbeddings(
+ index, "/content/assets/photo1.jpg", inferenceModelConfigName, inferenceModelName, embeddings);
+
+ assertEventually(() -> {
+ assertTrue(countDocuments(index) > 0);
+ ObjectNode doc = getDocument(index, "/content/assets/photo1.jpg");
+ assertNotNull(doc.get(VECTOR_SPACES));
+ assertNotNull(doc.get(VECTOR_SPACES).get(inferenceModelConfigName));
+ });
+
+ ObjectNode embeddingResponse = MAPPER.createObjectNode();
+ embeddingResponse.put("object", "list");
+ ArrayNode data = embeddingResponse.putArray("data");
+ ObjectNode embeddingObj = data.addObject();
+ embeddingObj.put("object", "embedding");
+ ArrayNode embeddingArray = embeddingObj.putArray("embedding");
+ embeddings.forEach(embeddingArray::add);
+ embeddingObj.put("index", 0);
+
+ wireMock.stubFor(WireMock.post("/v1/embeddings")
+ .willReturn(WireMock.ok()
+ .withHeader("Content-Type", "application/json")
+ .withBody(MAPPER.writeValueAsString(embeddingResponse))));
+
+ String sql2Query = "select [jcr:path], [rep:facet(jcr:content/metadata/dc:format)], " +
+ "[rep:facet(jcr:content/metadata/status)] from [nt:base] where " +
+ "ISDESCENDANTNODE('/content/assets') and contains(*, '?{}?find similar images')";
+ String explainResult = explain(sql2Query, SQL2);
+ assertNotNull(explainResult);
+
+ // Get ES request from the plan explanation
+ String jsonBody = extractSearchRequestJsonBody(explainResult);
+ URL expectedJsonUrl = ElasticInferenceUsingConfigTest.class.getResource(
+ "/inferenceUsingConfig/expected/semantic_search_with_facets.json");
+ assertNotNull(expectedJsonUrl);
+ String expectedSearchRequestJson = IOUtils.toString(expectedJsonUrl, StandardCharsets.UTF_8);
+ JsonNode actualJson = MAPPER.readTree(jsonBody);
+ JsonNode expectedJson = MAPPER.readTree(expectedSearchRequestJson);
+
+ // Check elastic query matches exactly the expected query
+ assertJsonEquals("SearchRequest", expectedJson, actualJson, 1e-6);
+ }
+
+ private String extractSearchRequestJsonBody(String explainResult) {
+ final String token = "SearchRequest";
+ int tokenIdx = explainResult.indexOf(token);
+ assertTrue("Explain output should contain " + token, tokenIdx >= 0);
+ int jsonStart = explainResult.indexOf('{', tokenIdx);
+ assertTrue("Explain output should contain JSON body after " + token, jsonStart >= 0);
+ String jsonBody = explainResult.substring(jsonStart);
+ int newlineIdx = jsonBody.indexOf('\n');
+ if (newlineIdx > 0) {
+ jsonBody = jsonBody.substring(0, newlineIdx).trim();
+ }
+ return jsonBody;
+ }
+
+ /**
+ * assertEquals for Json, but with a delta tolerance for floats.
+ */
+ private void assertJsonEquals(String path, JsonNode expected, JsonNode actual, double tolerance) {
+ assertEquals(path + " - node type mismatch", expected.getNodeType(), actual.getNodeType());
+ if (expected.isObject()) {
+ expected.fieldNames().forEachRemaining(field ->
+ assertTrue(path + " - missing field: " + field, actual.has(field)));
+ actual.fieldNames().forEachRemaining(field ->
+ assertTrue(path + " - unexpected field: " + field, expected.has(field)));
+ expected.fieldNames().forEachRemaining(field ->
+ assertJsonEquals(path + "." + field, expected.get(field), actual.get(field), tolerance));
+ } else if (expected.isArray()) {
+ assertEquals(path + " - array size", expected.size(), actual.size());
+ for (int i = 0; i < expected.size(); i++) {
+ assertJsonEquals(path + "[" + i + "]", expected.get(i), actual.get(i), tolerance);
+ }
+ } else if (expected.isNumber()) {
+ assertEquals(path, expected.doubleValue(), actual.doubleValue(), tolerance);
+ } else {
+ assertEquals(path, expected, actual);
+ }
+ }
}
diff --git a/oak-search-elastic/src/test/resources/inferenceUsingConfig/expected/semantic_search_with_facets.json b/oak-search-elastic/src/test/resources/inferenceUsingConfig/expected/semantic_search_with_facets.json
new file mode 100644
index 0000000000..e9470cc287
--- /dev/null
+++ b/oak-search-elastic/src/test/resources/inferenceUsingConfig/expected/semantic_search_with_facets.json
@@ -0,0 +1,102 @@
+{
+ "_source": {
+ "includes": [":path"]
+ },
+ "aggregations": {
+ "jcr:content/metadata/dc:format": {
+ "terms": {
+ "field": "jcr:content/metadata/dc:format.keyword",
+ "size": 10
+ }
+ },
+ "jcr:content/metadata/status": {
+ "terms": {
+ "field": "jcr:content/metadata/status.keyword",
+ "size": 10
+ }
+ }
+ },
+ "query": {
+ "bool": {
+ "filter": [
+ {
+ "term": {
+ ":ancestors": {
+ "value": "/content/assets"
+ }
+ }
+ }
+ ],
+ "must": [
+ {
+ "bool": {
+ "must": [
+ {
+ "bool": {
+ "should": [
+ {
+ "nested": {
+ "path": ":vectorSpaces.inferenceConfig1",
+ "query": {
+ "knn": {
+ "field": ":vectorSpaces.inferenceConfig1.vector",
+ "query_vector": [
+ 0.1,
+ 0.2,
+ 0.3
+ ],
+ "num_candidates": 100,
+ "k": 100,
+ "filter": [
+ {
+ "term": {
+ ":ancestors": {
+ "value": "/content/assets"
+ }
+ }
+ }
+ ],
+ "similarity": 0.5
+ }
+ }
+ }
+ },
+ {
+ "query_string": {
+ "boost": 0.066666667,
+ "default_operator": "and",
+ "fields": [
+ "jcr:content/metadata/dc:format^1.0",
+ "jcr:content/metadata/status^1.0",
+ ":fulltext"
+ ],
+ "lenient": true,
+ "query": "find similar images",
+ "tie_breaker": 0.5,
+ "type": "cross_fields"
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+ },
+ "size": 10,
+ "sort": [
+ {
+ "_score": {
+ "order": "desc"
+ }
+ },
+ {
+ ":path": {
+ "order": "asc"
+ }
+ }
+ ],
+ "track_total_hits": 10000
+}