Arjun V
Arjun V

Reputation: 197

How to implement a fast search(2-3 sec) in django containing large dataset?

I'm working on a project in Django, where I have to find a solution to create a fast search which takes roughly 2 - 3 seconds to load the search result instantaneously. I'm using Django REST API for handing the queries. Currently, I'm getting the result, but it tends to take a lot of time going through the entire database containing a lot of data. I need a solution that I can implement, so that I can reduce the search time to maximum of 3 seconds. PS. I'm using PostgreSQL as the database.

My motive is to search the city or country and I need to get the results.

models.py

 class Search(models.Model):
 city = models.CharField('Search Destination', max_length=256, null = true)
 country = models.CharField('Search country', max_length=256, null = true)
 latitude = models.CharField('Search latitude', max_length=256, null = true)
 longitude = models.CharField('Search longitude', max_length=256, null = true)
 createdAt = models.DateTimeField('Created At', auto_now_add=True, null = true)
 slug = models.SlugField(max_length=256, null=True, blank=True)

 def __str__(self):
     return self.city

serializers.py

class SearchSerializer(serializers.ModelSerializer):
    class Meta:
        model = Search
        fields = '__all__'

views.py

class ApiLandingSearchView(generics.ListAPIView):
    search_fields = ['city', country ]
    serializer_class = SearchSerializer
    filter_backends = [filters.SearchFilter]
    queryset = Search.objects.all()

Upvotes: 1

Views: 981

Answers (1)

Denis Cornehl
Denis Cornehl

Reputation: 4182

You seem to want a full-text search over your searches via API.

The SearchFilter uses the django-admin search functionality in the background, so it's searching for text inside text-fields, which is slow on every database by default.

You will have to add a proper full-text-search to your application.

In your case I see two approaches you can pursue:

  • django-watson, which is based on the postgres full-text search capabilities
  • django-haystack, which lets you integrate with elasticsearch

Both you would have to integrate with django-rest-framework by building your own custom Filter-Backend.

Upvotes: 1

Related Questions