Skip to content

Commit

Permalink
chore:enhance search functionality with combined search terms
Browse files Browse the repository at this point in the history
Added a new utility function `combine_search_terms` to merge `AND` and `OR` search terms into a single expression. Updated `SearchDocumentConfig` to use this combined expression and modified `search` method in `legislation.py` to use the new combined search terms. This improves query precision and flexibility.
  • Loading branch information
hareshkainthdbt committed Oct 30, 2024
1 parent 0674624 commit 4dc1722
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 4 additions & 1 deletion orp/orp_search/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from orp_search.utils.terms import parse_search_terms
from orp_search.utils.terms import combine_search_terms, parse_search_terms

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -41,6 +41,9 @@ def __init__(
search_terms_and, search_terms_or = parse_search_terms(search_terms)
self.search_terms_and = search_terms_and
self.search_terms_or = search_terms_or
self.final_search_expression = combine_search_terms(
search_terms_and, search_terms_or
)

def validate(self):
"""
Expand Down
10 changes: 6 additions & 4 deletions orp/orp_search/legislation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ def __init__(self):
def search(self, config: SearchDocumentConfig):
logger.info("searching legislation...")

logger.info(
f"final_search_expression terms: {config.final_search_expression}"
)

# List of search terms
title_search_terms = config.search_terms
search_terms = ",".join(title_search_terms)
headers = {"Accept": "application/atom+xml"}
params = {
"lang": "en",
"title": search_terms,
"text": search_terms,
"title": config.final_search_expression,
"text": config.final_search_expression,
"results-count": 20,
}

Expand Down
17 changes: 17 additions & 0 deletions orp/orp_search/utils/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,20 @@ def parse_search_terms(search):
search_terms_or.append(token)

return search_terms_and, search_terms_or


def combine_search_terms(search_terms_and, search_terms_or):
# Join terms in `search_terms_and` with " AND "
combined_and = " AND ".join(search_terms_and) if search_terms_and else ""

# Join terms in `search_terms_or` with " OR "
combined_or = " OR ".join(search_terms_or) if search_terms_or else ""

# Combine both parts, adding parentheses around each if both are present
if combined_and and combined_or:
combined_query = f"{combined_and} OR {combined_or}"
else:
# Use whichever part is non-empty
combined_query = combined_and or combined_or

return combined_query

0 comments on commit 4dc1722

Please sign in to comment.