Skip to content

Commit

Permalink
Merge pull request #4834 from freelawproject/4707-case-name-search-bug
Browse files Browse the repository at this point in the history
4707 Fixed Case name filter search bug
  • Loading branch information
mlissner authored Dec 17, 2024
2 parents 88924a3 + 336869f commit b0764a4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
34 changes: 14 additions & 20 deletions cl/lib/elasticsearch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,13 @@ def build_fulltext_query(
q_should.append(
Q(
"match_phrase",
caseName={"query": query_value, "boost": 2, "slop": 1},
**{
"caseName.exact": {
"query": query_value,
"boost": 2,
"slop": 1,
}
},
)
)

Expand Down Expand Up @@ -481,26 +487,14 @@ def build_text_filter(field: str, value: str) -> List:
if isinstance(value, str):
validate_query_syntax(value, QueryType.FILTER)

base_query_string = {
"query_string": {
"query": value,
"fields": [field],
"default_operator": "AND",
}
}
if "caseName" in field and '"' not in value:
# Use phrase with slop, and give it a boost to prioritize the
# phrase match to ensure that caseName filtering returns exactly
# this order as the first priority.
# Avoid applying slop to quoted queries, as they expect exact matches.
base_query_string["query_string"].update(
{
"type": "phrase",
"phrase_slop": "1",
"boost": "2", # Boosting the phrase match to ensure it's ranked higher than individual term matches
}
return [
Q(
"query_string",
query=value,
fields=[field],
default_operator="AND",
)
return [Q(base_query_string)]
]
return []


Expand Down
6 changes: 4 additions & 2 deletions cl/search/tests/tests_es_opinion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,8 @@ def test_uses_exact_version_for_case_name_field(self) -> None:
r = async_to_sync(self._test_article_count)(
search_params, 1, "case_name exact filter"
)
self.assertIn("<mark>Maecenas Howell</mark>", r.content.decode())
self.assertIn("<mark>Maecenas</mark>", r.content.decode())
self.assertIn("<mark>Howell</mark>", r.content.decode())

# case_name filter: Howells
search_params = {
Expand All @@ -2241,7 +2242,8 @@ def test_uses_exact_version_for_case_name_field(self) -> None:
r = async_to_sync(self._test_article_count)(
search_params, 1, "case_name exact filter"
)
self.assertIn("<mark>Maecenas Howells</mark>", r.content.decode())
self.assertIn("<mark>Maecenas</mark>", r.content.decode())
self.assertIn("<mark>Howells</mark>", r.content.decode())

# text query: Howell
search_params = {
Expand Down
25 changes: 22 additions & 3 deletions cl/search/tests/tests_es_recap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1795,8 +1795,9 @@ async def test_results_highlights(self) -> None:
"case_name": "SUBPOENAS SERVED ON",
}
r = await self._test_article_count(params, 1, "highlights caseName")
# Count child documents under docket.
self.assertIn("<mark>SUBPOENAS SERVED ON</mark>", r.content.decode())
self.assertIn("<mark>SUBPOENAS</mark>", r.content.decode())
self.assertIn("<mark>SERVED</mark>", r.content.decode())
self.assertIn("<mark>ON</mark>", r.content.decode())

# Highlight filter: description
params = {
Expand Down Expand Up @@ -2806,6 +2807,24 @@ def test_uses_exact_version_for_case_name_field(self) -> None:
r = async_to_sync(self._test_article_count)(cd, 1, "Disable stemming")
self.assertIn("<mark>Howells v. Indiana</mark>", r.content.decode())

# No quoted case_name filter: A match for 'Indiana Howell' is expected,
# as the order is not mandatory
cd = {
"type": SEARCH_TYPES.RECAP,
"case_name": "Indiana Howell",
}
r = async_to_sync(self._test_article_count)(cd, 1, "No phrase search.")
self.assertIn("<mark>Howell</mark>", r.content.decode())
self.assertIn("<mark>Indiana</mark>", r.content.decode())

# Quoted case_name filter: "Indiana v. Howells" match is not expected
# as order is mandatory
cd = {
"type": SEARCH_TYPES.RECAP,
"case_name": '"Indiana v. Howells"',
}
async_to_sync(self._test_article_count)(cd, 0, "Phrase search.")

# text query: Howell
cd = {
"type": SEARCH_TYPES.RECAP,
Expand Down Expand Up @@ -3718,7 +3737,7 @@ async def test_results_api_highlighted_fields(self) -> None:
"result": self.rd_api,
"V4": True,
"assignedTo": "<mark>George</mark> Doe II",
"caseName": "<mark>America vs API</mark> Lorem",
"caseName": "<mark>America</mark> <mark>vs</mark> <mark>API</mark> Lorem",
"cause": "<mark>401</mark> <mark>Civil</mark>",
"court_citation_string": "<mark>Appeals</mark>. CA9.",
"docketNumber": "<mark>1:24-bk-0000</mark>",
Expand Down

0 comments on commit b0764a4

Please sign in to comment.