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
13 logger
= logging
.getLogger(__name__
)
17 """ Create a connection from Django settings """
18 conn
= ES(settings
.ELASTICSEARCH_SERVER
,
19 timeout
=settings
.ELASTICSEARCH_TIMEOUT
)
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)
35 """ Creates the Elasticsearch index """
36 conn
= get_connection()
38 logger
.info('Creating index %s' % settings
.ELASTICSEARCH_INDEX
)
40 conn
.indices
.create_index(settings
.ELASTICSEARCH_INDEX
)
42 except IndexAlreadyExistsException
as 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',
59 # workaround for https://github.com/aparo/pyes/pull/418
60 decay
._internal
_name
= 'gauss'
62 q
= FunctionScoreQuery(
63 query
=QueryStringQuery(query
),
65 boost_mode
=FunctionScoreQuery
.BoostModes
.MULTIPLY
,
67 results
= conn
.search(query
=q
, indices
=settings
.ELASTICSEARCH_INDEX
,
69 model
=lambda conn
, doc
: PodcastResult
.from_doc(doc
))