This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
search.py
53 lines (46 loc) · 1.78 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from operator import attrgetter
from django.conf import settings
import elasticutils
import pyes.exceptions as pyes
from .models import Addon
from compat.models import AppCompat
def extract(addon):
"""Extract indexable attributes from an add-on."""
attrs = ('id', 'name', 'created', 'last_updated', 'weekly_downloads',
'bayesian_rating', 'average_daily_users', 'status', 'type',
'is_disabled')
d = dict(zip(attrs, attrgetter(*attrs)(addon)))
# Coerce the Translation into a string.
d['name'] = unicode(d['name']).lower()
d['app'] = [a.id for a in addon.compatible_apps]
# This is an extra query, not good for perf.
d['category'] = getattr(addon, 'category_ids', [])
return d
def setup_mapping():
"""Set up the addons index mapping."""
# Mapping describes how elasticsearch handles a document during indexing.
# Most fields are detected and mapped automatically.
m = {
# Turn off analysis on name so we can sort by it.
'name': {
'type': 'multi_field',
'fields': {
'name': {'type': 'string', 'index': 'not_analyzed'},
'fulltext': {'type': 'string', 'index': 'analyzed'},
},
},
}
es = elasticutils.get_es()
try:
es.create_index_if_missing(settings.ES_INDEX)
except pyes.ElasticSearchException:
pass
# Adjust the mapping for all models at once because fields are shared
# across all doc types in an index. If we forget to adjust one of them
# we'll get burned later on.
for model in Addon, AppCompat, Collection:
try:
es.put_mapping(model._meta.app_label, {'properties': m},
settings.ES_INDEX)
except pyes.ElasticSearchException:
pass