diff --git a/pyes/es.py b/pyes/es.py index 5e398a61..a51a3596 100644 --- a/pyes/es.py +++ b/pyes/es.py @@ -178,10 +178,10 @@ def default(self, value): """ if isinstance(value, datetime): - return value.strftime("%Y-%m-%dT%H:%M:%S") + return value.isoformat() elif isinstance(value, date): dt = datetime(value.year, value.month, value.day, 0, 0, 0) - return dt.strftime("%Y-%m-%dT%H:%M:%S") + return dt.isoformat() elif isinstance(value, Decimal): return float(str(value)) else: @@ -481,7 +481,8 @@ def _validate_indices(self, indices=None): If `indices` is not supplied, returns the default_indices. """ - indices = indices or self.default_indices + if indices is None: + return self.default_indices if isinstance(indices, basestring): indices = [indices] return indices @@ -491,12 +492,26 @@ def _dump_curl_request(self, request): params = {'pretty': 'true'} params.update(request.parameters) method = Method._VALUES_TO_NAMES[request.method] - url = urlunsplit(('http', self.servers[0], request.uri, urlencode(params), '')) + ### using the new (prot,host,port) tuple, the below code throws a 'can not + ### concatenate tuple + str' exception + #url = urlunsplit(('http', self.servers[0], request.uri, urlencode(params), '')) + url = urlunsplit(('http', ("%s:%s" % self.servers[0][1:3]), request.uri, urlencode(params), '')) curl_cmd = "curl -X%s '%s'" % (method, url) if request.body: curl_cmd += " -d '%s'" % request.body print >> self.dump_curl, curl_cmd + def _get_default_indices(self): + return self._default_indices + + def _set_default_indices(self, default_indices): + if default_indices is not None: + default_indices = self._validate_indices(default_indices) + self._default_indices = default_indices + + default_indices = property(_get_default_indices, _set_default_indices) + del _get_default_indices, _set_default_indices + #---- Admin commands def status(self, indices=None): """ @@ -632,7 +647,7 @@ def change_aliases(self, commands): } return self._send_request('POST', "_aliases", body) - def add_alias(self, alias, indices): + def add_alias(self, alias, indices=None): """Add an alias to point to a set of indices. """ @@ -640,7 +655,7 @@ def add_alias(self, alias, indices): return self.change_aliases(['add', index, alias] for index in indices) - def delete_alias(self, alias, indices): + def delete_alias(self, alias, indices=None): """Delete an alias. The specified index or indices are deleted from the alias, if they are @@ -652,7 +667,7 @@ def delete_alias(self, alias, indices): return self.change_aliases(['remove', index, alias] for index in indices) - def set_alias(self, alias, indices): + def set_alias(self, alias, indices=None): """Set an alias. This handles removing the old list of indices pointed to by the alias. diff --git a/pyes/tests/test_serialize.py b/pyes/tests/test_serialize.py index fd61db60..3d3f0351 100644 --- a/pyes/tests/test_serialize.py +++ b/pyes/tests/test_serialize.py @@ -5,7 +5,8 @@ import unittest from pyes.tests import ESTestCase -from pyes import TermQuery +from pyes import TermQuery, RangeQuery +from pyes.utils import ESRange from datetime import datetime """ @@ -42,6 +43,7 @@ def setUp(self): self.conn.put_mapping(self.document_type, {'properties':mapping}, self.index_name) self.conn.index({"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1, 'inserted':datetime(2010, 10, 22, 12, 12, 12)}, self.index_name, self.document_type, 1) self.conn.index({"name":"Bill Baloney", "parsedtext":"Joe Testere nice guy", "uuid":"22222", "position":2, 'inserted':datetime(2010, 10, 22, 12, 12, 10)}, self.index_name, self.document_type, 2) + self.conn.index({"name":"Jesus H Christ", "parsedtext":"Bible guy", "uuid":"33333", "position":3, 'inserted':datetime(1, 1, 1, 0, 0, 0)}, self.index_name, self.document_type, 3) self.conn.refresh(self.index_name) def test_TermQuery(self): @@ -51,6 +53,13 @@ def test_TermQuery(self): hit = resultset[0] self.assertEquals(hit.inserted, datetime(2010, 10, 22, 12, 12, 12)) + def test_DateBefore1900(self): + q = RangeQuery(ESRange("inserted", datetime(1, 1, 1), datetime(2, 1, 1))) + resultset = self.conn.search(query=q, indices=self.index_name) + self.assertEquals(resultset.total, 1) + hit = resultset[0] + self.assertEquals(hit.inserted, datetime(1, 1, 1, 0, 0, 0)) + if __name__ == "__main__": unittest.main() diff --git a/setup.py b/setup.py index 116f0631..654581fb 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,41 @@ from setuptools import setup, find_packages, Command from setuptools.command.test import test as TestCommand -import pyes as distmeta +# Extract distribution meta values, hint taken from Celery + +import re +re_meta = re.compile(r'__(\w+?)__\s*=\s*(.*)') +re_vers = re.compile(r'VERSION\s*=\s*\((.*?)\)') +re_doc = re.compile(r'^"""(.+?)"""') +rq = lambda s: s.strip("\"'") + +def add_default(m): + attr_name, attr_value = m.groups() + return ((attr_name, rq(attr_value)), ) + + +def add_version(m): + v = list(map(rq, m.groups()[0].split(", "))) + return (("VERSION", ".".join(v[0:3]) + "".join(v[3:])), ) + + +def add_doc(m): + return (("doc", m.groups()[0]), ) + +pats = {re_meta: add_default, + re_vers: add_version, + re_doc: add_doc} +here = os.path.abspath(os.path.dirname(__file__)) +meta_fh = open(os.path.join(here, "pyes/__init__.py")) +try: + meta = {} + for line in meta_fh: + for pattern, handler in pats.items(): + m = pattern.match(line.strip()) + if m: + meta.update(handler(m)) +finally: + meta_fh.close() class QuickRunTests(TestCommand): @@ -53,11 +87,11 @@ def run(self, *args, **kwargs): setup( name='pyes', - version=distmeta.__version__, + version=meta['VERSION'], description="Python Elastic Search driver", - author=distmeta.__author__, - author_email=distmeta.__contact__, - url=distmeta.__homepage__, + author=meta['author'], + author_email=meta['contact'], + url=meta['homepage'], platforms=["any"], license="BSD", packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*', "docs.*"]),