Skip to content

Commit

Permalink
Strip standalone wildcards from the searchable text (#362)
Browse files Browse the repository at this point in the history
Fix the problem and make "foo **" result in the same query as "foo".
Without the fix this used to cause an 500 Internal Server Error.

One might argue that "foo **"  is not a valid query, to start with.
However it can be expected as an input, thus it should not cause a
cryptic error anyway. The fix causes to yield the expected results by
stripping out the standalone wildcard.
  • Loading branch information
reebalazs committed Jun 7, 2023
1 parent fc674b1 commit 2b91260
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
9.1.4 (unreleased)
------------------

- Nothing changed yet.
- Strip standalone wildcards from the searchable text (#362) [reebalazs]


9.1.3 (2023-04-11)
Expand Down
3 changes: 3 additions & 0 deletions src/collective/solr/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def testQuotingWildcardSearches(self):
self.assertEqual(quote("?test", prefix_wildcard=True), "?test")
self.assertEqual(quote("**test", prefix_wildcard=True), "*test")
self.assertEqual(quote("??test", prefix_wildcard=True), "?test")
## standalone wildcard stripped
self.assertEqual(quote("*"), "")
self.assertEqual(quote("**"), "")

def testQuotingFuzzySearches(self):
self.assertEqual(quote("roam~"), "roam~")
Expand Down
5 changes: 5 additions & 0 deletions src/collective/solr/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def testSplitSimpleSearch(self):
)
self.assertRaises(AssertionError, splitSimpleSearch, "foo AND bar")
self.assertEqual(splitSimpleSearch("foo 42"), ["foo", "42"])
# standalone wildcard stripped
self.assertEqual(splitSimpleSearch("foo *"), ["foo"])
self.assertEqual(splitSimpleSearch("foo **"), ["foo"])
# connected wildcard not stripped
self.assertEqual(splitSimpleSearch("foo**"), ["foo**"])

def testIsWildCard(self):
self.assertTrue(isWildCard("foo*"))
Expand Down
3 changes: 2 additions & 1 deletion src/collective/solr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def splitSimpleSearch(term):
for i in range(0, len(parts)):
if i % 2 == 0:
# unquoted text
words = [word for word in parts[i].split() if word]
# note, make sure that both empty words and standalone wildcards are omitted
words = [word for word in parts[i].split() if word and word!="*" and word!="**"]
tokens.extend(words)
else:
# The uneven parts are those inside quotes.
Expand Down

0 comments on commit 2b91260

Please sign in to comment.