HOME BLOG

How to add Search feature to your Dango app with Haystack and Solr

Posted on: August 4th, 2021 by Olu No Comments

Hi folks,

In this post I talk briefly about how you can add search functionality to your Django app. Why add search platforms like Solr, Elasticsearch,e tc. to your app? Because it makes it easier for users to find relevant information, especially when it may be resource-intensive to perform those searches with your database alone. There’s an interesting article on this topic: Solr and RDBMS: Designing your application for the best of both. https://lucidworks.com/post/solr-and-rdbms-the-basics-of-designing-your-application-for-the-best-of-both/. There are several search tools you can use including Solr, Sphinx, etc. let’s assume you want to use Solr.

There’s a modular library you can use called Haystack (https://django-haystack.readthedocs.io/en/master/toc.html). Haystack supports several search tools, including Solr, Elasticsearch, etc.

If you want to use Haystack with Solr, you’ll need to have Solr running first. You can install Solr on different platforms. Haystack config details are foundĀ  at https://django-haystack.readthedocs.io/en/master/tutorial.html#installation.

Details about installing Solr for use with Haystack can be found here https://django-haystack.readthedocs.io/en/master/installing_search_engines.html.

When you install and start Solr, you would usually want to create something called a core. You perform indexing in that core and perform your searches against the core. Multiple cores can be created on a single Solr instance. When referencing Solr in an application, you would usually use a url like http://127.0.0.1/solr/<yourcore>. Haystack provides management command for interacting building search index and updating search index. You can install Solr using a docker container. See https://hub.docker.com/r/geerlingguy/solr/.

That’s all for now. Till next time, happy software development.

Leave a Reply