Integration of Elastic Search and Django(A Python Framework) – Part One
In this tutorial I am going to show you the Integration of Elastic Search and Django. I am using Django 1.10, ElasticSearch 5.2.0, and Ubuntu 16.04 OS for the application. Lets Have a look on the model we are going to build. A simple application that have five simple models : – Product Model, Category Model, SubCategory Model, UserProfile, and the User Model. This is the part One of the tutorial, in this part I am going to build an application at Django Part. To install Django on your system refer here. Open terminal and type command
1 |
django-admin.py startproject elasticdjango |
make virtual environment
1 |
virtualenv -p python3 venv |
activate the virtual environment
1 |
source venv/bin/activate |
install django 1.10 in the virtual environment using pip
1 |
pip install django==1.10 |
make a app named core that will contain our models
1 |
django-admin startapp core |
Here is the directory structure for our application go to the elasticdjango/settings.py file and add the core application to the Installed apps:- add following code to the core/models.py file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
from django.db import models from django.contrib.auth.models import User import django.db.models.options as options options.DEFAULT_NAMES=options.DEFAULT_NAMES+('es_index_name','es_type_name','es_mapping') # Create your models here. class Product(models.Model): """Product Model""" CHOICES=(('downloadable_product','DOWNLOADABLE PRODUCT'),('shipped_product','SHIPPED PRODUCT')) name=models.CharField(max_length=150) price=models.PositiveIntegerField(default=0) category=models.ForeignKey('Category') subcategory=models.ForeignKey('Subcategory') seller=models.ForeignKey('UserProfile') product_type=models.CharField(choices=CHOICES,max_length=50,default='downloadable_field') def __unicode__(self): return self.name def es_repr(self): data={ "name":self.name, "price":self.price, "category":{ "name": self.category.name, }, "subcategory":{ "name": self.subcategory.name, }, "seller":{ "email": self.seller.user.email, }, "product_type":self.product_type } return data class Meta: es_index_name='ecommerce' es_type_name='products' es_mapping={ 'properties':{ 'name':{'type':'text'}, 'price':{'type':"long"}, 'category':{ 'properties':{ 'name':{'type':'keyword'}, } }, 'subcategory':{ 'properties':{ 'name':{'type':'keyword'}, } }, 'seller':{ 'properties':{ 'email':{'type':'text'} } }, 'product_type':{'type':"keyword"}, } } class Category(models.Model): """Category Model""" name=models.CharField(max_length=150) def __unicode__(self): return self.name class Subcategory(models.Model): """Category Model""" name=models.CharField(max_length=150) category=models.ForeignKey(Category) def __unicode__(self): return self.name class UserProfile(models.Model): """User Profile model""" CHOICES=(('vendor','VENDOR'),('consumer','CONSUMER')) user_type=models.CharField(choices=CHOICES,default='consumer',max_length=150) user=models.ForeignKey(User) def __unicode__(self): return self.user_type |
Don’t worry if you are not able to understand this time the whole code. This in simple words contain the models definitions […]