Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Products/CMFPlone/browser/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ def munge_search_term(query):
continue
r.append(f'"{clean_qp}"')

r += map(quote, query.strip().split())
# Add wildcards to individual terms, not just at the end
if not original_query.endswith('"'):
individual_terms = [quote(term) + "*" for term in query.strip().split()]
else:
individual_terms = [quote(term) for term in query.strip().split()]

r += individual_terms
r = " AND ".join(r)
r = r + ("*" if r and not original_query.endswith('"') else "")
return r


Expand Down
24 changes: 11 additions & 13 deletions Products/CMFPlone/tests/testSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ def test_quoted_phrase(self):
self.assertEqual(
view.results(query=dict(SearchableText='"ham spam"')).sequence_length, 0
)

# arbitrary words within index
self.assertEqual(
view.results(query=dict(SearchableText="spam eggs")).sequence_length, 12
Expand All @@ -314,7 +313,6 @@ def test_quoted_phrase(self):
self.assertEqual(
view.results(query=dict(SearchableText='"spam eggs"')).sequence_length, 0
)

# unquoted substring search
self.assertEqual(
view.results(query=dict(SearchableText="egg")).sequence_length, 12
Expand All @@ -323,13 +321,11 @@ def test_quoted_phrase(self):
self.assertEqual(
view.results(query=dict(SearchableText='"egg"')).sequence_length, 0
)

# unquoted multi substring search
# XXX: this is munged to "egg AND spa*" and doesn't find any results
# XXX: this is munged to "egg* AND spa*" and SHOULD find results
self.assertEqual(
view.results(query=dict(SearchableText="egg spa")).sequence_length, 0
view.results(query=dict(SearchableText="egg spa")).sequence_length, 12
)

# weird input
self.assertEqual(
view.results(query=dict(SearchableText='"eggs" ham spam')).sequence_length,
Expand All @@ -352,7 +348,7 @@ def test_munge_search_term(self):
(
# search term
"spam ham",
"spam AND ham*",
"spam* AND ham*",
),
(
# quoted term
Expand Down Expand Up @@ -382,26 +378,28 @@ def test_munge_search_term(self):
(
# mixed cases
"Spam hAm",
"Spam AND hAm*",
"Spam* AND hAm*",
),
(
# mix quoting and unquoted
'let\'s eat some "ham and eggs " without spam ',
'"ham and eggs" AND let\'s AND eat AND some ' "AND without AND spam*",
'"ham and eggs" AND let\'s* AND eat* AND some* '
"AND without* AND spam*",
),
(
# UNQUOTED TERMS GET WILDCARDS
'test "Welcome" to "Plone" retest',
'"Welcome" AND "Plone" AND test AND to AND retest*',
'"Welcome" AND "Plone" AND test* AND to* AND retest*',
),
(
# parentheses
"spam (ham)",
'spam AND "("ham")"*',
'spam* AND "("ham")"*',
),
(
# special keywords
"spam or not ham and eggs",
'spam AND "or" AND "not" AND ham AND "and" AND eggs*',
'spam* AND "or"* AND "not"* AND ham* AND "and"* AND eggs*',
),
(
# bad characters
Expand All @@ -411,7 +409,7 @@ def test_munge_search_term(self):
(
# weird input
'test ""Welcome" to "Plone"" retest',
'"to" AND test AND WelcomePlone AND retest*',
'"to" AND test* AND WelcomePlone* AND retest*',
),
]

Expand Down
1 change: 1 addition & 0 deletions news/4205.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enhance munge_search_term to add wildcards to individual search terms @rohnsha0