Answer the question
In order to leave comments, you need to log in
How to speed up Sphinx search on Django?
The problem is searching for 3 or more words, if 1-2 words, then everything works instantly, if you enter more than 3, then postgresql starts working and eats 100% of the processor
. I use this package https://github.com/jorgecarleitao/django- sphinxql
Made indexes.py according to the instructions:
from sphinxql import indexes, fields
from core import models
class ProductIndex(indexes.Index):
name_rus = fields.Text(model_attr='name_rus')
name_poland = fields.Text(model_attr='name_poland')
category_id = fields.Text(model_attr='category_id')
class Meta:
model = models.Product
def search(request):
if request.method == 'GET':
form = SearchForm(request.GET)
if form.is_valid():
name_rus = form.cleaned_data['search']
try:
product = Product.objects.get(Q(name_rus=name_rus)|Q(sku=name_rus))
return redirect('product_item', product.slug)
except:
auto = Category.objects.filter(slug='avtotovaryi-0').get_descendants(include_self=False)
lower = set('абвгдеёжзийклмнопрстуфхцчшщъыьэюя')
if lower.intersection(name_rus.lower()) != set():
products_search = SearchQuerySet(ProductIndex).search('@name_rus '+name_rus)[0:4000]
else:
products_search = SearchQuerySet(ProductIndex).search('@name_poland '+name_rus)[0:4000]
#products_search = ProductIndex.objects.search_filter(name_rus__icontains=name_rus, category_id__in=auto)[0:3000]
#products_search = Product.objects.filter(category_id__in=auto, name_rus__icontains=name_rus)[0:3000]
last_question = '?search=%s' % name_rus
#categories = Category.objects.filter(categories_products__in=products_search).distinct()
categories = Category.objects.filter(categories_products__in=products_search).distinct()
paginator = Paginator(products_search, 40)
page = request.GET.get('page')
products = paginator.get_page(page)
return render(request, 'core/search.html', {'products':products, 'categories':categories, 'page':page, 'last_question':last_question, 'products_search':products_search})
else: return HttpResponse('Ошибка поиска')
def typeahead(request):
q = request.GET.get('q', '')
prodlist = []
objects = Product.objects.filter(Q(name_rus__icontains=q)|Q(sku=q))[0:10]
for i in objects:
new = {'q':i.name_rus}
prodlist.append(new)
return HttpResponse(json.dumps(prodlist), content_type="aplication/json")
Answer the question
In order to leave comments, you need to log in
And what does postgres have to do with it if you have a search through the sphinx, you just need to look at the active requests of all postgres and the sphinx has nothing to do with it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question