Skip to content

Commit

Permalink
Merge pull request #3301 from freelawproject/3257-fix-recap-query-to-…
Browse files Browse the repository at this point in the history
…filter-docs-with-no-pdfs

fix(elasticsearch): Add filter to remove results with no children doc
  • Loading branch information
mlissner authored Oct 25, 2023
2 parents 4372d03 + 6987a4f commit a49bb52
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
11 changes: 11 additions & 0 deletions cl/lib/elasticsearch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,17 @@ def build_full_join_es_queries(
string_query = build_fulltext_query(
parent_query_fields, cd.get("q", ""), only_queries=True
)

# Adds filter to the parent query to exclude results with no children
if cd.get("available_only", ""):
parent_filters.append(
Q(
"has_child",
type="recap_document",
score_mode="max",
query=Q("term", is_available=True),
)
)
parent_query = None
match parent_filters, string_query:
case [], []:
Expand Down
73 changes: 71 additions & 2 deletions cl/search/tests/tests_es_recap.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,81 @@ async def test_document_number_filter(self) -> None:
# Frontend
await self._test_article_count(params, 1, "document_number")

async def test_available_only_field(self) -> None:
def test_available_only_field(self) -> None:
"""Confirm available only filter works properly"""
params = {"type": SEARCH_TYPES.RECAP, "available_only": True}

# Frontend
await self._test_article_count(params, 1, "available_only")
async_to_sync(self._test_article_count)(params, 1, "available_only")

# Add docket with no document
with self.captureOnCommitCallbacks(execute=True):
docket = DocketFactory(
court=self.court,
case_name="Reese Exploration v. Williams Natural Gas ",
date_filed=datetime.date(2015, 8, 16),
date_argued=datetime.date(2013, 5, 20),
docket_number="5:90-cv-04007",
nature_of_suit="440",
)

# perform the previous query and check we still get one result
async_to_sync(self._test_article_count)(params, 1, "available_only")

# perform a text query using the name of the new docket and the available_only filter
params = {
"type": SEARCH_TYPES.RECAP,
"q": "Reese",
"available_only": True,
}
async_to_sync(self._test_article_count)(params, 0, "available_only")

# add a document that is not available to the new docket
with self.captureOnCommitCallbacks(execute=True):
entry = DocketEntryWithParentsFactory(
docket=docket,
entry_number=1,
date_filed=datetime.date(2015, 8, 19),
description="MOTION for Leave to File Amicus Curiae Lorem",
)
recap_document = RECAPDocumentFactory(
docket_entry=entry,
description="New File",
document_number="1",
is_available=False,
page_count=5,
)

# Query all documents but only show results with PDFs
params = {"type": SEARCH_TYPES.RECAP, "available_only": True}
async_to_sync(self._test_article_count)(params, 1, "available_only")

# Repeat the text query using the name of the new docket
params = {
"type": SEARCH_TYPES.RECAP,
"q": "Reese",
"available_only": True,
}
async_to_sync(self._test_article_count)(params, 0, "available_only")

# Update the status of the document to reflect it's available
with self.captureOnCommitCallbacks(execute=True):
recap_document.is_available = True
recap_document.save()

# Query all documents but only show results with PDFs
params = {"type": SEARCH_TYPES.RECAP, "available_only": True}
async_to_sync(self._test_article_count)(params, 2, "available_only")

# Repeat text search, 1 result expected since the doc is available now
params = {
"type": SEARCH_TYPES.RECAP,
"q": "Reese",
"available_only": True,
}
async_to_sync(self._test_article_count)(params, 1, "available_only")

docket.delete()

async def test_party_name_filter(self) -> None:
"""Confirm party_name filter works properly"""
Expand Down

0 comments on commit a49bb52

Please sign in to comment.