2 """ Contains code for indexing other objects """
4 from pyes
import ES
, QueryStringQuery
, FunctionScoreQuery
5 from pyes
.exceptions
import IndexAlreadyExistsException
, NoServerAvailable
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']
28 logger
.info('Indexing podcast %s', podcast
)
30 document
= podcast_to_json(podcast
)
33 conn
.index(document
, settings
.ELASTICSEARCH_INDEX
,
34 'podcast', podcast
.id.hex)
35 except NoServerAvailable
:
36 logger
.exception('Indexing podcast failed')
40 """ Creates the Elasticsearch index """
41 conn
= get_connection()
43 logger
.info('Creating index %s' % settings
.ELASTICSEARCH_INDEX
)
45 conn
.indices
.create_index(settings
.ELASTICSEARCH_INDEX
)
47 except IndexAlreadyExistsException
as ex
:
51 def search_podcasts(query
):
52 """ Search for podcasts according to 'query' """
53 conn
= get_connection()
57 "boost_mode": 'replace',
59 'simple_query_string': {'query': query
}
62 'script': "_score * (doc.subscribers.value / 4000)"
66 results
= conn
.search(query
=q
, indices
=settings
.ELASTICSEARCH_INDEX
,
68 model
=lambda conn
, doc
: PodcastResult
.from_doc(doc
))