Skip to content

Commit

Permalink
bug 585207, blended persona search.
Browse files Browse the repository at this point in the history
  • Loading branch information
davedash committed Aug 9, 2010
1 parent d4e0e3f commit 933d6ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
31 changes: 31 additions & 0 deletions apps/search/client.py
Expand Up @@ -548,6 +548,37 @@ def _tags_meta(self, results, **kwargs):
return manual_order(Tag.objects.all(), tag_ids)


class AddonsPersonasClient(Client):
"""Search client that queries both personas and addons."""

def query(self, term, limit=10, offset=0, **kwargs):
sc = self.sphinx
sc.SetSelect('addon_id')
sc.SetLimits(min(offset, SPHINX_HARD_LIMIT - 1), limit)
term = sanitize_query(term)
self.log_query(term)

try:
result = sc.Query(term, 'personas, addons')
except socket.timeout:
log.error("Query has timed out.")
raise SearchError("Query has timed out.")
except Exception, e:
log.error("Sphinx threw an unknown exception: %s" % e)
raise SearchError("Sphinx threw an unknown exception.")

if sc.GetLastError():
raise SearchError(sc.GetLastError())

self.total_found = result['total_found'] if result else 0

if result and result['total']:
return self.get_result_set(term, result, offset, limit)
else:
return []



class PersonasClient(Client):
"""A search client that queries sphinx for Personas."""

Expand Down
18 changes: 9 additions & 9 deletions apps/search/views.py
Expand Up @@ -9,13 +9,15 @@
from tower import ugettext as _

import amo
from amo.decorators import json_view
from amo.helpers import urlparams
from amo import urlresolvers
from addons.models import Category
from versions.compare import dict_from_int, version_int
from search import forms
from search.client import (Client as SearchClient, SearchError,
CollectionsClient, PersonasClient)
CollectionsClient, AddonsPersonasClient,
PersonasClient)
from search.forms import SearchForm, SecondarySearchForm
from translations.query import order_by_translation

Expand Down Expand Up @@ -217,7 +219,7 @@ def _collections(request):

return jingo.render(request, 'search/collections.html', c)


@json_view
def ajax_search(request):
""" Returns a json feed of ten results for auto-complete used in
collections.
Expand All @@ -228,16 +230,14 @@ def ajax_search(request):
"""

q = request.GET.get('q', '')
client = SearchClient()
client = AddonsPersonasClient()
try:
results = client.query(q, limit=10)
items = [dict(id=result.id, label=unicode(result.name),
icon=result.icon_url, value=unicode(result.name).lower())
for result in results]
return [dict(id=result.id, label=unicode(result.name),
icon=result.icon_url, value=unicode(result.name).lower())
for result in results]
except SearchError:
items = []

return HttpResponse(json.dumps(items), mimetype='application/json')
return []


def search(request, tag_name=None):
Expand Down

0 comments on commit 933d6ca

Please sign in to comment.