You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by GitBox <gi...@apache.org> on 2022/05/16 18:23:01 UTC
[GitHub] [solr] thelabdude opened a new pull request, #865: SOLR-16199: Improve handling of LIKE queries with wildcard
thelabdude opened a new pull request, #865:
URL: https://github.com/apache/solr/pull/865
https://issues.apache.org/jira/browse/SOLR-16199
# Description
Adding some more unit tests for LIKE clauses against text analyzed fields
# Solution
Still tinkering ...
# Tests
new tests added in this PR
# Checklist
Please review the following and check all that apply:
- [ ] I have reviewed the guidelines for [How to Contribute](https://wiki.apache.org/solr/HowToContribute) and my code conforms to the standards described there to the best of my ability.
- [ ] I have created a Jira issue and added the issue ID to my pull request title.
- [ ] I have given Solr maintainers [access](https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork) to contribute to my PR branch. (optional but recommended)
- [ ] I have developed this patch against the `main` branch.
- [ ] I have run `./gradlew check`.
- [ ] I have added tests for my changes.
- [ ] I have added documentation for the [Reference Guide](https://github.com/apache/solr/tree/main/solr/solr-ref-guide)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] kiranchitturi commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
kiranchitturi commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r876250212
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
+ char c = term.charAt(i);
+ // Only replace special characters if they are not escaped
+ if (escapeChar != null && c == escapeChar) {
+ isEscaped = true;
+ }
+ if (c == '%' && !isEscaped) {
+ sb.append('*');
+ } else if (c == '_' && !isEscaped) {
+ sb.append('?');
+ } else if (c == '\'' && isEscaped) {
+ sb.append('\'');
+ isEscaped = false;
+ } else if ((escapeChar == null || escapeChar != c) && c != '\'') {
+ sb.append(c);
+ isEscaped = false;
+ }
Review Comment:
The single quote should only be appended if it's escaped and it should not be appended if it's not escaped
The branch `c == '\'' && isEscaped` appends the single quote if it's escaped
The last branch appends everything (except single quote and escape character).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] kiranchitturi commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
kiranchitturi commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r876255428
##########
solr/modules/sql/src/test/org/apache/solr/handler/sql/TestSQLHandler.java:
##########
@@ -2434,15 +2434,73 @@ public void testColIsNull() throws Exception {
@Test
public void testLike() throws Exception {
new UpdateRequest()
- .add("id", "1", "a_s", "hello-1", "b_s", "foo")
- .add("id", "2", "a_s", "world-2", "b_s", "foo")
- .add("id", "3", "a_s", "hello-3", "b_s", "foo")
- .add("id", "4", "a_s", "world-4", "b_s", "foo")
- .add("id", "5", "a_s", "hello-5", "b_s", "foo")
- .add("id", "6", "a_s", "world-6", "b_s", "bar")
+ .add(
+ "id",
+ "1",
+ "a_s",
+ "hello-1",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "2",
+ "a_s",
+ "world-2",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleeping pig")
+ .add(
+ "id",
+ "3",
+ "a_s",
+ "hello-3",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "4",
+ "a_s",
+ "world-4",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleepy pig")
+ .add(
+ "id",
+ "5",
+ "a_s",
+ "hello-5",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "6",
+ "a_s",
+ "world-6",
+ "b_s",
+ "bar",
+ "c_t",
+ "the sly black dog jumped over the sleepin piglet")
+ .add(
+ "id",
+ "7",
+ "a_s",
+ "world%_7",
+ "b_s",
+ "zaz",
+ "c_t",
+ "the lazy dog jumped over the quick brown fox")
.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'h_llo-%'", 3);
+ expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'world\\%\\__' ESCAPE '\\'", 1);
Review Comment:
`%` is 0 or more characters. If the term is parsed as `world%__`, then it would be translated to `world*??` which would match three values `world-4`, `world%_7` and `world-2`
Example showing that this matches 3 documents: this would pass `expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'world%__'", 3);`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] thelabdude merged pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
thelabdude merged PR #865:
URL: https://github.com/apache/solr/pull/865
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] madrob commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
madrob commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r876342658
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
+ char c = term.charAt(i);
+ // Only replace special characters if they are not escaped
+ if (escapeChar != null && c == escapeChar) {
+ isEscaped = true;
+ }
+ if (c == '%' && !isEscaped) {
+ sb.append('*');
+ } else if (c == '_' && !isEscaped) {
+ sb.append('?');
+ } else if (c == '\'' && isEscaped) {
+ sb.append('\'');
+ isEscaped = false;
+ } else if ((escapeChar == null || escapeChar != c) && c != '\'') {
+ sb.append(c);
+ isEscaped = false;
+ }
Review Comment:
Right. So I think most of this condition had already been checked at line 379 above? Maybe 382 needs to be an `else if`? Something here feels like it is more complicated than it needs to be, and I'm struggling a little to understand it.
Maybe we need a test case where `%` is the escape char and make sure that we don't break that way? It's kind of a strange use case but it seems like it should work?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] madrob commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
madrob commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875982767
##########
solr/modules/sql/src/test/org/apache/solr/handler/sql/TestSQLHandler.java:
##########
@@ -2434,15 +2434,73 @@ public void testColIsNull() throws Exception {
@Test
public void testLike() throws Exception {
new UpdateRequest()
- .add("id", "1", "a_s", "hello-1", "b_s", "foo")
- .add("id", "2", "a_s", "world-2", "b_s", "foo")
- .add("id", "3", "a_s", "hello-3", "b_s", "foo")
- .add("id", "4", "a_s", "world-4", "b_s", "foo")
- .add("id", "5", "a_s", "hello-5", "b_s", "foo")
- .add("id", "6", "a_s", "world-6", "b_s", "bar")
+ .add(
+ "id",
+ "1",
+ "a_s",
+ "hello-1",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "2",
+ "a_s",
+ "world-2",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleeping pig")
+ .add(
+ "id",
+ "3",
+ "a_s",
+ "hello-3",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "4",
+ "a_s",
+ "world-4",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleepy pig")
+ .add(
+ "id",
+ "5",
+ "a_s",
+ "hello-5",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "6",
+ "a_s",
+ "world-6",
+ "b_s",
+ "bar",
+ "c_t",
+ "the sly black dog jumped over the sleepin piglet")
+ .add(
+ "id",
+ "7",
+ "a_s",
+ "world%_7",
+ "b_s",
+ "zaz",
+ "c_t",
+ "the lazy dog jumped over the quick brown fox")
.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'h_llo-%'", 3);
+ expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'world\\%\\__' ESCAPE '\\'", 1);
Review Comment:
if the escapes weren’t working to parse those as literal `%` and `_`, then `world%__` (using wildcards) would still match the literal `world%_7` but it wouldn’t match anything else. `world-4` is too short, right? is `%` 1 or more, or 0 or more characters? If it’s 1 or more then we need a string somewhere that is `worldXXX` with three trailing characters
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] kiranchitturi commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
kiranchitturi commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875262356
##########
solr/modules/sql/src/test/org/apache/solr/handler/sql/TestSQLHandler.java:
##########
@@ -2434,15 +2434,73 @@ public void testColIsNull() throws Exception {
@Test
public void testLike() throws Exception {
new UpdateRequest()
- .add("id", "1", "a_s", "hello-1", "b_s", "foo")
- .add("id", "2", "a_s", "world-2", "b_s", "foo")
- .add("id", "3", "a_s", "hello-3", "b_s", "foo")
- .add("id", "4", "a_s", "world-4", "b_s", "foo")
- .add("id", "5", "a_s", "hello-5", "b_s", "foo")
- .add("id", "6", "a_s", "world-6", "b_s", "bar")
+ .add(
+ "id",
+ "1",
+ "a_s",
+ "hello-1",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "2",
+ "a_s",
+ "world-2",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleeping pig")
+ .add(
+ "id",
+ "3",
+ "a_s",
+ "hello-3",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "4",
+ "a_s",
+ "world-4",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleepy pig")
+ .add(
+ "id",
+ "5",
+ "a_s",
+ "hello-5",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "6",
+ "a_s",
+ "world-6",
+ "b_s",
+ "bar",
+ "c_t",
+ "the sly black dog jumped over the sleepin piglet")
+ .add(
+ "id",
+ "7",
+ "a_s",
+ "world%_7",
+ "b_s",
+ "zaz",
+ "c_t",
+ "the lazy dog jumped over the quick brown fox")
.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'h_llo-%'", 3);
+ expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'world\\%\\__' ESCAPE '\\'", 1);
Review Comment:
if the escapes weren't working, the test would fail because it wouldn't match anything
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] madrob commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
madrob commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875987015
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
+ char c = term.charAt(i);
+ // Only replace special characters if they are not escaped
+ if (escapeChar != null && c == escapeChar) {
+ isEscaped = true;
+ }
+ if (c == '%' && !isEscaped) {
+ sb.append('*');
+ } else if (c == '_' && !isEscaped) {
+ sb.append('?');
+ } else if (c == '\'' && isEscaped) {
+ sb.append('\'');
+ isEscaped = false;
+ } else if ((escapeChar == null || escapeChar != c) && c != '\'') {
+ sb.append(c);
+ isEscaped = false;
+ }
Review Comment:
I think I'm mainly confused about what the last branch is supposed to do. Append anything that's not the escape char and not single quote?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] madrob commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
madrob commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875276783
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
+ char c = term.charAt(i);
+ // Only replace special characters if they are not escaped
+ if (escapeChar != null && c == escapeChar) {
+ isEscaped = true;
+ }
+ if (c == '%' && !isEscaped) {
+ sb.append('*');
+ } else if (c == '_' && !isEscaped) {
+ sb.append('?');
+ } else if (c == '\'' && isEscaped) {
+ sb.append('\'');
+ isEscaped = false;
+ } else if ((escapeChar == null || escapeChar != c) && c != '\'') {
+ sb.append(c);
+ isEscaped = false;
+ }
Review Comment:
They both append c and set escaped
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] kiranchitturi commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
kiranchitturi commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875261198
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
+ char c = term.charAt(i);
+ // Only replace special characters if they are not escaped
+ if (escapeChar != null && c == escapeChar) {
+ isEscaped = true;
+ }
+ if (c == '%' && !isEscaped) {
+ sb.append('*');
+ } else if (c == '_' && !isEscaped) {
+ sb.append('?');
+ } else if (c == '\'' && isEscaped) {
+ sb.append('\'');
+ isEscaped = false;
+ } else if ((escapeChar == null || escapeChar != c) && c != '\'') {
+ sb.append(c);
+ isEscaped = false;
+ }
Review Comment:
not really. If single quote is escaped, it should be added. The last branch wouldn't catch that
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] kiranchitturi commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
kiranchitturi commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875259672
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
Review Comment:
that was an oversight, fixing
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
[GitHub] [solr] madrob commented on a diff in pull request #865: SOLR-16199: Improve handling of LIKE queries with wildcard
Posted by GitBox <gi...@apache.org>.
madrob commented on code in PR #865:
URL: https://github.com/apache/solr/pull/865#discussion_r875176213
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
Review Comment:
why is this `- 1`?
##########
solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrFilter.java:
##########
@@ -346,21 +346,52 @@ protected String translateAnd(RexNode node0) {
}
protected String translateLike(RexNode like) {
- Pair<String, RexLiteral> pair = getFieldValuePair(like);
+ Pair<Pair<String, RexLiteral>, Character> pairWithEscapeCharacter =
+ getFieldValuePairWithEscapeCharacter(like);
+ Pair<String, RexLiteral> pair = pairWithEscapeCharacter.getKey();
+ Character escapeChar = pairWithEscapeCharacter.getValue();
+
String terms = pair.getValue().toString().trim();
- terms = terms.replace("'", "").replace('%', '*').replace('_', '?');
- boolean wrappedQuotes = false;
+ terms = translateLikeTermToSolrSyntax(terms, escapeChar);
+
if (!terms.startsWith("(") && !terms.startsWith("[") && !terms.startsWith("{")) {
- // restore the * and ? after escaping
- terms =
- "\""
- + ClientUtils.escapeQueryChars(terms).replace("\\*", "*").replace("\\?", "?")
- + "\"";
- wrappedQuotes = true;
- }
+ terms = escapeWithWildcard(terms);
+
+ // if terms contains multiple words and one or more wildcard chars, then we need to employ
+ // the complexphrase parser
+ // but that expects the terms wrapped in double-quotes, not parens
+ boolean hasMultipleTerms = terms.split("\\s+").length > 1;
+ if (hasMultipleTerms && (terms.contains("*") || terms.contains("?"))) {
+ String quotedTerms = "\"" + terms.substring(1, terms.length() - 1) + "\"";
+ return "{!complexphrase}" + pair.getKey() + ":" + quotedTerms;
+ }
+ } // else treat as an embedded Solr query and pass-through
+
+ return pair.getKey() + ":" + terms;
+ }
- String query = pair.getKey() + ":" + terms;
- return wrappedQuotes ? "{!complexphrase}" + query : query;
+ private String translateLikeTermToSolrSyntax(String term, Character escapeChar) {
+ boolean isEscaped = false;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < term.length() - 1; i++) {
+ char c = term.charAt(i);
+ // Only replace special characters if they are not escaped
+ if (escapeChar != null && c == escapeChar) {
+ isEscaped = true;
+ }
+ if (c == '%' && !isEscaped) {
+ sb.append('*');
+ } else if (c == '_' && !isEscaped) {
+ sb.append('?');
+ } else if (c == '\'' && isEscaped) {
+ sb.append('\'');
+ isEscaped = false;
+ } else if ((escapeChar == null || escapeChar != c) && c != '\'') {
+ sb.append(c);
+ isEscaped = false;
+ }
Review Comment:
The last two branches do the same thing?
##########
solr/modules/sql/src/test/org/apache/solr/handler/sql/TestSQLHandler.java:
##########
@@ -2434,15 +2434,73 @@ public void testColIsNull() throws Exception {
@Test
public void testLike() throws Exception {
new UpdateRequest()
- .add("id", "1", "a_s", "hello-1", "b_s", "foo")
- .add("id", "2", "a_s", "world-2", "b_s", "foo")
- .add("id", "3", "a_s", "hello-3", "b_s", "foo")
- .add("id", "4", "a_s", "world-4", "b_s", "foo")
- .add("id", "5", "a_s", "hello-5", "b_s", "foo")
- .add("id", "6", "a_s", "world-6", "b_s", "bar")
+ .add(
+ "id",
+ "1",
+ "a_s",
+ "hello-1",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "2",
+ "a_s",
+ "world-2",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleeping pig")
+ .add(
+ "id",
+ "3",
+ "a_s",
+ "hello-3",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "4",
+ "a_s",
+ "world-4",
+ "b_s",
+ "foo",
+ "c_t",
+ "the sly black dog jumped over the sleepy pig")
+ .add(
+ "id",
+ "5",
+ "a_s",
+ "hello-5",
+ "b_s",
+ "foo",
+ "c_t",
+ "the quick brown fox jumped over the lazy dog")
+ .add(
+ "id",
+ "6",
+ "a_s",
+ "world-6",
+ "b_s",
+ "bar",
+ "c_t",
+ "the sly black dog jumped over the sleepin piglet")
+ .add(
+ "id",
+ "7",
+ "a_s",
+ "world%_7",
+ "b_s",
+ "zaz",
+ "c_t",
+ "the lazy dog jumped over the quick brown fox")
.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'h_llo-%'", 3);
+ expectResults("SELECT a_s FROM $ALIAS WHERE a_s LIKE 'world\\%\\__' ESCAPE '\\'", 1);
Review Comment:
I'm unclear if this would match `world-6` if the escapes weren't working, but I think it's important to have a negative test case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org