def search(self, query=None, **kwargs): filters = {} fine_filter = {} if query: query = SearchQuery(query.strip(), config=SEARCH_LANG) fine_filter['search_vector__startswith'] = query filters['fines__search_vector__startswith'] = query amount_lte = kwargs.pop('amount__lte', None) amount_gte = kwargs.pop('amount__gte', None) for key, val in kwargs.items(): if val is not None and val != '': filters['fines__%s' % key] = val q = Organisation.objects.filter(**filters) if filters or amount_lte is not None or amount_gte is not None: amount_col = 'filtered_amount' q = q.annotate( filtered_amount=Sum('fines__amount'), fine_count=Count('fines') ) a_filters = {} if amount_lte is not None: a_filters['filtered_amount__lte'] = amount_lte if amount_gte is not None: a_filters['filtered_amount__gte'] = amount_gte final_q = q if a_filters: final_q = final_q.filter(**a_filters) else: amount_col = 'sum_fines' final_q = q.annotate( fine_count=Count('fines') ) ordering = '-%s' % amount_col ordered_final_q = final_q.order_by(ordering) fines = Fine.objects.filter(**fine_filter).order_by() aggs = { 'total_sum': final_q.aggregate( total_sum=Sum(amount_col))['total_sum'] or 0.0, 'max_amount': q.aggregate( max_amount=Max(amount_col))['max_amount'] or 0.0, 'years': fines.values('year').annotate( doc_count=Count('year')), 'states': fines.values('state').annotate( doc_count=Count('state')), 'doc_count': fines.count() } return ordered_final_q, aggs
def get_queryset(self): self.query = self.request.GET.get("q") year = self.request.GET.get("year", None) query = None ordering = ('title', 'id') distinct_on = ('title', 'id') if (self.query[0] == "'" or self.query[0] == '"') and self.query[0] == self.query[-1]: query = SearchQuery(self.query) else: query_words = self.query.split() query = SearchQuery(query_words[0]) for word in query_words[1:]: query.bitor(SearchQuery(word)) vector = ( SearchVector('title', weight='A') + SearchVector('description', weight='C') + SearchVector('directors__last_name', weight='B') + SearchVector('countries__name', weight='B') ) qs = self.model.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.12) if qs.count() != 0: ordering = ('-rank',) + ordering distinct_on = ('rank',) + distinct_on else: qs = Film.objects.annotate(similarity=TrigramSimilarity('title', self.query)).filter(similarity__gt=0.25) ordering = ('-similarity',) + ordering distinct_on = ('similarity',) + distinct_on if year: try: year_val = int(year) qs = qs.filter(year=year_val) except ValueError: pass return qs.order_by(*ordering).distinct(*distinct_on)
def get(self, request): if 'query' in request.GET: form = self.form(request.GET) if form.is_valid(): query = form.cleaned_data.get('query') search_vector = SearchVector( 'title', weight='A') + SearchVector('body', weight='B') search_query = SearchQuery(query) posts = Post.objects.filter(is_publish=True) results = posts.annotate( rank=SearchRank(search_vector, search_query)).filter( rank__gte=0.3).order_by('-rank') return render(request, self.template_name, { 'form': form, 'posts': results }) else: form = self.form() return render(request, self.template_name, {'form': form}) else: form = self.form() return render(request, self.template_name, {'form': form})
def post_search(request): form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] #results = Post.objects.annotate(search=SearchVector('title', 'body'),).filter(search=query) search_vector = SearchVector('title', weight='A') + SearchVector( 'body', weight='B') search_query = SearchQuery(query) results = Post.objects.annotate( search=search_vector, rank=SearchRank( search_vector, search_query)).filter(rank__gte=0.3).order_by('-rank') return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })
def get_queryset(self): queryset = Post.objects.all().prefetch_related(Prefetch('author'), Prefetch('category')) keywords = self.request.GET.get('q') sort = self.request.GET.get('sort') if keywords: print(keywords) query = SearchQuery(keywords) title_vector = SearchVector('title', weight='A') content_vector = SearchVector('content', weight='B') vectors = title_vector + content_vector queryset = queryset.annotate(search=vectors).filter(search=query) queryset = queryset.annotate( rank=SearchRank(vectors, query)).order_by('-rank') if sort: print(sort) queryset = queryset.order_by(sort) return queryset
def post_search(request): """ Crear funcion de busqueda de posts llamando el form SearchForm Cuando el formulario es emitido,mandas el formulario usando el metodo GET atraves del POST. Cuando el formulario es emiido se instancia con los datos enviados por GET y verifica que los datos sean validos, si el formulario es valido puedes buscar los post publicados con la instancia custom SearchVector construida con los campos titulo y body Se aplican diferentes weights a la busqueda construida por el title y body los weights defaults son D, C, B y A y se refieren a numeros 0.1, 0.2, 0.4 y 1.0 respectivamente Puedes aplicar un weight de 1.0 para el title y weight de 04 al body los resultados de title van a prevalecer sobre el contenido compatible con el body Se filtra el resultado para mostrar solo los que tengan el rank mas alto de 0.3 """ form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] search_vector = SearchVector('title', weight='A') + SearchVector( 'body', weight='B') search_query = SearchQuery(query) results = Post.published.annotate( search=search_vector, rank=SearchRank( search_vector, search_query)).filter(rank__gte=0.3).order_by('-rank') """results = Post.published.annotate( search=search_vector, rank=SearchRank(search_vector, search_query) ).filter(search=search_query).order_by('-rank')""" #results = Post.published.annotate(search=SearchVector('title', 'body'),).filter(search=query) return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })
def post_search(request): form = BuscarForm() query = None results = [] if 'palabras' in request.GET: form = BuscarForm(request.GET) if form.is_valid(): query = form.cleaned_data['palabras'] search_vector = SearchVector('titulo', weight='A') + SearchVector( 'contenido', weight='B') search_query = SearchQuery(query) results = Post.objectos.annotate( # search=search_vector, # rank=SearchRank(search_vector, search_query) # ).filter(rank__gte=0.3).order_by('-rank') similarity=TrigramSimilarity('titulo', 'query')).filter( similarity=0.3).order_by('-similarity') return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })
def post_search(request): form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] search_vector = SearchVector('title', weight='A') + SearchVector( 'body', weight='B') search_query = SearchQuery(query) results = Post.published.annotate( #similarity=TrigramSimilarity('title', query), search=search_vector, rank=SearchRank(search_vector, search_query), ).filter(rank__gte=0.3).order_by('-rank') #).filter(similarity__gt=0.1).order_by('-similarity') return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })
def simpleTaskSearch(query_params): queryset = Task.objects.filter(is_trashed=False) team = query_params.get('team', None) if team is not None: queryset = queryset.filter(process_type__team_created_by=team) tags = query_params.get('tags', None) if tags is not None: tag_names = tags.strip().split(',') queryset = queryset.filter(process_type__tags__name__in=tag_names) | \ queryset.filter(product_type__tags__name__in=tag_names) label = query_params.get('label', None) dashboard = query_params.get('dashboard', None) if label is not None and dashboard is not None: queryset = queryset.filter(Q(keywords__icontains=label)) elif label is not None: query = SearchQuery(label) queryset = queryset.filter(Q(search=query) | Q(label__istartswith=label) | Q(custom_display__istartswith=label)) return queryset\ .order_by('-updated_at').select_related('process_type', 'product_type').prefetch_related('items')
def perform_query(text): current_search = Search.objects.create() vector = (SearchVector('text', weight='A') + SearchVector('debate__title', weight='B')) query = SearchQuery(text) search_results = BlockQuote.objects.annotate( rank=SearchRank(vector, query)).filter(rank__gte=0.3).order_by('-rank') results = [] # Create search query object search = PrimitiveQuery.objects.create(search=current_search, term=text) for search_result in search_results: result = PrimitiveQueryResult.objects.create(quote=search_result, query=search) result.create_excerpt() result.save() current_search.completed = datetime.datetime.now() current_search.finished = True current_search.save()
def busqueda(request): message = "No se encontraron coincidencias" resultados = [] q = '' if request.method == "GET" and request.GET.get('q', None): q = request.GET['q'].strip() try: n = int(q) resultados = Url.objects.filter( Q(codigo=n) | Q(dominio__asn__asn=n)).order_by( 'url', '-timestamp_creacion').distinct('url') return render(request, 'busqueda.html', { 'resultados': resultados, 'query': q }) except Exception as e: pass resultados = Url.objects.annotate( search=SearchVector('url') + SearchVector('dominio__dominio') + SearchVector('dominio__rir__nombre') + SearchVector('dominio__dns__nombre') + SearchVector('dominio__ip') + SearchVector('dominio__pais') + SearchVector('dominio__asn__nombre') + SearchVector('dominio__correos__correo') + SearchVector('dominio__isp') + SearchVector('dominio__servidor') + SearchVector('urlactiva__titulo') + SearchVector('urlactiva__entidad_afectada__nombre') + SearchVector( 'urlactiva__entidad_afectada__clasificacion__nombre') + SearchVector('urlactiva__ofuscaciones__nombre') + SearchVector('urlactiva__hash_archivo') + SearchVector('urlredireccion__redireccion__url') + SearchVector('ticket__ticket') + SearchVector('urlredireccion__redireccion_final__url')).filter( search=SearchQuery(q)).order_by( 'url', '-timestamp_creacion').distinct('url') return render(request, 'busqueda.html', { 'resultados': resultados, 'query': q })
def product_search(qs, phrase): """Return matching products for storefront views. Name and description is matched using search vector. Args: qs (ProductsQueryset): searched data set phrase (str): searched phrase """ query = SearchQuery(phrase, config="english") vector = F("search_vector") ft_in_description_or_name = Q(search_vector=query) variants = ProductVariant.objects.filter(sku=phrase).values("id") ft_by_sku = Q(Exists(variants.filter(product_id=OuterRef("pk")))) return ( qs.annotate(rank=SearchRank(vector, query)) .filter((ft_in_description_or_name | ft_by_sku)) .order_by("-rank", "id") )
def query_list(q): q = q.split(",") queries = [SearchQuery(query) for query in q] query = reduce(operator.or_, queries) vector = SearchVector('name', weight='A') + SearchVector('description', weight='B') rank_metric = 0.2 results_page = Page.objects.annotate( rank=SearchRank(vector, query)).filter( rank__gte=rank_metric, is_sponsored=False, deleted=False, stripe_verified=True).order_by('-rank') sponsored_page = Page.objects.annotate( rank=SearchRank(vector, query)).filter( rank__gte=rank_metric, is_sponsored=True, deleted=False, stripe_verified=True).order_by('-rank') results_campaign = Campaign.objects.annotate( rank=SearchRank(vector, query)).filter( rank__gte=rank_metric, page__is_sponsored=False, deleted=False, is_active=True).order_by('-rank') sponsored_campaign = Campaign.objects.annotate( rank=SearchRank(vector, query)).filter( rank__gte=rank_metric, page__is_sponsored=True, deleted=False, is_active=True).order_by('-rank') results = list(chain(results_page, results_campaign)) sponsored = list(chain(sponsored_page, sponsored_campaign)) results.sort(key=lambda x: x.rank, reverse=True) sponsored.sort(key=lambda x: x.rank, reverse=True) return (results, sponsored)
def filter_queryset(self, request, queryset, view): if view.detail: return queryset # this is the compicated part, we emply both trigram similarity and full text search here # see https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/search/ # and http://rachbelaid.com/postgres-full-text-search-is-good-enough/ query = request.GET.get('query') if query: # first, split the search string along _ and whitespace into words search_words = query.replace('_', ' ').replace('-', ' ').replace( '/', ' ').split() # second, lookup similar words in the "words" table and join them with OR search_strings = [] for search_word in search_words: words = Word.objects.using('metadata') \ .annotate(similarity=TrigramSimilarity('word', search_word)) \ .filter(similarity__gt=settings.SEARCH_SIMILARITY) \ .order_by('-similarity') \ .values_list('word', flat=True)[:settings.SEARCH_SIMILARITY_LIMIT] if words: search_strings.append('(%s)' % ' | '.join(words)) # next, join the search_strings for the different search_words with an AND search_string = ' & '.join(search_strings) logger.debug('search_string = %s', search_string) # get the search_query and the search_rank objects search_query = SearchQuery(search_string, search_type='raw') search_rank = SearchRank('search_vector', search_query) # last, perform a full text search with ranking on the search_vector field queryset = queryset.filter(search_vector=search_query).annotate( search_rank=search_rank, ).order_by('-search_rank', 'name') return queryset
def get(self, request, *args, **kwargs): keywords = request.query_params.get('keywords', '') keywords = urllib.parse.unquote(keywords) ctx = {'request': request} query = SearchQuery(keywords) # Posts post_vector = (SearchVector('title', weight='A') + SearchVector('body', weight='C')) posts = (Post.objects.annotate( rank=SearchRank(post_vector, query)).filter( rank__gte=0.1).distinct('id')) posts = sorted(posts, key=operator.attrgetter('rank'), reverse=True) posts_serializer = PostSerializer(posts, context=ctx, many=True) # Pages page_vector = (SearchVector('title', weight='A') + SearchVector('sub_title', weight='B') + SearchVector('body', weight='C')) pages = (Page.objects.annotate( rank=SearchRank(page_vector, query)).filter(rank__gte=0.1)) pages = sorted(pages, key=operator.attrgetter('rank'), reverse=True) pages_serializer = PageSerializer(pages, context=ctx, many=True) # Combine into Response results = { 'posts': posts_serializer.data, 'pages': pages_serializer.data, } return Response(results, status.HTTP_200_OK)
def search(request): if not request.method == 'POST': if 'search-post' in request.session: request.POST = request.session['search-post'] request.method = 'POST' if request.method == 'POST': request.session['search-post'] = request.POST keywords = request.POST.get('search_input', '') vector = SearchVector('text', 'title') query = SearchQuery(keywords) news_list = NewsData.objects.annotate( rank=SearchRank(vector, query)).filter( rank__gte=0.04).order_by('-rank').order_by("-pub_date") #news_list = NewsData.objects.annotate(similarity=TrigramSimilarity('text', keywords),).filter(similarity__gt=0.3).order_by('-similarity') paginator = Paginator(news_list, 6) # Show 25 contacts per page page = request.GET.get('page') try: news = paginator.page(page) except PageNotAnInteger: news = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. news = paginator.page(paginator.num_pages) for n in news: soup = BeautifulSoup(n.text) n.text = (soup.text)[:130] + ' ...' n.text = n.text.replace(" ", " ") return render( request, 'searchnews.html', { 'searchnews': news, "keywords": keywords, 'partners': Partners.objects.all(), 'contacts': Contacts.objects.all()[0] })
def get_queryset(self): queryset = Event.objects.all() if 'not_expired' in self.request.query_params: queryset = queryset.filter(event_end_time__gt=datetime.utcnow()) if 'search' in self.request.query_params: search_vector = SearchVector('event_name', weight='A') + SearchVector('event_description', weight='A') + SearchVector('event_address', weight='B') search_query = SearchQuery(self.request.query_params['search']) queryset = queryset.annotate(rank=SearchRank(search_vector, search_query)).filter(rank__gte=0.25).order_by('-rank') if 'lon' in self.request.query_params and 'lat' in self.request.query_params: #pdb.set_trace() ref_location = Point(float(self.request.query_params['lon']), float(self.request.query_params['lat'])) queryset = queryset.filter( event_point_location__distance_lt=(ref_location, D(km=500))).annotate( distance=Distance('event_point_location', ref_location)).order_by( 'distance') return queryset
def airman_search(request): form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] search_vector = SearchVector( 'first_name', 'last_name', 'ssn', ) search_query = SearchQuery(query) results = Airman.active.annotate( search=search_vector, ranking=SearchRank(search_vector, search_query)).filter( search=search_query).order_by('-ranking') return render(request, 'unit/airman/search.html', { 'form': form, 'query': query, 'results': results })
def post_search(request): if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] search_vector = SearchVector('title', 'body') search_query = SearchQuery(query) results = Post.published.annotate( search=search_vector, rank=SearchRank(search_vector, search_query)).filter( search=search_query).order_by('-rank') else: form = SearchForm() query = None results = [] return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })
def search_page(request): search_form = SearchForm() query = None results = [] if 'query' in request.GET: search_form = SearchForm(request.GET) if search_form.is_valid(): query = search_form.cleaned_data['query'] search_vector = SearchVector('title', 'body', 'tags') search_query = SearchQuery(query) results = Post.published.annotate( search=search_vector, search_rank=SearchRank(search_vector, search_query)).filter( search=query).order_by('-search_rank') return render(request, 'pages/page/search.html', { 'search_form': search_form, 'query': query, 'results': results })
def search_podcasts(query): """ Search for podcasts according to 'query' """ logger.debug('Searching for "{query}" podcasts"', query=query) query = SearchQuery(query) results = ( Podcast.objects.annotate(rank=SearchRank(F('search_vector'), query)) .annotate( order=ExpressionWrapper( F('rank') * F('subscribers'), output_field=FloatField() ) ) .filter(rank__gte=SEARCH_CUTOFF) .order_by('-order') ) logger.debug( 'Found {count} podcasts for "{query}"', count=len(results), query=query ) return results
def post_search(request): form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] search_vector = SearchVector('title', weight='A') + SearchVector('body', weight='B') search_query = SearchQuery(query) ## For trigram search ## If using postgres, open DB shell and enable extension: `CREATE EXTENSION pg_trgm` results = Post.objects.annotate(similarity=TrigramSimilarity('title', query),).filter(similarity__gt=0.3).order_by('-similarity') ## For stem search # results = Post.objects.annotate(search=search_vector, rank=SearchRank(search_vector, search_query)).filter(rank__gte=0.3).order_by('-rank') return render(request, 'blog/post/search.html', {'form': form, 'query': query, 'results': results})
def post_search(request): form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] search_vector = SearchVector('title', 'body'), search_query = SearchQuery(query) # results = Post.objects.annotate( # search=search_vector, # rank=SearchRank(search_vector, search_query) # ).filter(search=query).order_by('-rank') results = Post.objects.annotate( similarity=TrigramSimilarity('title', query)).filter( similarity__gt=0.3).order_by('-similarity') return render(request, 'post/search.html', { 'form': form, 'query': query, 'results': results })
def get_available_realty_search_results( query: Optional[str] = None) -> 'CustomDeleteQueryset[Realty]': """ Get all available realty filtered by a `query`. If `query` isn't passed, return all available realty objects. Args: query(Optional[str]): search query Returns: CustomDeleteQueryset[Realty]: filtered realty """ if query: search_vector = SearchVector('location__city', weight='A') + \ SearchVector('name', weight='B') + \ SearchVector('description', weight='C') search_query = SearchQuery(query) return Realty.available.annotate(rank=SearchRank( search_vector, search_query), ).filter(rank__gte=0.3).order_by('-rank') return Realty.available.all()
def search_podcasts(query): """ Search for podcasts according to 'query' """ if is_query_too_short(query): logger.debug('Found no podcasts for "{query}". Query is too short', query=query) return Podcast.objects.none() logger.debug('Searching for "{query}" podcasts"', query=query) query = SearchQuery(query) results = (Podcast.objects.annotate( rank=SearchRank(F("search_vector"), query)).annotate( order=ExpressionWrapper( F("rank") * F("subscribers"), output_field=FloatField())).filter( rank__gte=SEARCH_CUTOFF).order_by("-order")[:100]) logger.debug('Found {count} podcasts for "{query}"', count=len(results), query=query) return results
def get_queryset(self): if self.kwargs.get('tag_slug', None): self.tags = get_object_or_404(Tag, slug=self.kwargs['tag_slug']) return Post.published.filter(tags__in=[self.tags]) form = self.form_class(self.request.GET) if form.is_valid(): query = form.cleaned_data['query'] self.query = query # Try Postgres SearchVector over Body and Title search_vector = SearchVector('title', weight='A') + SearchVector('body', weight='B') search_query = SearchQuery(query) results = (Post.published.annotate( search=search_vector, rank=SearchRank(search_vector, search_query)) .filter(search=search_query).order_by('-rank')) if results: return results # Fallback to TrigramSimilarity if there was no results return (Post.published.annotate( similarity=TrigramSimilarity('title', query) )#.filter(similarity__gt=0.01) .order_by('-similarity')) return Post.published.all()
def post_search(request): form = SearchForm() # создаем объект формы SearchForm query = None results = [] if 'query' in request.GET: # Поисковый запрос будет отправляться методом GET, чтобы результирующий URL содержал в себе фразу поиска в параметре query. # Для того чтобы определить, отправлена ли форма для поиска, обращаемся к параметру запроса query из словаря request.GET form = SearchForm( request.GET ) # Когда запрос отправлен, мы инициализируем объект формы с параметрами из request.GET, if form.is_valid(): # проверяем корректность введенных данных query = form.cleaned_data['query'] search_vector = SearchVector('title', weight='A') + SearchVector( 'body', weight='B' ) # формируем запрос c разным весом для полей: title и body; # По умолчанию используются веса D, C, B и A, которые соответствуют числам 0.1, 0.2, 0.4 и 1. # для вектора по полю title применили вес 1.0, для вектора по полю body - 0.4. search_query = SearchQuery(query) results = Post.objects.annotate( similarity=TrigramSimilarity('title', query)).filter( similarity__gt=0.3).order_by('-similarity') context = {'form': form, 'query': query, 'results': results} return render(request, 'blog/post/search.html', context=context)
def search_contracts(query, service_id): filter_kwargs = {} if query: filter_kwargs["search_vector"] = SearchQuery(query) if service_id: filter_kwargs["service_id"] = service_id if not filter_kwargs: return [] return ( Contract.objects.select_related("document", "entity", "service") .prefetch_related("contractors") .defer( "document__pages", "document__preview_data_file", "document__vision_data_file", ) .filter(**filter_kwargs) .order_by("-date_of_grant") )
def get_articles_feed_queryset(description=''): if description == '': articles = FeedObject.objects.filter( Q(profile_status__status=True) | Q(article__status=Article.STATUS_PUBLISH)).prefetch_related( "content_object", "content_object__author", "content_type", "communities", "communities__taxonomy").order_by( "-date", "id").distinct("id", "date", "article__publishin") else: main_criteria = get_feed_main_criteria() query = SearchQuery(description) articles = FeedObject.objects.annotate( rank=SearchRank(Article.VECTOR + ProfileStatus.VECTOR, query) ).filter(main_criteria & (Q(profile_status__search_vector=query) | Q(article__search_vector=query))).prefetch_related( "content_object", "content_object__author", "content_type", "communities", "communities__taxonomy").order_by('-rank', "id") return articles
def post_search(request): # Поиск постов по названию и тексту form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] # Установка большего веса вектора поиска для title для повышения релевантности search_vector = SearchVector('title', weight='A') + \ SearchVector('body', weight='B') search_query = SearchQuery(query) results = Post.published.annotate( search=search_vector, rank=SearchRank( search_vector, search_query)).filter(rank__gte=0.3).order_by('-rank') return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })
def filter_by_q(self, queryset, name, value): """ Full text search provided by the 'q' option. Args: queryset: The query to add the additional full-text search filtering onto name: The name of the option specified, i.e. 'q' value: The string to search on Returns: The Django queryset that was passed in, additionally filtered by full-text search. """ search_query = SearchQuery(value) qs = queryset.filter(search_vector=search_query) ts_rank_fn = Func( F("search_vector"), search_query, 32, # RANK_NORMALIZATION = 32 function="ts_rank", output_field=db_fields.FloatField(), ) return qs.annotate(rank=ts_rank_fn).order_by("-rank")
def post_serach(request): form = SerachForm() query = None results = [] if 'query' in request.GET: form = SerachForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] # A B C D => 1.0 , 0.4 , 0.2 , 0.1 search_vector = SearchVector('title', weight='A') + SearchVector( 'body', weight="C") serach_query = SearchQuery(query) results = Post.published.annotate( search=search_vector, rank=SearchRank( search_vector, serach_query)).filter(rank__gte=0.5).order_by('-rank') return render(request, 'blog/post/search.html', { 'form': form, 'query': query, 'results': results })