[Search] get search results from Elasticsearch
[mygpo.git] / mygpo / search / index.py
blobeb9719ce16049613d47de24f76613b96d394b505
2 """ Contains code for indexing other objects """
4 from pyes import ES, QueryStringQuery, FunctionScoreQuery
5 from pyes.exceptions import IndexAlreadyExistsException
7 from django.conf import settings
9 from mygpo.search.json import podcast_to_json
10 from mygpo.search.models import PodcastResult
12 import logging
13 logger = logging.getLogger(__name__)
16 def get_connection():
17 """ Create a connection from Django settings """
18 conn = ES(settings.ELASTICSEARCH_SERVER,
19 timeout=settings.ELASTICSEARCH_TIMEOUT)
20 return conn
23 def index_podcast(sender, **kwargs):
24 """ Indexes a podcast """
26 conn = get_connection()
27 podcast = kwargs['instance']
29 document = podcast_to_json(podcast)
30 conn.index(document, settings.ELASTICSEARCH_INDEX,
31 'podcast', podcast.id.hex)
34 def create_index():
35 """ Creates the Elasticsearch index """
36 conn = get_connection()
38 logger.info('Creating index %s' % settings.ELASTICSEARCH_INDEX)
39 try:
40 conn.indices.create_index(settings.ELASTICSEARCH_INDEX)
42 except IndexAlreadyExistsException as ex:
43 logger.info(str(ex))
46 def search_podcasts(query):
47 """ Search for podcasts according to 'query' """
48 conn = ES(settings.ELASTICSEARCH_SERVER)
50 # we have some "optimal" number of subscribers (eg the max)
51 # the farther we get from there, the lower the score
52 decay = FunctionScoreQuery.DecayFunction(
53 decay_function='gauss',
54 field='subscribers',
55 origin=2000,
56 scale=1000,
57 decay=.3,
59 # workaround for https://github.com/aparo/pyes/pull/418
60 decay._internal_name = 'gauss'
62 q = FunctionScoreQuery(
63 query=QueryStringQuery(query),
64 functions=[decay],
65 boost_mode=FunctionScoreQuery.BoostModes.MULTIPLY,
67 results = conn.search(query=q, indices=settings.ELASTICSEARCH_INDEX,
68 doc_types='podcast',
69 model=lambda conn, doc: PodcastResult.from_doc(doc))
70 return results