From 66ee03760370f6c5948feeb153ca5b9fb97b1ad0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Tue, 22 Aug 2017 21:27:13 +0200 Subject: [PATCH] Dynamically construct search vectors --- mygpo/search/tasks.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/mygpo/search/tasks.py b/mygpo/search/tasks.py index 10fb3fe7..93c986fe 100644 --- a/mygpo/search/tasks.py +++ b/mygpo/search/tasks.py @@ -1,3 +1,5 @@ +import functools +import operator from datetime import timedelta from celery.decorators import periodic_task @@ -7,6 +9,8 @@ from django.contrib.postgres.search import SearchVector from mygpo.podcasts.models import Podcast +from . import INDEX_FIELDS + from celery.utils.log import get_task_logger logger = get_task_logger(__name__) @@ -35,12 +39,23 @@ def update_search_index(run_every=UPDATE_INTERVAL): count = to_update.count() logger.info('Updating search index for {} podcasts'.format(count)) + vectors = _get_search_vectors() + for podcast in to_update: Podcast.objects.filter(pk=podcast.pk)\ - .update(search_vector= - SearchVector('title', weight='A') + - SearchVector('description', weight='B'), - search_index_uptodate=True, - ) + .update(search_vector=vectors, + search_index_uptodate=True, + ) logger.info('Finished indexing podcasts') + + +def _get_search_vectors(): + """ Return the combined search vector to use for indexing podcasts """ + vectors = [] + for (field, weight) in INDEX_FIELDS.items(): + # index the podcast based on the stored language + vectors.append(SearchVector(field, weight=weight)) + + # vectors can be combined with + + return functools.reduce(operator.__add__, vectors) -- 2.11.4.GIT