本文整理汇总了Python中django.contrib.postgres.search.SearchQuery方法的典型用法代码示例。如果您正苦于以下问题:Python search.SearchQuery方法的具体用法?Python search.SearchQuery怎么用?Python search.SearchQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.search
的用法示例。
在下文中一共展示了search.SearchQuery方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_contracts
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def search_contracts(query, service_id, service_group_id):
filter_kwargs = {}
if query:
filter_kwargs["search_vector"] = SearchQuery(query)
if service_id:
filter_kwargs["service_id"] = service_id
if service_group_id:
filter_kwargs["service__group_id"] = service_group_id
if not filter_kwargs:
return []
return (
Contract.objects.select_related("document", "entity", "service")
.prefetch_related("contractors")
.defer("document__pages")
.filter(**filter_kwargs)
.order_by("-date_of_grant")
)
示例2: filter_text
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [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")
)
示例3: filter_queryset
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def filter_queryset(self, request, queryset, view):
search_term = self.get_search_term(request)
if not search_term:
return queryset
return queryset.filter(search_vector=SearchQuery(search_term))
示例4: search_taxonomy_node
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [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
#############################
示例5: search_series
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def search_series(self, *terms, user=None, queryset=None):
self._last_keywords = []
self._projects = set()
q = reduce(
lambda x, y: x & y, map(lambda t: self._process_term(t, user), terms), Q()
)
if queryset is None:
queryset = Message.objects.series_heads()
if self._last_keywords:
if connection.vendor == "postgresql":
queryset = queryset.annotate(
subjsearch=NonNullSearchVector("subject", config="english")
)
searchq = reduce(
lambda x, y: x & y,
map(
lambda x: SearchQuery(x, config="english"), self._last_keywords
),
)
q = q & Q(subjsearch=searchq)
else:
q = reduce(
lambda x, y: x & Q(subject__icontains=y), self._last_keywords, q
)
return queryset.filter(q)
示例6: filter_queryset
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [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
示例7: get_query
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def get_query(query_string):
"""
Returns a query, that is a combination of Q objects. That combination
aims to search keywords within a model by testing the given search fields.
"""
query_items = normalize_query(query_string)
query = SearchQuery(query_items.pop())
for term in query_items:
query &= SearchQuery(term)
return query
示例8: search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def search(self, search_term):
query = SearchQuery(search_term, config=settings.SEARCH_CONFIG)
qs = self.annotate(rank=SearchRankCD(models.F("search_tsv"), query))
return qs.filter(search_tsv=query).order_by("-rank", "-id")
示例9: ranked_search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [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))
示例10: test_headline
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def test_headline(self):
query = SearchQuery('hovercraft')
result = list(
self.objects
.annotate(match=Headline(F('title'), query))
.values_list('match', flat=True)
)
self.assertEqual(result, [
'My <b>hovercraft</b> is full of eels.',
'Spam! Spam! Spam! Spam! Spam! Spam!',
])
示例11: search_fts
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [as 别名]
def search_fts(raw_query, model):
"""Search using Postgres full text search
Usage:
q_companies = search_fts('Juan', model=Person)
q_companies = list(q_companies) # Force
"""
words = raw_query.split()
query = SearchQuery(words[0])
for word in words[1:]:
# AND
query &= SearchQuery(word)
return model.objects.filter(document=query)
示例12: search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchQuery [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})