Skip to content

[Elasticsearch] Integrate Elasticsearch for Fast Search Access to Startups and Projects #85

Description

@mehalyna

Task: Integrate Elasticsearch for Fast Search Access to Startups and Projects

Objective

To enhance the search performance and capabilities in the project, we aim to integrate Elasticsearch. This will enable quick, complex search queries on Startup and Project models, supporting features like keyword searches, filtering, and sorting.


Sub-Tasks

  1. Setup and Installation

    • Install Elasticsearch:
      • Run Elasticsearch locally or use a Docker container for development. For production, you may choose a managed Elasticsearch service.
    • Install Required Libraries:
      • Install django-elasticsearch-dsl and django-elasticsearch-dsl-drf libraries in your Django project.
      pip install django-elasticsearch-dsl django-elasticsearch-dsl-drf
  2. Define Elasticsearch Settings

    • Update Django Settings:
      • Configure Elasticsearch settings in settings.py.
      ELASTICSEARCH_DSL = {
          'default': {
              'hosts': 'localhost:9200'
          },
      }
  3. Create Elasticsearch Documents

    • Define Documents for Models:

      • Create Elasticsearch document definitions for Startup and Project models. These documents will map model fields to Elasticsearch fields.
      • Create a documents.py file in each relevant app (e.g., startups and projects).
      # startups/documents.py
      from django_elasticsearch_dsl import Document, Index
      from .models import Startup
      
      startup_index = Index('startups')
      
      @startup_index.doc_type
      class StartupDocument(Document):
          class Django:
              model = Startup
              fields = [
                  'company_name',
                  'description',
                  'funding_stage',
                  'location',
                  'industries',
              ]
    • Repeat for Projects:

      # projects/documents.py
      from django_elasticsearch_dsl import Document, Index
      from .models import Project
      
      project_index = Index('projects')
      
      @project_index.doc_type
      class ProjectDocument(Document):
          class Django:
              model = Project
              fields = [
                  'title',
                  'description',
                  'status',
                  'required_amount',
                  'startup',
              ]
  4. Synchronize Data with Elasticsearch

    • Index Existing Data:

      • Run a management command to index existing data in Elasticsearch.
      python manage.py search_index --rebuild
    • Set Up Django Signals for Syncing:

      • Use Django signals to keep Elasticsearch indexes updated when data in the database changes.
      • Add signals in signals.py in each app to listen for post_save and post_delete events on Startup and Project.
  5. Create DRF Views for Search Functionality

    • Build Search Views:

      • Implement views in views.py that leverage Elasticsearch to perform searches on Startup and Project.
      • Use django-elasticsearch-dsl-drf to create DRF views for search endpoints.
      # startups/views.py
      from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet
      from .documents import StartupDocument
      from .serializers import StartupDocumentSerializer
      
      class StartupSearchViewSet(DocumentViewSet):
          document = StartupDocument
          serializer_class = StartupDocumentSerializer
          filter_backends = [
              FilteringFilterBackend,
              OrderingFilterBackend,
          ]
          search_fields = ('company_name', 'description')
          filter_fields = {
              'funding_stage': 'exact',
              'location': 'exact',
          }
    • Repeat for Projects.

  6. Create Serializers for Elasticsearch Documents

    • Define Elasticsearch Document Serializers:
      • Create serializers for StartupDocument and ProjectDocument.
      # startups/serializers.py
      from rest_framework import serializers
      from .documents import StartupDocument
      
      class StartupDocumentSerializer(serializers.ModelSerializer):
          class Meta:
              model = StartupDocument
              fields = '__all__'
  7. Update URLs

    • Add URLs for Search Endpoints:
      • Create URL routes for StartupSearchViewSet and ProjectSearchViewSet.
      # startups/urls.py
      from django.urls import path
      from .views import StartupSearchViewSet
      
      urlpatterns = [
          path('search/', StartupSearchViewSet.as_view({'get': 'list'}), name='startup-search'),
      ]
  8. Testing

    • Write Unit Tests:
      • Test the search functionality with sample data to ensure the indexes are working as expected.
    • Perform Manual Testing:
      • Run searches with various filters and keywords to confirm Elasticsearch is providing fast and accurate results.
  9. Documentation

    • Document API Endpoints:
      • Add endpoint descriptions, parameters, and example queries to the API documentation.
  10. Deployment and Optimization

    • Configure Elasticsearch for Production:
      • Set up production configurations, including security settings, optimized indices, and backups.
    • Monitor and Tune:
      • Use monitoring tools to check Elasticsearch performance and make adjustments as necessary.

Deliverables

  • Working search functionality on the /startups/search/ and /projects/search/ endpoints.
  • Documentation for API search endpoints.
  • Tests covering indexing, querying, and filtering with Elasticsearch.

Notes

https://testdriven.io/blog/django-drf-elasticsearch/

https://codewithmuh.medium.com/how-to-integrate-elasticsearch-with-drf-a24ede1739d2

https://django-elasticsearch-dsl-drf.readthedocs.io/en/latest/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions