Add index to search function

This commit is contained in:
Skia 2016-12-19 21:00:09 +01:00
parent 5b95299bde
commit e66b274f0e
3 changed files with 4 additions and 23 deletions

View File

@ -613,7 +613,6 @@ class SithFile(models.Model):
self.copy_rights() self.copy_rights()
def apply_rights_recursively(self, only_folders=False): def apply_rights_recursively(self, only_folders=False):
print(self)
children = self.children.all() children = self.children.all()
if only_folders: if only_folders:
children = children.filter(is_folder=True) children = children.filter(is_folder=True)

View File

@ -13,7 +13,3 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable):
"""Used when the entire index for model is updated.""" """Used when the entire index for model is updated."""
return self.get_model().objects.all() return self.get_model().objects.all()
def prepare(self, obj):
ret = super(UserIndex, self).prepare(obj)
print(ret)
return ret

View File

@ -10,6 +10,8 @@ import os
import json import json
from itertools import chain from itertools import chain
from haystack.query import SearchQuerySet
from core.models import User, Notification from core.models import User, Notification
from club.models import Club from club.models import Club
@ -34,24 +36,8 @@ def notification(request, notif_id):
return redirect("/") return redirect("/")
def search_user(query, as_json=False): def search_user(query, as_json=False):
users = [] res = SearchQuerySet().models(User).filter(text=query).filter_or(text__contains=query)[:20]
if query: return [r.object for r in res]
exact_nick = User.objects.filter(nick_name__iexact=query).all()
nicks = User.objects.filter(nick_name__icontains=query).exclude(id__in=exact_nick).all()
users = User.objects.filter(Q(first_name__icontains=query) |
Q(last_name__icontains=query)).exclude(id__in=exact_nick).exclude(id__in=nicks).all()
nicks = nicks[:5]
users = users[:50]
if as_json: # Re-loads json to avoid double encoding by JsonResponse, but still benefit from serializers
exact_nick = json.loads(serializers.serialize('json', exact_nick, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
nicks = json.loads(serializers.serialize('json', nicks, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
users = json.loads(serializers.serialize('json', users, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
else:
exact_nick = list(exact_nick)
nicks = list(nicks)
users = list(users)
users = exact_nick + nicks + users
return users
def search_club(query, as_json=False): def search_club(query, as_json=False):
clubs = [] clubs = []