Permalink
Browse files

Issue 84. Fix get_indexes/get_doctypes silliness

All the S.get_indexes() returned a list of indexes except the one
in Django contrib which could return a basestring or a list of
indexes.

Ditto for S.get_doctypes().

This fixes that, adds docs, and adds tests.
  • Loading branch information...
1 parent cd76217 commit 7dcc0344d22b6b5ea2ab33066a2f26634dfcc734 Will Kahn-Greene committed Oct 16, 2012
View
10 docs/django.rst
@@ -72,6 +72,16 @@ file:
run queries for other models in `main_index` because that's the
default.
+ Example 3::
+
+ ES_INDEXES = {'default': ['main_index'],
+ 'splugs': ['splugs_index']}
+
+ FIXME: The API allows for this. Pretty sure it should query
+ multiple indexes, but we have no tests for that and I haven't
+ tested it, either.
+
+
.. data:: ES_TIMEOUT
Defines the timeout for the `ES` connection. This defaults to 5
View
8 elasticutils/__init__.py
@@ -651,6 +651,7 @@ def _do_search(self):
return self._results_cache
def get_es(self, default_builder=get_es):
+ """Returns the ES object to use."""
# The last one overrides earlier ones.
for action, value in reversed(self.steps):
if action == 'es_builder':
@@ -662,16 +663,21 @@ def get_es(self, default_builder=get_es):
return default_builder()
def get_indexes(self, default_indexes=DEFAULT_INDEXES):
+ """Returns the list of indexes to act on."""
for action, value in reversed(self.steps):
if action == 'indexes':
return value
if self.type is not None:
- return self.type.get_index()
+ indexes = self.type.get_index()
+ if isinstance(indexes, basestring):
+ indexes = [indexes]
+ return indexes
return default_indexes
def get_doctypes(self, default_doctypes=DEFAULT_DOCTYPES):
+ """Returns the list of doctypes to use."""
for action, value in reversed(self.steps):
if action == 'doctypes':
return value
View
11 elasticutils/contrib/django/__init__.py
@@ -204,15 +204,22 @@ def raw(self):
return hits
def get_es(self, default_builder=None):
+ """Returns the ES to use."""
# Override the default_builder with the Django one
return super(S, self).get_es(default_builder=get_es)
def get_indexes(self, default_indexes=None):
+ """Returns the list of indexes to act on."""
doctype = self.type.get_mapping_type_name()
indexes = (settings.ES_INDEXES.get(doctype) or
settings.ES_INDEXES['default'])
+ if isinstance(indexes, basestring):
+ indexes = [indexes]
return super(S, self).get_indexes(default_indexes=indexes)
def get_doctypes(self, default_doctypes=None):
- doctype = self.type.get_mapping_type_name()
- return super(S, self).get_doctypes(default_doctypes=doctype)
+ """Returns the doctypes (or mapping type names) to use."""
+ doctypes = self.type.get_mapping_type_name()
+ if isinstance(doctypes, basestring):
+ doctypes = [doctypes]
+ return super(S, self).get_doctypes(default_doctypes=doctypes)
View
7 elasticutils/contrib/django/models.py
@@ -44,7 +44,12 @@ def get_index(cls):
"""
indexes = settings.ES_INDEXES
- return indexes.get(cls.get_mapping_type_name()) or indexes['default']
+ index = indexes.get(cls.get_mapping_type_name()) or indexes['default']
+ if not (isinstance(index, basestring)):
+ # FIXME - not sure what to do here, but we only want one
+ # index and somehow this isn't one index.
+ index = index[0]
+ return index
@classmethod
def get_mapping_type_name(cls):
View
47 elasticutils/tests/test_django.py
@@ -25,7 +25,7 @@
from elasticutils.contrib.django import (
S, F, get_es, InvalidFieldActionError)
from elasticutils.tests.django_utils import (
- FakeDjangoMappingType, FakeModel)
+ FakeDjangoMappingType, FakeDjangoMappingTypeWithIndexes, FakeModel)
except ImportError:
SKIP_TESTS = True
@@ -45,6 +45,51 @@ def test_require_mapping_type(self):
"""The Django S requires a mapping type."""
self.assertRaises(TypeError, S)
+ @requires_django
+ def test_get_indexes(self):
+ """Test get_indexes always returns a list of strings."""
+
+ # Pulls it from ES_INDEXES (list of strings).
+ s = S(FakeDjangoMappingType)
+ eq_(s.get_indexes(), ['elasticutilstest'])
+
+ # Pulls it from ES_INDEXES (string).
+ old_indexes = settings.ES_INDEXES
+ try:
+ settings.ES_INDEXES = {'default': 'elasticutilstest'}
+
+ s = S(FakeDjangoMappingType)
+ eq_(s.get_indexes(), ['elasticutilstest'])
+ finally:
+ settings.ES_INDEXES = old_indexes
+
+ # Pulls from indexes.
+ s = S(FakeDjangoMappingType).indexes('footest')
+ eq_(s.get_indexes(), ['footest'])
+
+ s = S(FakeDjangoMappingType).indexes('footest', 'footest2')
+ eq_(s.get_indexes(), ['footest', 'footest2'])
+
+ s = S(FakeDjangoMappingType).indexes('footest').indexes('footest2')
+ eq_(s.get_indexes(), ['footest', 'footest2'])
+
+ @requires_django
+ def test_get_doctypes(self):
+ """Test get_doctypes always returns a list of strings."""
+ # Pulls from ._meta.db_table.
+ s = S(FakeDjangoMappingType)
+ eq_(s.get_doctypes(), ['fake'])
+
+ # Pulls from doctypes.
+ s = S(FakeDjangoMappingType).doctypes('footype')
+ eq_(s.get_doctypes(), ['footype'])
+
+ s = S(FakeDjangoMappingType).doctypes('footype', 'footype2')
+ eq_(s.get_doctypes(), ['footype', 'footype2'])
+
+ s = S(FakeDjangoMappingType).doctypes('footype').doctypes('footype2')
+ eq_(s.get_doctypes(), ['footype', 'footype2'])
+
class ESTest(TestCase):
@requires_django
View
2 test_settings.py
@@ -1,3 +1,3 @@
ES_HOSTS = ['127.0.0.1:9200']
-ES_INDEXES = {'default': 'elasticutilstest'}
+ES_INDEXES = {'default': ['elasticutilstest']}
ES_TIMEOUT = 10

0 comments on commit 7dcc034

Please sign in to comment.