diff --git a/project/apps/core/management/commands/push-to-index.py b/project/apps/core/management/commands/push-to-index.py index 1d6afcc..a398815 100644 --- a/project/apps/core/management/commands/push-to-index.py +++ b/project/apps/core/management/commands/push-to-index.py @@ -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 + ) diff --git a/project/apps/core/models.py b/project/apps/core/models.py index 79372b3..50d32d4 100644 --- a/project/apps/core/models.py +++ b/project/apps/core/models.py @@ -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): @@ -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 + } + } + } diff --git a/project/conf/base.py b/project/conf/base.py index 7561d16..d3c5333 100644 --- a/project/conf/base.py +++ b/project/conf/base.py @@ -214,8 +214,7 @@ } } -# from elasticsearch import Elasticsearch, RequestsHttpConnection - +# ES CONFIG. ES_CLIENT = Elasticsearch( ['http://127.0.0.1:9200/'], connection_class=RequestsHttpConnection