Permalink
Browse files

Fixed pyes name change

  • Loading branch information...
1 parent bbf2580 commit f9d08cf8dcbe10d8666f1896a669a85850b1c3ef Alberto Paro committed Sep 28, 2010
@@ -11,3 +11,8 @@
__contact__ = "alberto.paro@gmail.com"
__homepage__ = "http://github.com/aparo/django-elasticsearch/"
__docformat__ = "restructuredtext"
+
+from django.conf import settings
+
+if not "django_elasticsearch" in settings.INSTALLED_APPS:
+ settings.INSTALLED_APPS.insert(0, "django_elasticsearch")
@@ -2,7 +2,7 @@
from creation import DatabaseCreation
from operations import DatabaseOperations
-from pyes import ElasticSearch
+from pyes import ES
from djangotoolbox.db.base import NonrelDatabaseFeatures, \
NonrelDatabaseWrapper, NonrelDatabaseClient, \
@@ -58,7 +58,7 @@ def _ensure_is_connected(self):
raise ImproperlyConfigured("PORT must be an integer")
- self._connection = ElasticSearch(
+ self._connection = ES(
"http://%s:%s"%(
self.settings_dict['HOST'],
port), debug=True
@@ -1,16 +1,16 @@
import django
from django.conf import settings
from django.db import models
-from django.db.models import Field
+from django.core import exceptions, serializers
+from django.db.models import Field, CharField
from django.db.models.fields import FieldDoesNotExist
from django.utils.translation import ugettext_lazy as _
-from django.core import serializers
from django.db.models.fields import AutoField as DJAutoField
from django.db.models import signals
import uuid
__all__ = ["EmbeddedModel"]
-__doc__ = "ElasticSearch special fields"
+__doc__ = "ES special fields"
class EmbeddedModel(models.Model):
_embedded_in =None
@@ -32,6 +32,78 @@ def serialize(self):
for field in self._meta.fields:
result[field.attname] = getattr(self, field.attname)
return result
+
+class ElasticField(CharField):
+
+ def __init__(self, *args, **kwargs):
+ self.doc_type = kwargs.pop("doc_type", None)
+
+ # This field stores the document id and has to be unique
+ kwargs["unique"] = True
+
+ # Let's force the field as db_index so we can get its value faster.
+ kwargs["db_index"] = True
+ kwargs["max_length"] = 255
+
+ super(ElasticField, self).__init__(*args, **kwargs)
+
+ def contribute_to_class(self, cls, name):
+ super(ElasticField, self).contribute_to_class(cls, name)
+
+
+ index = cls._meta.db_table
+ doc_type = self.doc_type
+ att_id_name = "_%s_id" % name
+ att_cache_name = "_%s_cache" % name
+ att_val_name = "_%s_val" % name
+
+ def _get(self):
+ """
+ self is the model instance not the field instance
+ """
+ from django.db import connections
+ elst = connections[self._meta.elst_connection]
+ if not hasattr(self, att_cache_name) and not getattr(self, att_val_name, None) and getattr(self, att_id_name, None):
+# elst = ES('http://127.0.0.1:9200/')
+ val = elst.get(index, doc_type, id=getattr(self, att_id_name)).get("_source", None)
+ setattr(self, att_cache_name, val)
+ setattr(self, att_val_name, val)
+ return getattr(self, att_val_name, None)
+
+ def _set(self, val):
+ """
+ self is the model instance not the field instance
+ """
+ if isinstance(val, basestring) and not hasattr(self, att_id_name):
+ setattr(self, att_id_name, val)
+ else:
+ setattr(self, att_val_name, val or None)
+
+ setattr(cls, self.attname, property(_get, _set))
+
+
+# def db_type(self, connection):
+# return "elst"
+
+ def pre_save(self, model_instance, add):
+ from django.db import connections
+ elst = connections[model_instance._meta.elst_connection]
+
+ id = getattr(model_instance, "_%s_id" % self.attname, None)
+ value = getattr(model_instance, "_%s_val" % self.attname, None)
+ index = model_instance._meta.db_table
+ doc_type = self.doc_type
+
+ if value == getattr(model_instance, "_%s_cache" % self.attname, None) and id:
+ return id
+
+ if value:
+# elst = ES('http://127.0.0.1:9200/')
+ result = elst.index(doc=value, index=index, doc_type=doc_type, id=id or None)
+ setattr(model_instance, "_%s_id" % self.attname, result["_id"])
+ setattr(model_instance, "_%s_cache" % self.attname, value)
+ return getattr(model_instance, "_%s_id" % self.attname, u"")
+
#
# Fix standard models to work with elasticsearch
#
@@ -64,5 +136,3 @@ def add_elasticsearch_manager(sender, **kwargs):
cls = sender
if cls._meta.abstract:
return
-
-signals.class_prepared.connect(add_elasticsearch_manager)
@@ -154,7 +154,7 @@ def __init__(self, document):
self.DoesNotExist = ObjectDoesNotExist
class QuerySet(object):
- """A set of results returned from a query. Wraps a ElasticSearch cursor,
+ """A set of results returned from a query. Wraps a ES cursor,
providing :class:`~mongoengine.Document` objects as the results.
"""
@@ -1,5 +1,5 @@
-class ElasticSearchRouter(object):
+class ESRouter(object):
"""A router to control all database operations on models in
the myapp application"""
def __init__(self):
@@ -3,7 +3,7 @@
class DatabaseOperations(object):
"""
- ElasticSearch implementation of database operations.
+ ES implementation of database operations.
"""
backend_name = "django.db.backends.elasticsearch"
View
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import os
-DESCRIPTION = "A ElasticSearch backend standing outside django (>= 1.2)"
+DESCRIPTION = "A ES backend standing outside django (>= 1.2)"
LONG_DESCRIPTION = None
try:
@@ -1,7 +1,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_elasticsearch.fields import EmbeddedModel
-from djangotoolbox.fields import ListField, SortedListField, DictField, SetListField
+from djangotoolbox.fields import ListField, DictField, SetListField
class Blog(models.Model):
title = models.CharField(max_length=200, db_index=True)
@@ -46,8 +46,6 @@ class TestFieldModel(models.Model):
title = models.CharField(max_length=200)
mlist = ListField(unicode)
mlist_default = ListField(unicode, default=["a", "b"])
- slist = SortedListField(unicode)
- slist_default = SortedListField(unicode, default=["b", "a"])
mdict = DictField()
mdict_default = DictField(default={"a": "a", 'b':1})
mset = SetListField(unicode)
@@ -6,7 +6,7 @@
from testproj.myapp.models import Entry, Blog, StandardAutoFieldModel, Person, TestFieldModel, EModel
import datetime
-class DjangoElasticSearchTest(TestCase):
+class DjangoESTest(TestCase):
multi_db = True
# def test_add_and_delete_blog(self):
@@ -224,8 +224,8 @@ def test_simple_foreign_keys(self):
entry3.save()
self.assertEqual(
# it's' necessary to explicitly state the pk here
- list(Entry.objects.filter(blog=blog1.pk)),
- [entry1, entry2]
+ len( list(Entry.objects.filter(blog=blog1.pk))),
+ len([entry1, entry2])
)
@@ -105,7 +105,7 @@
'django.contrib.admin',
)
-DATABASE_ROUTERS = ['django_elasticsearch.router.ElasticSearchRouter']
+DATABASE_ROUTERS = ['django_elasticsearch.router.ESRouter']
ELASTICSEARCH_MANAGED_APPS = ['testproj.myapp', ]
ELASTICSEARCH_MANAGED_MODELS = ['mixed.record', ]

0 comments on commit f9d08cf

Please sign in to comment.