Skip to content

Commit

Permalink
Merge pull request #4742 from freelawproject/4737-fix-boosting-fields…
Browse files Browse the repository at this point in the history
…-queries-syntax

4737 Avoid wrapping numbers in quotes for boost queries.
  • Loading branch information
mlissner authored Nov 26, 2024
2 parents 56a41d4 + adf676e commit b30275e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cl/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def cleanup_main_query(query_string: str) -> str:
"""
inside_a_phrase = False
cleaned_items = []
for item in re.split(r'([^a-zA-Z0-9_\-~":]+)', query_string):
for item in re.split(r'([^a-zA-Z0-9_\-^~":]+)', query_string):
if not item:
continue

Expand Down
73 changes: 14 additions & 59 deletions cl/search/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,20 @@ def test_round_estimated_search_counts(self) -> None:
with self.subTest(test=test, msg="Test estimated search counts."):
self.assertEqual(simplify_estimated_count(test[0]), test[1])

def test_avoid_wrapping_boosted_numbers_in_quotes(self) -> None:
"""Confirm that numbers in boost queries are not wrapped in quotes
that makes the query to fail.
"""
search_params = {
"type": SEARCH_TYPES.ORAL_ARGUMENT,
"q": "Jose^3",
}
r = self.client.get(
reverse("show_results"),
search_params,
)
self.assertNotIn("encountered an error", r.content.decode())


class SearchAPIV4CommonTest(ESIndexTestCase, TestCase):
"""Common tests for the Search API V4 endpoints."""
Expand Down Expand Up @@ -1643,35 +1657,6 @@ def test_search_query_saving(self) -> None:
"Repeated query not marked as having hit cache",
)

# Force Solr use
@override_flag("oa-es-active", False)
@override_flag("r-es-active", False)
@override_flag("p-es-active", False)
@override_flag("o-es-active", False)
def test_search_query_saving_solr(self) -> None:
"""Are queries saved when using solr search (do_search)"""
for query in self.searches:
url = f"{reverse('show_results')}?{query}"
self.client.get(url)
last_query = SearchQuery.objects.last()
expected_query = self.normalize_query(query, replace_space=True)
stored_query = self.normalize_query(last_query.get_params)
self.assertEqual(
expected_query,
stored_query,
f"Query was not saved properly. Expected {expected_query}, got {stored_query}",
)
self.assertEqual(
last_query.engine,
SearchQuery.SOLR,
f"Saved wrong `engine` value, expected {SearchQuery.SOLR}",
)
self.assertEqual(
last_query.source,
SearchQuery.WEBSITE,
self.source_error_message,
)

def test_failed_es_search_queries(self) -> None:
"""Do we flag failed ElasticSearch queries properly?"""
query = "type=r&q=contains/sproximity token"
Expand Down Expand Up @@ -1772,36 +1757,6 @@ def test_failed_es_search_v3_api_queries(self) -> None:
f"Saved wrong `engine` value, expected {SearchQuery.ELASTICSEARCH}",
)

@override_flag("oa-es-active", False)
@override_flag("oa-es-activate", False)
@override_flag("r-es-search-api-active", False)
@override_flag("p-es-active", False)
@override_flag("o-es-search-api-active", False)
def test_search_solr_api_v3_query_saving(self) -> None:
"""Do we save queries on all V3 Search Solr endpoints"""
for query in self.base_searches:
url = f"{reverse("search-list", kwargs={"version": "v3"})}?{query}"
self.client.get(url)
# Compare parsed query strings;
last_query = SearchQuery.objects.last()
expected_query = self.normalize_query(query, replace_space=True)
stored_query = self.normalize_query(last_query.get_params)
self.assertEqual(
expected_query,
stored_query,
f"Query was not saved properly. Expected {expected_query}, got {stored_query}",
)
self.assertEqual(
last_query.engine,
SearchQuery.SOLR,
f"Saved wrong `engine` value, expected {SearchQuery.ELASTICSEARCH}",
)
self.assertEqual(
last_query.source,
SearchQuery.API,
self.source_error_message,
)


class CaptionTest(TestCase):
"""Can we make good looking captions?"""
Expand Down

0 comments on commit b30275e

Please sign in to comment.