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 ng...@apache.org on 2022/07/29 11:34:47 UTC
[jackrabbit-oak] branch trunk updated: OAK-9870 | Escaping brackets to avoid query parse exceptions (#643)
This is an automated email from the ASF dual-hosted git repository.
ngupta pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push:
new c194a0d754 OAK-9870 | Escaping brackets to avoid query parse exceptions (#643)
c194a0d754 is described below
commit c194a0d7547d6e5d6da4a5df0ab9e1efd647c2f5
Author: nit0906 <ni...@gmail.com>
AuthorDate: Fri Jul 29 17:04:41 2022 +0530
OAK-9870 | Escaping brackets to avoid query parse exceptions (#643)
* OAK-9870 | Escaping brackets to avoid query parse exceptions
---
.../index/search/spi/query/FulltextIndex.java | 5 ++--
.../oak/plugins/index/FullTextIndexCommonTest.java | 30 ++++++++++++++++++++++
2 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndex.java b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndex.java
index fb61936f8c..56edd3d635 100644
--- a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndex.java
+++ b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndex.java
@@ -281,14 +281,15 @@ public abstract class FulltextIndex implements AdvancedQueryIndex, QueryIndex, N
}
/**
- * Following chars are used as operators in Lucene Query and should be escaped
+ * Following chars are used as operators in Lucene and Elastic Queries and should be escaped
*/
- private static final char[] QUERY_OPERATORS = {':' , '/', '!', '&', '|', '='};
+ private static final char[] QUERY_OPERATORS = {':' , '/', '!', '&', '|', '=', '{', '}', '[', ']', '(', ')'};
/**
* Following logic is taken from org.apache.jackrabbit.core.query.lucene.JackrabbitQueryParser#parse(java.lang.String)
*/
public static String rewriteQueryText(String textsearch) {
+
// replace escaped ' with just '
StringBuilder rewritten = new StringBuilder();
// most query parsers recognize 'AND' and 'NOT' as
diff --git a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/FullTextIndexCommonTest.java b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/FullTextIndexCommonTest.java
index c60d348af9..f9851ec783 100644
--- a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/FullTextIndexCommonTest.java
+++ b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/FullTextIndexCommonTest.java
@@ -88,4 +88,34 @@ public abstract class FullTextIndexCommonTest extends AbstractQueryTest {
});
}
+
+ @Test
+ public void testWithSpecialCharsInSearchTerm() throws Exception {
+ IndexDefinitionBuilder builder = indexOptions.createIndex(
+ indexOptions.createIndexDefinitionBuilder(), false, "analyzed_field");
+ builder.noAsync();
+ builder.indexRule("nt:base")
+ .property("analyzed_field")
+ .analyzed().nodeScopeIndex();
+
+ indexOptions.setIndex(root, UUID.randomUUID().toString(), builder);
+ root.commit();
+
+ //add content
+ Tree test = root.getTree("/").addChild("test");
+
+ test.addChild("a").setProperty("analyzed_field", "foo");
+ root.commit();
+
+ assertEventually(() -> {
+ assertQuery("//*[jcr:contains(@analyzed_field, '{foo}')] ", XPATH, Collections.singletonList("/test/a"));
+ assertQuery("//*[jcr:contains(@analyzed_field, '\\{foo}')] ", XPATH, Collections.singletonList("/test/a"));
+ assertQuery("//*[jcr:contains(@analyzed_field, 'foo:')] ", XPATH, Collections.singletonList("/test/a"));
+ assertQuery("//*[jcr:contains(@analyzed_field, '[foo]')] ", XPATH, Collections.singletonList("/test/a"));
+ assertQuery("//*[jcr:contains(@analyzed_field, '|foo/')] ", XPATH, Collections.singletonList("/test/a"));
+ assertQuery("//*[jcr:contains(@analyzed_field, '(&=!foo')] ", XPATH, Collections.singletonList("/test/a"));
+ });
+
+ }
+
}