Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Add author search to search API (bug 1032828)
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Oct 2, 2014
1 parent 11ed4b0 commit b0ac444
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/api/topics/search.rst
Expand Up @@ -49,6 +49,9 @@ Search
codes should be provided in ISO 639-1 format, using a comma-separated
list if supplying multiple languages.
:type languages: string
:param optional author: Filters by author. Requires a case-insensitive
exact match of the author field.
:type author: string
:param optional region: Filters apps by a supported region. A region
code should be provided in ISO 3166 format (e.g., `pl`). If not
provided, the region is automatically detected via requesting IP
Expand Down
4 changes: 4 additions & 0 deletions mkt/search/forms.py
Expand Up @@ -106,6 +106,7 @@ class ApiSearchForm(forms.Form):
label=_lazy('Works offline'))
languages = forms.CharField(required=False,
label=_lazy('Supported languages'))
author = forms.CharField(required=False, label=_lazy('Author name'))

sort = forms.MultipleChoiceField(required=False,
choices=LISTING_SORT_CHOICES)
Expand Down Expand Up @@ -170,6 +171,9 @@ def clean_device_and_dev(self):
elif device:
raise forms.ValidationError('Invalid device or device type.')

def clean_author(self):
return self.cleaned_data.get('author', '').lower()

def clean(self):
self.clean_device_and_dev()
return self.cleaned_data
7 changes: 7 additions & 0 deletions mkt/search/indexers.py
Expand Up @@ -182,6 +182,13 @@ def get_analysis(cls):
'icu_normalizer'],
}

# An analyzer that can do case-insensitive exact matching.
analyzers['exact_lowercase'] = {
'type': 'custom',
'tokenizer': 'keyword',
'filter': ['lowercase'],
}

for lang, stemmer in amo.STEMMER_MAP.items():
filters['%s_stem_filter' % lang] = {
'type': 'stemmer',
Expand Down
7 changes: 6 additions & 1 deletion mkt/search/tests/test_filters.py
Expand Up @@ -15,7 +15,6 @@
from mkt.search.views import _sort_search, DEFAULT_SORTING
from mkt.site.fixtures import fixture
from mkt.webapps.indexers import WebappIndexer
from mkt.webapps.models import Webapp


class TestSearchFilters(BaseOAuth):
Expand All @@ -41,6 +40,7 @@ def _filter(self, req, filters, **kwargs):
form_data = form.cleaned_data
sq = WebappIndexer.get_app_filter(self.req, {
'app_type': form_data['app_type'],
'author.raw': form_data['author'],
'category': form_data['cat'],
'device': form_data['device'],
'is_offline': form_data['offline'],
Expand Down Expand Up @@ -188,6 +188,11 @@ def test_languages(self):
ok_({'terms': {'supported_locales': ['ar', 'en-US']}}
in qs['query']['filtered']['filter']['bool']['must'])

def test_author(self):
qs = self._filter(self.req, {'author': 'Mozilla LABS'})
ok_({'term': {'author.raw': u'mozilla labs'}}
in qs['query']['filtered']['filter']['bool']['must'])

def test_region_exclusions(self):
self.req.REGION = regions.CO
qs = self._filter(self.req, {'q': 'search terms'})
Expand Down
16 changes: 15 additions & 1 deletion mkt/search/tests/test_views.py
Expand Up @@ -500,12 +500,26 @@ def test_name_localized_to_default_locale(self):

# Make a request in another language that we know will fail.
res = self.anon.get(self.url,
data={'q': 'something', 'lang': 'de'})
data={'q': 'something', 'lang': 'de'})
eq_(res.status_code, 200)
obj = res.json['objects'][0]
eq_(obj['slug'], self.webapp.app_slug)
eq_(obj['name'], u'Algo Algo Steamcube!')

def test_author(self):
res = self.anon.get(self.url,
data={'author': self.webapp.developer_name})
eq_(res.status_code, 200)
obj = res.json['objects'][0]
eq_(obj['slug'], self.webapp.app_slug)

def test_author_case(self):
res = self.anon.get(
self.url, data={'author': self.webapp.developer_name.upper()})
eq_(res.status_code, 200)
obj = res.json['objects'][0]
eq_(obj['slug'], self.webapp.app_slug)

def test_device(self):
AddonDeviceType.objects.create(
addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
Expand Down
3 changes: 2 additions & 1 deletion mkt/search/views.py
Expand Up @@ -143,11 +143,12 @@ def search_form_to_es_fields(form_data):
return {
'app_type': form_data['app_type'],
'category': form_data['cat'],
'author.raw': form_data['author'],
'device': form_data['device'],
'is_offline': form_data['offline'],
'manifest_url': form_data['manifest_url'],
'q': form_data['q'],
'premium_type': form_data['premium_types'],
'q': form_data['q'],
'supported_locales': form_data['languages'],
'tags': form_data['tag'],
}
Expand Down
17 changes: 14 additions & 3 deletions mkt/webapps/indexers.py
Expand Up @@ -76,7 +76,16 @@ def _locale_field_mapping(field, analyzer):
'id': {'type': 'long'},
'app_slug': {'type': 'string'},
'app_type': {'type': 'byte'},
'author': {'type': 'string', 'analyzer': 'default_icu'},
'author': {
'type': 'string',
'analyzer': 'default_icu',
'fields': {
# For exact matches. The simple analyzer allows
# for case-insensitive matching.
'raw': {'type': 'string',
'analyzer': 'exact_lowercase'},
},
},
'banner_regions': cls.string_not_indexed(),
'bayesian_rating': {'type': 'float', 'doc_values': True},
'category': cls.string_not_analyzed(),
Expand Down Expand Up @@ -454,6 +463,7 @@ def get_app_filter(cls, request, additional_data=None, sq=None,

data = {
'app_type': [],
'author.raw': None,
'category': None, # Slug.
'device': None, # ID.
'gaia': getattr(request, 'GAIA', False),
Expand All @@ -466,13 +476,14 @@ def get_app_filter(cls, request, additional_data=None, sq=None,
'region': getattr(get_region_from_request(request), 'id', None),
'status': None,
'supported_locales': [],
'tags': '',
'tablet': getattr(request, 'TABLET', False),
'tags': '',
}
data.update(additional_data)

# Fields that will be filtered with a term query.
term_fields = ('device', 'manifest_url', 'status', 'tags')
term_fields = ('author.raw', 'device', 'manifest_url', 'status',
'tags')
# Fields that will be filtered with a terms query.
terms_fields = ('category', 'premium_type', 'app_type',
'supported_locales')
Expand Down

0 comments on commit b0ac444

Please sign in to comment.