Commit 75bb5b42 by Tim Babych Committed by Oleg Marshev

add elasticsearch searcher

parent 10480c3a
import json import json
from django.db import models from django.db import models
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from elasticutils.contrib.django import Indexable, MappingType
class Note(models.Model): class Note(models.Model):
...@@ -69,3 +73,46 @@ class Note(models.Model): ...@@ -69,3 +73,46 @@ class Note(models.Model):
'created': created, 'created': created,
'updated': updated, 'updated': updated,
} }
@receiver(post_save, sender=Note)
def update_in_index(sender, instance, **kw):
if settings.ES_DISABLED:
return
NoteMappingType.bulk_index([instance.as_dict()], id_field='id')
class NoteMappingType(MappingType, Indexable):
@classmethod
def get_model(cls):
return Note
@classmethod
def get_mapping(cls):
"""
Returns an Elasticsearch mapping for Note MappingType
"""
charfield = {'type': 'string', 'index': 'not_analyzed', 'store': True}
return {
'properties': {
'id': charfield,
'course_id': charfield,
'usage_id': charfield,
'text': {'type': 'string', 'index': 'snowball', 'store': True},
'quote': {'type': 'string', 'index': 'snowball', 'store': True},
'created': charfield,
'updated': charfield,
}
}
@classmethod
def extract_document(cls, obj_id, obj=None):
"""Converts this instance into an Elasticsearch document"""
if obj is None:
obj = cls.get_model().objects.get(pk=obj_id)
return obj.as_dict()
note_searcher = NoteMappingType.search()
\ No newline at end of file
...@@ -19,6 +19,10 @@ CLIENT_SECRET = 'edx-notes-secret' ...@@ -19,6 +19,10 @@ CLIENT_SECRET = 'edx-notes-secret'
ELASTICSEARCH_URL = 'http://127.0.0.1:9200' ELASTICSEARCH_URL = 'http://127.0.0.1:9200'
ELASTICSEARCH_INDEX = 'edx-notes' ELASTICSEARCH_INDEX = 'edx-notes'
ES_URLS = ['http://localhost:9200']
ES_INDEXES = {'default': 'main_index'}
ES_DISABLED = False
# Number of rows to return by default in result. # Number of rows to return by default in result.
RESULTS_DEFAULT_SIZE = 25 RESULTS_DEFAULT_SIZE = 25
......
...@@ -19,4 +19,8 @@ DATABASES = { ...@@ -19,4 +19,8 @@ DATABASES = {
es.host = ELASTICSEARCH_URL es.host = ELASTICSEARCH_URL
es.index = ELASTICSEARCH_INDEX es.index = ELASTICSEARCH_INDEX
annotator.elasticsearch.RESULTS_MAX_SIZE = RESULTS_MAX_SIZE annotator.elasticsearch.RESULTS_MAX_SIZE = RESULTS_MAX_SIZE
ES_URLS = ['http://localhost:9200']
ES_INDEXES = {'default': 'main_index'}
ES_DISABLED = False
############################################################################### ###############################################################################
...@@ -21,3 +21,29 @@ es.host = ELASTICSEARCH_URL ...@@ -21,3 +21,29 @@ es.host = ELASTICSEARCH_URL
es.index = ELASTICSEARCH_INDEX es.index = ELASTICSEARCH_INDEX
annotator.elasticsearch.RESULTS_MAX_SIZE = RESULTS_MAX_SIZE annotator.elasticsearch.RESULTS_MAX_SIZE = RESULTS_MAX_SIZE
############################################################################### ###############################################################################
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stderr,
'formatter': 'standard',
},
},
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s %(process)d [%(name)s] %(filename)s:%(lineno)d - %(message)s',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': False,
},
},
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ Django==1.7.1 ...@@ -2,6 +2,7 @@ Django==1.7.1
requests==2.4.3 requests==2.4.3
djangorestframework==2.4.4 djangorestframework==2.4.4
django-rest-swagger==0.2.0 django-rest-swagger==0.2.0
elasticutils==0.10
elasticsearch==1.2.0 elasticsearch==1.2.0
annotator==0.12.0 annotator==0.12.0
django-cors-headers==0.13 django-cors-headers==0.13
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment