本文整理汇总了Python中django.contrib.postgres.search.SearchVector方法的典型用法代码示例。如果您正苦于以下问题:Python search.SearchVector方法的具体用法?Python search.SearchVector怎么用?Python search.SearchVector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.search
的用法示例。
在下文中一共展示了search.SearchVector方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_queryset
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def get_queryset(self) -> QuerySet:
"""
Returns a queryset of `carrot.models.MessageLog` objects. If a `search_term` is provided in the request query
params, then the result is filtered based on this. If using postgres, this is done using SearchVectors for
improved performance
"""
search_term = self.request.query_params.get('search', None)
qs = self.queryset.all()
if search_term:
if settings.DATABASES.get('default', {}).get('ENGINE') == 'django.db.backends.postgresql_psycopg2':
qs = qs.annotate(search=SearchVector('task', 'content', 'task_args')).filter(search=search_term)
else:
qs = (
qs.filter(task__icontains=search_term) |
qs.filter(content__icontains=search_term) |
qs.filter(task_args__icontains=search_term)
).distinct()
return qs
示例2: as_vector
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def as_vector(self, texts, for_autocomplete=False):
"""
Converts an array of strings into a SearchVector that can be indexed.
"""
texts = [(text.strip(), weight) for text, weight in texts]
texts = [(text, weight) for text, weight in texts if text]
if not texts:
return EMPTY_VECTOR
search_config = self.autocomplete_config if for_autocomplete else self.config
return ADD([
SearchVector(Value(text, output_field=TextField()), weight=weight, config=search_config)
for text, weight in texts
])
示例3: update
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def update(self):
lines = self.entry.lines.all()
text_vector = pg_search.SearchVector(models.Value(strip_tags(self.entry.note), output_field=models.TextField()),
weight='C', config='english')
for tag in self.entry.tags.all():
text_vector += pg_search.SearchVector(models.Value(tag.name, output_field=models.TextField()), weight='A',
config='english')
for tag in self.entry.event.tags.all():
text_vector += pg_search.SearchVector(models.Value(tag.name, output_field=models.TextField()), weight='C',
config='english')
if lines.exists():
speaker_vectors = []
for line in lines:
text_vector += pg_search.SearchVector(
models.Value(strip_tags(line.text), output_field=models.TextField()),
weight='B', config='english')
speaker_vectors.append(pg_search.SearchVector(
models.Value(strip_tags(line.speaker), output_field=models.TextField()),
weight='A', config='english'))
text_vector += pg_search.SearchVector(
models.Value(strip_tags(line.speaker), output_field=models.TextField()),
weight='D', config='english')
speaker_vector = speaker_vectors[0]
for sv in speaker_vectors[1:]:
speaker_vector += sv
self.speaker_vector = speaker_vector
self.text_vector = text_vector
self.save()
示例4: search_taxonomy_node
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [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: __init__
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def __init__(self, *expressions, **extra):
super(SearchVector, self).__init__(*expressions, **extra)
self.config = self.extra.get("config", self.config)
self.weight = None
示例6: get_fields_vectors
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def get_fields_vectors(self, search_query):
return [
(SearchVector(
field_lookup,
config=search_query.config,
), search_field.boost)
for field_lookup, search_field in self.search_fields.items()
]
示例7: filter_queryset
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [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
示例8: generic_search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def generic_search(self, query):
try:
results = Books.objects.annotate(
search=SearchVector("title", "file_name", "author"),
).filter(search=query)
except Exception as e:
raise
return results
示例9: combine_index
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def combine_index(field_a, field_b):
vector = SearchVector(Value(field_a, output_field=models.TextField()), config=settings.SEARCH_CONFIG, weight="A") + \
SearchVector(Value(field_b, output_field=models.TextField()), config=settings.SEARCH_CONFIG, weight="B")
return vector
示例10: update_search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def update_search(self):
vectors = SearchVector(Value("", output_field=models.TextField()), config=settings.SEARCH_CONFIG)
for weight in ["a", "b", "c", "d"]:
if hasattr(self, "index_search_{}".format(weight)):
content = getattr(self, "index_search_{}".format(weight))()
vectors = vectors + SearchVector(Value(content, output_field=models.TextField()),
config=settings.SEARCH_CONFIG, weight=weight.upper())
self.search_tsv = vectors
示例11: psql_update_documents_batch
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def psql_update_documents_batch():
"""
Same as psql_update_documents() but using atomic batches.
This way, the table is not locked for a long time when the update is big
"""
affected_rows = 0
while Company.objects.filter(document__isnull=True).exists():
with connection.cursor() as cursor:
with transaction.atomic():
for row in Company.objects.filter(document__isnull=True)[:1000]:
row.document = SearchVector("name")
row.save()
affected_rows += cursor.rowcount
logger.info("Updated {} borme_company records"
.format(cursor.rowcount))
while Person.objects.filter(document__isnull=True).exists():
with connection.cursor() as cursor:
with transaction.atomic():
for row in Person.objects.filter(document__isnull=True)[:1000]:
row.document = SearchVector("name")
row.save()
affected_rows += cursor.rowcount
logger.info("Updated {} borme_company records"
.format(cursor.rowcount))
return affected_rows
示例12: search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [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})
示例13: filter
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [as 别名]
def filter(self, qs, value):
if value in EMPTY_VALUES:
return qs
qs = qs.annotate(
search=SearchVector(
*[self._build_search_expression(field) for field in self.fields]
)
)
return qs.filter(search=value)
示例14: search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import SearchVector [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)