Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:enhance search functionality with partial matches #50

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions orp/orp_search/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,33 @@ def search_database(
# Search across specific fields
vector = SearchVector("title", "description", "regulatory_topics")

queryset = DataResponseModel.objects.all()

if query_objs:
queryset = DataResponseModel.objects.annotate(search=vector).filter(
search=query_objs,
# Treat the query for partial and full-text search
query_chunks = query_str.split()
search_vector = SearchVector(
"title", "description", "regulatory_topics"
)
queryset = queryset.annotate(search=search_vector)

# Creating a combined SearchQuery object from chunks
search_queries = [
SearchQuery(chunk, search_type="plain") for chunk in query_chunks
]
combined_query = search_queries[0]
for sq in search_queries[1:]:
combined_query |= sq

partial_matches = Q()
for chunk in query_chunks:
partial_matches |= (
Q(title__icontains=chunk)
| Q(description__icontains=chunk)
| Q(regulatory_topics__icontains=chunk)
)

queryset = queryset.filter(partial_matches | Q(search=combined_query))
else:
queryset = DataResponseModel.objects.annotate(search=vector)

Expand Down