Skip to content

Commit

Permalink
Refactor document search to use database query.
Browse files Browse the repository at this point in the history
Replaced the previous external `PublicGateway` search implementation with an internal database search using `search_database`. This change improves performance and consistency by leveraging Django's ORM for querying documents.
  • Loading branch information
hareshkainthdbt committed Nov 19, 2024
1 parent e7970e5 commit 8f44e5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
15 changes: 13 additions & 2 deletions orp/orp_search/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,20 @@ def _create_search_query(search_string):
return preprocess_query


def _search_database(
def search_database(
config: SearchDocumentConfig,
) -> QuerySet[DataResponseModel]:
"""
Search the database for documents based on the search query
:param config: The search configuration object
:return: A QuerySet of DataResponseModel objects
"""

# If an id is provided, return the document with that id
if config.id:
return DataResponseModel.objects.filter(id=config.id)

# Sanatize the query string
query_str = sanitize_input(config.search_query)
logger.info(f"sanitized search query: {query_str}")
Expand Down Expand Up @@ -125,7 +136,7 @@ def search(context: dict, request: HttpRequest) -> dict:
config.print_to_log()

# Search across specific fields
results = _search_database(config)
results = search_database(config)

# convert search_results into json
pag_start_time = time.time()
Expand Down
22 changes: 5 additions & 17 deletions orp/orp_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from orp_search.config import SearchDocumentConfig
from orp_search.models import DataResponseModel
from orp_search.public_gateway import PublicGateway
from orp_search.utils.search import search
from orp_search.utils.search import search, search_database

from django.conf import settings
from django.core.serializers import serialize
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
Expand Down Expand Up @@ -38,22 +38,10 @@ def document(request: HttpRequest, id) -> HttpResponse:
config = SearchDocumentConfig(search_query="", id=document_id)

# Use the PublicGateway class to fetch the details
public_gateway = PublicGateway()
try:
search_result = public_gateway.search(config)
# logger.info("search result: %s", search_result)

if "regulatory_topics" in search_result:
search_result["regulatory_topics"] = str(
search_result["regulatory_topics"]
).split("\n")

if "related_legislation" in search_result:
search_result["related_legislation"] = str(
search_result["related_legislation"]
).split("\n")

context["result"] = search_result
try:
queryset = search_database(config)
context["result"] = serialize("json", queryset)
return render(request, template_name="document.html", context=context)
except Exception as e:
logger.error("error fetching details: %s", e)
Expand Down

0 comments on commit 8f44e5c

Please sign in to comment.