Skip to content

Commit 6fa9f0c

Browse files
committed
OAK-11719: vector query should not break normal queries
1 parent 0992d27 commit 6fa9f0c

File tree

6 files changed

+73
-36
lines changed

6 files changed

+73
-36
lines changed

oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/FullTextSearchImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ public FullTextExpression getFullTextConstraint(SelectorImpl s) {
144144
// e.g. ?{"inferenceModelConfig": "ada-test-model"}?little red fox
145145
// So here we split the query into text part of query and inferenceConfig part of query.
146146
// Afterwards we only parse text part of query as this part of query is what we want to search.
147-
if (query.getSettings().isInferenceEnabled()) {
148-
VectorQuery vectorQuery = new VectorQuery(rawText);
149-
queryText = vectorQuery.getQueryText();
150-
}
147+
// We are explicitly removing vectorQueryConfig from raw query
148+
VectorQuery vectorQuery = new VectorQuery(rawText);
149+
queryText = vectorQuery.getQueryText();
151150
FullTextExpression e = FullTextParser.parse(p2, queryText);
152151
return new FullTextContains(p2, rawText, e);
153152
} catch (ParseException e) {

oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction;
8484
import org.apache.jackrabbit.oak.spi.query.QueryConstants;
8585
import org.apache.jackrabbit.oak.spi.query.QueryLimits;
86+
import org.apache.jackrabbit.oak.spi.query.fulltext.VectorQuery;
8687
import org.apache.jackrabbit.oak.spi.state.NodeState;
8788
import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
8889
import org.apache.lucene.analysis.Analyzer;
@@ -1477,6 +1478,14 @@ public boolean visit(FullTextTerm term) {
14771478

14781479
private boolean visitTerm(String propertyName, String text, String boost, boolean not) {
14791480
String p = getLuceneFieldName(propertyName, pr);
1481+
// below condition is for contains(*, 'xyz')
1482+
if (propertyName == null) {
1483+
// Lucene indexes don't support inference, so we should remove queryInferenceConfig
1484+
// from query before evaluating it.
1485+
VectorQuery vectorQuery = new VectorQuery(text);
1486+
text = vectorQuery.getQueryText();
1487+
}
1488+
14801489
Query q = tokenToQuery(text, p, pr, analyzer, augmentor);
14811490
if (q == null) {
14821491
return false;

oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneFullTextIndexTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ protected void createTestIndexNode() {
4545
setTraversalEnabled(false);
4646
}
4747

48+
@Override
49+
protected String[] getPrefixes() {
50+
return new String[]{"", "?{}?", "?"};
51+
}
52+
4853
@After
4954
public void shutdownExecutor() {
5055
executorService.shutdown();

oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneTestRepositoryBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public LuceneTestRepositoryBuilder(ExecutorService executorService, TemporaryFol
5858

5959
resultCountingIndexProvider = new ResultCountingIndexProvider(indexProvider);
6060
queryEngineSettings = new QueryEngineSettings();
61+
// enabling inference to check impact on all tests.
62+
queryEngineSettings.setInferenceEnabled(true);
6163
optionalEditorProvider = new TestUtil.OptionalEditorProvider();
6264
asyncIndexUpdate.setCorruptIndexHandler(trackingCorruptIndexHandler);
6365
}

oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticFullTextIndexTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ public void fullTextWithFuzzyEditDistance() throws Exception {
7070
});
7171
}
7272

73+
@Override
74+
protected String[] getPrefixes() {
75+
return new String[]{"", "?{}?", "?"};
76+
}
77+
7378
}

oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/FullTextIndexCommonTest.java

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ public void fullTextQueryWithDifferentBoosts() throws Exception {
152152
c.setProperty("propb", "Hello folks");
153153
test.addChild("d").setProperty("propb", "baz");
154154
root.commit();
155-
156-
assertEventually(() -> {
157-
assertQuery("//*[jcr:contains(., 'Hello')]", XPATH, List.of("/test/c", "/test/b", "/test/a"), true, true);
158-
assertQuery("//*[jcr:contains(., 'Hello')] order by @jcr:score ascending", XPATH,
155+
for (String prefix : getPrefixes()) {
156+
assertEventually(() -> {
157+
assertQuery("//*[jcr:contains(., '" + prefix + "Hello')]", XPATH,
158+
List.of("/test/c", "/test/b", "/test/a"), true, true);
159+
assertQuery("//*[jcr:contains(., '" + prefix + "Hello')] order by @jcr:score ascending", XPATH,
159160
List.of("/test/a", "/test/b", "/test/c"), true, true);
160-
assertQuery("//*[jcr:contains(., 'people')]", XPATH, List.of("/test/c"));
161-
});
161+
assertQuery("//*[jcr:contains(., '" + prefix + "people')]", XPATH, List.of("/test/c"));
162+
});
163+
}
162164
}
163165

164166
@Test
@@ -197,11 +199,13 @@ public void onlyNodeScopeIndexedQuery() throws Exception {
197199
d.setProperty("b", "world");
198200
root.commit();
199201

200-
assertEventually(() -> {
201-
assertQuery("//*[jcr:contains(., 'Hello')]", XPATH, List.of("/test/nodea", "/test/nodec", "/test/noded"));
202-
assertQuery("//*[jcr:contains(., 'hello world')]", XPATH, List.of("/test/nodec", "/test/noded"));
203-
assertQuery("//*[jcr:contains(., 'hello OR world')]", XPATH, List.of("/test/nodea", "/test/nodeb", "/test/nodec", "/test/noded"));
204-
});
202+
for (String prefix : getPrefixes()) {
203+
assertEventually(() -> {
204+
assertQuery("//*[jcr:contains(., '" + prefix + "Hello')]", XPATH, List.of("/test/nodea", "/test/nodec", "/test/noded"));
205+
assertQuery("//*[jcr:contains(., '" + prefix + "hello world')]", XPATH, List.of("/test/nodec", "/test/noded"));
206+
assertQuery("//*[jcr:contains(., '" + prefix + "hello OR world')]", XPATH, List.of("/test/nodea", "/test/nodeb", "/test/nodec", "/test/noded"));
207+
});
208+
}
205209
}
206210

207211
@Test
@@ -224,10 +228,12 @@ public void nodeScopeIndexedQuery() throws Exception {
224228
d.setProperty("b", "world");
225229
root.commit();
226230

227-
assertEventually(() -> {
228-
assertQuery("//*[jcr:contains(., 'Hello')]", XPATH, List.of("/test/a", "/test/c", "/test/d"));
229-
assertQuery("//*[jcr:contains(., 'hello world')]", XPATH, List.of("/test/c", "/test/d"));
230-
});
231+
for (String prefix : getPrefixes()) {
232+
assertEventually(() -> {
233+
assertQuery("//*[jcr:contains(., '" + prefix + "Hello')]", XPATH, List.of("/test/a", "/test/c", "/test/d"));
234+
assertQuery("//*[jcr:contains(., '" + prefix + "hello world')]", XPATH, List.of("/test/c", "/test/d"));
235+
});
236+
}
231237
}
232238

233239
@Test
@@ -250,10 +256,12 @@ public void propertyIndexWithNodeScopeIndexedQuery() throws Exception {
250256
d.setProperty("b", "world");
251257
root.commit();
252258

253-
assertEventually(() -> {
254-
assertQuery("//*[jcr:contains(., 'Hello')]", XPATH, List.of("/test/nodea", "/test/nodec", "/test/noded"));
255-
assertQuery("//*[jcr:contains(., 'hello world')]", XPATH, List.of("/test/nodec", "/test/noded"));
256-
});
259+
for (String prefix : getPrefixes()) {
260+
assertEventually(() -> {
261+
assertQuery("//*[jcr:contains(., '" + prefix + "Hello')]", XPATH, List.of("/test/nodea", "/test/nodec", "/test/noded"));
262+
assertQuery("//*[jcr:contains(., '" + prefix + "hello world')]", XPATH, List.of("/test/nodec", "/test/noded"));
263+
});
264+
}
257265
}
258266

259267
/*
@@ -280,10 +288,12 @@ public void onlyAnalyzedPropertyShouldNotBeReturnedForNodeScopeIndexedQuery() th
280288
d.setProperty("b", "world");
281289
root.commit();
282290

283-
assertEventually(() -> {
284-
assertQuery("//*[jcr:contains(., 'Hello')]", XPATH, List.of("/test/nodec", "/test/noded"));
285-
assertQuery("//*[jcr:contains(., 'hello world')]", XPATH, List.of("/test/nodec"));
286-
});
291+
for (String prefix : getPrefixes()) {
292+
assertEventually(() -> {
293+
assertQuery("//*[jcr:contains(., '" + prefix + "Hello')]", XPATH, List.of("/test/nodec", "/test/noded"));
294+
assertQuery("//*[jcr:contains(., '" + prefix + "hello world')]", XPATH, List.of("/test/nodec"));
295+
});
296+
}
287297
}
288298

289299
@Test
@@ -321,8 +331,10 @@ public void fulltextWithModifiedNodeScopeIndex() throws Exception {
321331
index.setProperty(FulltextIndexConstants.PROP_REFRESH_DEFN, true);
322332
root.commit();
323333

324-
assertEventually(() ->
325-
assertQuery("//*[jcr:contains(., 'jpg')]", XPATH, List.of("/test/a")));
334+
for (String prefix : getPrefixes()) {
335+
assertEventually(() ->
336+
assertQuery("//*[jcr:contains(., '" + prefix + "jpg')]", XPATH, List.of("/test/a")));
337+
}
326338
}
327339

328340
@Test
@@ -346,14 +358,16 @@ public void fulltextWithMalformedFields() throws Exception {
346358

347359
root.commit();
348360

349-
assertEventually(() -> {
350-
assertQuery("//*[jcr:contains(., 'foo')]", XPATH, List.of("/test/a"));
351-
assertQuery("//*[jcr:contains(., '2025')]", XPATH, List.of("/test/b"));
352-
assertQuery("//*[jcr:contains(., '123')]", XPATH, List.of("/test/c"));
353-
assertQuery("//*[jcr:contains(., '456.78')]", XPATH, List.of("/test/d"));
354-
assertQuery("//*[jcr:contains(., 'true')]", XPATH, List.of("/test/e"));
361+
for (String prefix : getPrefixes()) {
362+
assertEventually(() -> {
363+
assertQuery("//*[jcr:contains(., '" + prefix + "foo')]", XPATH, List.of("/test/a"));
364+
assertQuery("//*[jcr:contains(., '" + prefix + "2025')]", XPATH, List.of("/test/b"));
365+
assertQuery("//*[jcr:contains(., '" + prefix + "123')]", XPATH, List.of("/test/c"));
366+
assertQuery("//*[jcr:contains(., '" + prefix + "456.78')]", XPATH, List.of("/test/d"));
367+
assertQuery("//*[jcr:contains(., '" + prefix + "true')]", XPATH, List.of("/test/e"));
355368
}
356-
);
369+
);
370+
}
357371
}
358372

359373
protected void assertEventually(Runnable r) {
@@ -396,4 +410,7 @@ protected String explain(String query, String lang) {
396410
return executeQuery(explain, lang).get(0);
397411
}
398412

413+
protected String[] getPrefixes() {
414+
return new String[]{""};
415+
}
399416
}

0 commit comments

Comments
 (0)