Reputation: 87
I am currently using PostgreSQL, Python, and Django for my personal project. The webpage has a search bar which I got working using SearchVector. The problem I'm having is that my search bar function and request.GET method returns a tuple, and search vector searches for that exact phrase in my database. Basically single word searches work fine, but as soon as someone puts in another word or phrase, that exact phrase must be in the database or nothing is returned.
For example: I put the word "god" into the search and all entries with "god" show up fine. But I put the phrase "god snake" into the search and nothing comes up even though there are entries with those words in them.
Here is my search bar function:
def searchbar(request):
if request.method == "GET":
search = request.GET["search"],
print(search)
print(search[0])
deity = Deity.objects.annotate(search=SearchVector('name', 'location',
'alt_name', 'culture', 'religion', 'description',
'pop_culture'),).filter(search=SearchQuery(search[0], search_type='phrase')),
context = {
"user": User.objects.get(id = request.session['user_id']),
"deity": deity,
"search": search[0],
}
return render(request, 'search_results.html', context)
Upvotes: 1
Views: 207