From 55cccfee33794d2e43b4f6a03c5122ce0c369734 Mon Sep 17 00:00:00 2001 From: Kateryna Stetsenko Date: Wed, 6 Nov 2024 11:31:16 -0800 Subject: [PATCH] Enhanced candidate search to support multi-word queries across multiple fields - Split term into individual words to improve search - Applied individual filters for each word across multiple fields - Add descriptive comment about search --- candidate/views_admin.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/candidate/views_admin.py b/candidate/views_admin.py index d3609d9e8..e6c2cf9a2 100644 --- a/candidate/views_admin.py +++ b/candidate/views_admin.py @@ -128,31 +128,35 @@ def candidates_sync_out_view(request): # candidatesSyncOut elif positive_value_exists(state_code): queryset = queryset.filter(state_code__iexact=state_code) queryset = queryset.filter(candidate_year=current_year) - filters = [] if positive_value_exists(candidate_search): - new_filter = Q(candidate_name__icontains=candidate_search) - filters.append(new_filter) + # search for candidates based on search string + search_words = candidate_search.split() + filters = [] - new_filter = Q(candidate_twitter_handle__icontains=candidate_search) - filters.append(new_filter) + for word in search_words: + new_filter = Q(candidate_name__icontains=word) + filters.append(new_filter) - new_filter = Q(candidate_twitter_handle2__icontains=candidate_search) - filters.append(new_filter) + new_filter = Q(candidate_twitter_handle__icontains=word) + filters.append(new_filter) + + new_filter = Q(candidate_twitter_handle2__icontains=word) + filters.append(new_filter) - new_filter = Q(candidate_twitter_handle3__icontains=candidate_search) - filters.append(new_filter) + new_filter = Q(candidate_twitter_handle3__icontains=word) + filters.append(new_filter) - new_filter = Q(candidate_url__icontains=candidate_search) - filters.append(new_filter) + new_filter = Q(candidate_url__icontains=word) + filters.append(new_filter) - new_filter = Q(candidate_contact_form_url__icontains=candidate_search) - filters.append(new_filter) + new_filter = Q(candidate_contact_form_url__icontains=word) + filters.append(new_filter) - new_filter = Q(party__icontains=candidate_search) - filters.append(new_filter) + new_filter = Q(party__icontains=word) + filters.append(new_filter) - new_filter = Q(we_vote_id=candidate_search) - filters.append(new_filter) + new_filter = Q(we_vote_id=word) + filters.append(new_filter) # Add the first query if len(filters):