djangosolrdjango-haystack

How to configure solr with django-haystack


I am running solr 9.3 from docker and trying to integrate with django-haystack. I am unable to configure solrconfig.xml and schema.xml and not sure where to add these files.

I tried generating schema.xml using python manage.py build_solr_schema but again I don't know how/where to put these files. Thanks in advance.


Solution

  • To integrate Solr with Django using Haystack, you need to ensure that the Solr instance you're running has the correct configuration in place. Since you're running Solr from Docker, the process can be a bit different compared to a standalone installation. Here's how you can set it up:

    1. Generate the Schema:

      You've already generated the schema.xml using:

      python manage.py build_solr_schema
      
    2. Customize Solr Docker Image:

      You will need to create a custom Docker image that uses the base Solr image and then adds your custom configurations.

      Here's an example Dockerfile:

      FROM solr:9.3
      
      # Copy custom schema.xml and solrconfig.xml to the appropriate core directory
      COPY schema.xml /opt/solr/server/solr/my_core/conf/
      COPY solrconfig.xml /opt/solr/server/solr/my_core/conf/
      

      Replace my_core with the name of your core or the core you plan to create.

    3. Build Your Custom Docker Image:

      In the same directory as your Dockerfile, run:

      docker build -t custom_solr:9.3 .
      

      This will create an image named custom_solr with the tag 9.3.

    4. Run Your Custom Docker Image:

      You can now run your custom Solr image. If you haven't created the core yet, create it first:

      docker run --name solr_instance -d -p 8983:8983 custom_solr:9.3 solr-precreate my_core
      

      Again, replace my_core with the name of your core. If the core is already created, you can just run the container without the solr-precreate command.

    5. Connect Django Haystack to Solr:

      In your Django settings.py, make sure you point Haystack to the correct Solr URL:

      HAYSTACK_CONNECTIONS = {
          'default': {
              'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
              'URL': 'http://127.0.0.1:8983/solr/my_core',
          },
      }
      

      Remember to replace my_core with the name of your Solr core.

    6. Final Checks:

      With everything set up, restart your Django server and check if Haystack can communicate with Solr. You might want to run python manage.py update_index to ensure data is indexed properly.

    Remember, when using Docker, the configurations inside the container are ephemeral. If you remove the container, you lose those configurations. By creating a custom Docker image as described above, you ensure that you can always start a Solr container with your specific configurations.