From 8538821c2481a9101e9e9e29058dac6f1770bb22 Mon Sep 17 00:00:00 2001 From: d42 Date: Sat, 28 Apr 2018 00:06:06 +0200 Subject: [PATCH] search contains --- storage/views.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/storage/views.py b/storage/views.py index 2b62122..dafe2b4 100644 --- a/storage/views.py +++ b/storage/views.py @@ -6,6 +6,7 @@ from django.http import Http404, JsonResponse from django.contrib.admin.models import LogEntry from django_select2.views import AutoResponseView from django.db import connection +from django.db.models import Q from storage.models import Item, Label @@ -41,15 +42,13 @@ def apply_smart_search(query, objects): if not general_term: return objects - objects = objects.annotate( - search=SearchVector('name', 'description', 'props', config='simple'), - ) general_term = ' '.join(general_term) objects = objects.annotate( + search=SearchVector('name', 'description', 'props', config='simple'), similarity=TrigramSimilarity('name', general_term) ).filter( - similarity__gte=0.15 + Q(similarity__gte=0.15) | Q(search__contains=general_term) ).order_by('-similarity') return objects