本文整理汇总了Python中django.contrib.postgres.search.SearchRank方法的典型用法代码示例。如果您正苦于以下问题:Python search.SearchRank方法的具体用法?Python search.SearchRank怎么用?Python search.SearchRank使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.search
的用法示例。
在下文中一共展示了search.SearchRank方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_text
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def filter_text(self, queryset, name, value):
"""Full-text search."""
query = SearchQuery(value, config="simple")
return (
queryset.filter(**{name: query})
# This assumes that field is already a TextSearch vector and thus
# doesn't need to be transformed. To achieve that F function is
# required.
.annotate(rank=SearchRank(F(name), query)).order_by("-rank")
)
示例2: search_taxonomy_node
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def search_taxonomy_node(request, short_name):
if not request.user.is_authenticated:
return HttpResponse('Unauthorized', status=401)
dataset = get_object_or_404(Dataset, short_name=short_name)
if not dataset.user_is_maintainer(request.user):
raise HttpResponseNotAllowed
taxonomy = dataset.taxonomy
query = request.GET.get('q', '')
# vector = SearchVector('name', weight='A') + SearchVector('description', weight='C')
# query = SearchQuery(query)
# qs_results = TaxonomyNode.objects.filter(taxonomy__dataset=dataset)\
# .annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.3)\
# .order_by('rank')
qs_results = TaxonomyNode.objects.filter(taxonomy__dataset=dataset)\
.annotate(similarity=TrigramSimilarity('name', query) +
TrigramSimilarity('description', query),)\
.filter(similarity__gte=0.2)\
.order_by('-similarity')
results = [{'name': node.name,
'node_id': node.node_id,
'path': ' > '.join([TaxonomyNode.objects.get(node_id=node_id).name for node_id in path_list]),
'big_id': ','.join(path_list),
'omitted': node.omitted_curation_task}
for node in qs_results
for path_list in taxonomy.get_hierarchy_paths(node.node_id)]
return JsonResponse(results[:10], safe=False)
#############################
# CONTRIBUTE TO DATASET VIEWS
#############################
示例3: build_tsrank
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def build_tsrank(self, vector, query, config=None, boost=1.0):
if isinstance(query, (Phrase, PlainText, Not)):
rank_expression = SearchRank(
vector,
self.build_tsquery(query, config=config),
weights=self.sql_weights
)
if boost != 1.0:
rank_expression *= boost
return rank_expression
elif isinstance(query, Boost):
boost *= query.boost
return self.build_tsrank(vector, query.subquery, config=config, boost=boost)
elif isinstance(query, And):
return MUL(
1 + self.build_tsrank(vector, subquery, config=config, boost=boost)
for subquery in query.subqueries
) - 1
elif isinstance(query, Or):
return ADD(
self.build_tsrank(vector, subquery, config=config, boost=boost)
for subquery in query.subqueries
) / (len(query.subqueries) or 1)
raise NotImplementedError(
'`%s` is not supported by the PostgreSQL search backend.'
% query.__class__.__name__)
示例4: filter_queryset
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def filter_queryset(self, request, queryset, view):
search_fields = getattr(view, 'word_fields', None)
if not search_fields:
return queryset
search_term = request.query_params.get(self.search_param, '').split()
if not search_term:
return queryset
for ind, field in enumerate(search_fields):
if ind==0:
vector = SearchVector(field, config='spanish_unaccent')
else:
vector = vector + SearchVector(field, config='spanish_unaccent')
for ind, term in enumerate(search_term):
if ind==0:
query = SearchQuery(term, config='spanish_unaccent')
else:
query = query & SearchQuery(term, config='spanish_unaccent')
# [D, C, B, A] --> A = 0.8, B = 0.6
rank = SearchRank(vector, query, weights=[0.2, 0.4, 0.6, 0.8])
queryset = queryset.annotate(rank=rank).filter(rank__gte=0.1)
return queryset
示例5: ranked_search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def ranked_search(self, terms):
return list(self.objects
.annotate(rank=SearchRank(F('search'), SearchQuery(terms, config='english')))
.order_by('-rank')
.values_list('id', flat=True))
示例6: search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def search(request):
query_string = request.GET['q']
query = SearchQuery(query_string, config='simple')
vector = SearchVector('reporter__first_name', 'reporter__last_name', 'agency__name',
'request_subject', 'request_notes', 'request_number', 'submission_notes',
'response_notes', 'response_url', 'lawsuit_notes', config='simple' )
res = Foia.objects.annotate(rank=SearchRank(vector, query), search=vector).filter(search=query_string).order_by('-rank')[:50]
return render(request, 'foias/search.html', {'result_foias': res, 'query': query_string })
# # this is not implemented!
# but if you wanted a page for showing details of a FOIA other than the edit page, this would be where to do it.
# you'd also have to change foias/urls.py
# def detail(request, foia_id):
# foia = get_object_or_404(Foia, pk=foia_id)
# return render(request, 'foias/detail.html', {'foia': foia})
示例7: search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchRank [as 别名]
def search(request):
query_string = ''
found_entries = []
#filter by tags
if ('tag' in request.GET) and request.GET['tag'].strip():
query_string = request.GET['tag']
found_entries = NewsArticle.objects.filter(tags__name__in=[query_string]).order_by('-date').distinct()
#search by keyword
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
vector = SearchVector('title', weight='A') + SearchVector('subheading', weight='B') + SearchVector('author', weight='B') + SearchVector('body', weight='C') + SearchVector('tags__name', weight='C')
query = get_query(query_string)
found_entries = NewsArticle.objects.annotate(
rank=SearchRank(vector, query),
search=vector,
).filter(search=query).order_by('rank', '-date')
search_results_json = []
search_results_shown = set()
for result in found_entries:
if result.slug in search_results_shown:
continue
search_results_shown.add(result.slug)
search_results_json.append({
'id': result.id,
'title': result.title,
'subheading': result.subheading,
'body_blurb': result.first_paragraph,
'article_image': result.article_image,
'article_image_alt': result.featured_image_alt_text,
'date': result.date,
'author': result.author,
'pin_to_top': result.pin_to_top,
'tags': list(result.tags.names()),
'slug': result.slug,
'seo_title': result.seo_title,
'search_description': result.search_description,
})
return JsonResponse(search_results_json, safe=False)
return JsonResponse([], safe=False)