Implement search with Django-haystack and Elasticsearch Part-I

Installation:

pip install django-haystack

Configuration:

INSTALLED_APPS=[               'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
#add haystack here
'haystack',
'books'
]

Settings:

HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack_books',
},
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
class Book(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(unique=True)
created_on = models.DateTimeField(auto_now_add=True)
description = models.TextField()
authors = models.ManyToManyField(settings.AUTH_USER_MODEL)

def __unicode__(self):
return self.title

SearchIndex:

from haystack import indexes
from books.models import Book
class BookIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True, template_name="search/book_text.txt")
title = indexes.CharField(model_attr='title')
authors = indexes.CharField()
def get_model(self):
return Book
def prepare_authors(self, obj):
return [ author.name for a in obj.authors.all()]
def index_queryset(self, using=None):
return self.get_model().objects.all()
{{ object.title }}
{% for a in authors%}
{{ a }}
{% endfor %}

Setup:

urlpatterns = patterns(
# other urls
url(r'^search/', include('haystack.urls')),
)

build index:

Python manage.py rebuild_index
<form action="/search" method="get">
<input name="q" type="text" />
<input type="submit" />
</form>
{% for result in page.object_list %}
{{ result.object.title }}
{{ result.object.description }}
{{ result.object.created_on }}
{% empty %}
No results found.
{% endfor %}

The article was originally published at MicroPyramid blog.

--

--

--

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Taking care of our customers at EmailOctopus

Installing Mosquitto broker on Debian

Morpheus.Network Partners with Loadsure to Bring Automated Cargo Insurance to the Global Market

Cluster Governance — clean up resources periodically

Windows’ winget app — pros and cons

Swift vs Kotlin — Which is better?

Ruby on C(ocaine) 💉 Intro to C extensions for newbies.

Ease of Building UI Elements in Unity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MicroPyramid

MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

More from Medium

Location-based application using Django, PostGIS and Leaflet

Django Model implementation (Part II)

[Django][Restframework] Filter data in Django Rest Framework

FastAPI Exceptions