Skip to content

Commit

Permalink
Added commond for populating index wiht data. Had to adapt to ES6. Se…
Browse files Browse the repository at this point in the history
…ems to be working without error now. Movingon to final steps of part 3.
  • Loading branch information
taoteg committed Nov 29, 2017
1 parent 5593f3f commit 1caec01
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 78 deletions.
92 changes: 17 additions & 75 deletions project/apps/core/management/commands/push-to-index.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,30 @@
from django.core.management.base import BaseCommand
from core.models import University, Course, Student

# ADDED.
from model_mommy import mommy
import random
import names
from elasticsearch.client import IndicesClient
from django.conf import settings
from django.core.management.base import BaseCommand
from core.models import Student


class Command(BaseCommand):
help = "My shiny new management command."


def add_arguments(self, parser):
# parser.add_argument('sample', nargs='+')
# Take arg for number of students to generate.
parser.add_argument('count', nargs=1, type=int)


def handle(self, *args, **options):
# TEST COMMAND WORKS.
# print('Generating dummy data...')
# GENERATE THE DUMMY DATA.
self.clear()
self.make_universities()
self.make_courses()
self.make_students(options)
self.connect_courses()


def clear(self):
Student.objects.all().delete()
University.objects.all().delete()
Course.objects.all().delete()


def make_universities(self):
university_names = ('UT', 'TAMU', 'MIT', 'MGU', 'CalTech', 'KPI', 'DPI', 'PSTU')
self.universities = []
for name in university_names:
uni = mommy.make(University, name=name)
self.universities.append(uni)

print('Populating elastic search index...')
self.recreate_index()

def make_courses(self):
template_options = ['CS%s0%s', 'MATH%s0%s', 'CHEM%s0%s', 'PHYS%s0%s']
self.courses = []
for num in range(1, 4):
for course_num in range(1, 4):
for template in template_options:
name = template % (course_num, num)
course = mommy.make(Course, name=name)
self.courses.append(course)
def recreate_index(self):
indices_client = IndicesClient(client=settings.ES_CLIENT)

index_name = Student._meta.es_index_name

def make_students(self, options):
self.students = []
# for _ in xrange(options.get('count')[0]): # Python 2 version.
for _ in range(options.get('count')[0]): # Python 3 version.
stud = mommy.prepare(
Student,
university=random.choice(self.universities),
first_name=names.get_first_name(),
last_name=names.get_last_name(),
age=random.randint(17, 25)
)
self.students.append(stud)
Student.objects.bulk_create(self.students)
if indices_client.exists(index_name):
indices_client.delete(index=index_name)

indices_client.create(index=index_name)

def connect_courses(self):
ThroughModel = Student.courses.through
stud_courses = []
for student_id in Student.objects.values_list('pk', flat=True):
courses_already_linked = []
for _ in range(random.randint(1, 10)):
index = random.randint(0, len(self.courses) - 1)
if index not in courses_already_linked:
courses_already_linked.append(index)
else:
continue
stud_courses.append(
ThroughModel(
student_id=student_id,
course_id=self.courses[index].pk
)
)
ThroughModel.objects.bulk_create(stud_courses)
indices_client.put_mapping(
doc_type=Student._meta.es_type_name,
body=Student._meta.es_mapping,
index=index_name
)
57 changes: 56 additions & 1 deletion project/apps/core/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
# from django.contrib.contenttypes import generic
# ADDED.
import django.db.models.options as options


options.DEFAULT_NAMES = options.DEFAULT_NAMES + (
'es_index_name', 'es_type_name', 'es_mapping'
)


class University(models.Model):
Expand Down Expand Up @@ -42,3 +48,52 @@ class Student(models.Model):
# def __unicode__(self): # Python 2..
def __str__(self): # Python 3.
return (self.last_name + ', ' + self.first_name)

# ES Mapping for Indexing.
# ES6 tweaks:
# Replaces 'string' with 'text'.
# Replace 'index': 'not_analyzed' with 'index': False.
# Replace 'store': 'yes' with 'store': True.
class Meta:
es_index_name = 'django'
es_type_name = 'student'
es_mapping = {
'properties': {
'university': {
'type': 'object',
'properties': {
'name': {
'type': 'text',
'index': False,
}
}
},
'first_name': {
'type': 'text',
'index': False
},
'last_name': {
'type': 'text',
'index': False
},
'age': {
'type': 'short'
},
'year_in_school': {
'type': 'text'
},
'name_complete': {
'type': 'completion',
'analyzer': 'simple',
# 'payloads': True,
'preserve_separators': True,
'preserve_position_increments': True,
'max_input_length': 50
},
'course_names': {
'type': 'text',
'store': True,
'index': False
}
}
}
3 changes: 1 addition & 2 deletions project/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@
}
}

# from elasticsearch import Elasticsearch, RequestsHttpConnection

# ES CONFIG.
ES_CLIENT = Elasticsearch(
['http://127.0.0.1:9200/'],
connection_class=RequestsHttpConnection
Expand Down

0 comments on commit 1caec01

Please sign in to comment.