Ev.
Ev.

Reputation: 1567

Advise on search engine for Django application

I have an app with a search page. Users will be able to make searches hitting several tables. However I am afraid this will come with a huge cost to the server.

ads_list = advertisements.filter(Q(title__icontains=words) | Q(street_name__icontains=words) |
                                                 Q(city__in=city.filter(Q(name__icontains=words) | Q(related_names__icontains=words) | Q(post_code=words) | Q(region__icontains=words))) |
                                                 Q(state_category__in=state_category.filter(Q(category_name__icontains=words) | Q(search_tags__icontains=words))) |
                                                 Q(advertisement_type__in=advertisement_type.filter(Q(type_name__icontains=words) | Q(search_tags__icontains=words))))

In the search page I am using digg_paginator to spread the results.

My question is: are these results cached by Django or every time the user hits the "next page" button a new query is made? I was wondering if I should implement Django cache API and try to deal with it myself, but I don't know if Django is already making a better job than I would. I will also try to provide more and better results results per page so the user don't keep changing pages so often.

Upvotes: 1

Views: 45

Answers (1)

Dirty Penguin
Dirty Penguin

Reputation: 4412

are these results cached by Django or every time the user hits the "next page" button a new query is made?

That depends on how the button press is handled. If your button press calls a Django view and you're relying on Django's paginator, Django will query your database each time the button is pressed.

If you want to reduce the queries to the database, I can think of two methods:

  1. Handle pagination using javascript on the client side. The button press would call the Django view, which would return the full set of results to the client. The client would then use javascript to split the results across multiple pages and provide access to said pages via pagination.

  2. Use Django's caching framework. You could set up a Redis server and configure Django to use that for caching. Then update your view to use the cache_page decorator. Read more about that here: https://docs.djangoproject.com/en/1.9/topics/cache/

Upvotes: 1

Related Questions