Develop Real Time Search Apps with Enferno and Elastic Search as a Backend level09 Follow Aug 24, 2015 · 2 min read

Overview

In this article I will explain how to use Enferno Framework with Elastic Search as a backend for storing data. we will be using elasticsearch_dsl as an ORM to map and manage our data.

Prerequisite

Make sure you have Elastic Search installed, you can refer to the documentation at elastic website.

Setting up the project

Start by cloning the enferno_elasticsearch repository, it is a fork of the original enferno framework which replaces MongoEngine with Elasticsearch DSL

git clone https://github.com/level09/enferno_elasticsearch.git && cd enferno_elasticsearch

2. Create and activate the virtual environment, then install the requirements

virtualenv env && source env/bin/activate && pip install -r requirements.txt



Creating our data class

Just for demonstration purposes, let’s create a simple “Article” class, it will have the following fields: title, body, tags, created_at

The sample class already exists inside the cloned repository, all you have to do is to comment it out in the public/models.py file :

class Article(DocType):

title = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})

body = String(analyzer='snowball')

tags = String(index='not_analyzed')

created_at = Date()

lines = Integer()



class Meta:

index = 'articles'



def save(self, ** kwargs):

self.lines = len(self.body.split())

return super(Article, self).save(** kwargs)



def is_published(self):

return datetime.now() < self.published_from

Create and save some data

Let’s test our class, launch Enferno shell :

./manage.py shell

Import our Article class

from public.models import Article

from datetime import datetime

Create and save a few articles

Article(title='this is a test article',body='some data goes here', tags=['elastic', 'enferno'],created_at= datetime.now()).save() Article(title='Another Article',body='Some description', tags=['dummy','elastic', 'second'],created_at= datetime.now()).save()

Great! we are now storing our data inside elastic search index

Retrieve and display the data

Let’s retrieve our articles and display them on a HTML page.

Inside the public/views.py import the article class

from public.models import Article

Pass the articles list to the main template, update the index route as follows:

@bp_public.route('/')

def index():

return render_template('index.html',articles = Article.search().execute().hits)

Update the main template inside the templates/index.html to display the article data:

{% extends 'layout.html' %}



{% block content %}





<div class="container">

<div class="row">

<div class="one-half column" style="margin-top: 25%">

<h4>Articles</h4>

{% for article in articles %}

<h5>{{ article.title }}</h5>

<p>{{ article.body }}</p>

<ul>

{% for tag in article.tags %}

<li>{{ tag }}</li>

{% endfor %}

</ul>

<hr>

{% endfor %}

</div>

</div>

</div>







{% endblock %}

Launch the python server

./manage.py server

You should now see your article data, pulled form Elastic search and displayed on the page: