Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge in multi_get support. Fix #37. Fix #85.

  • Loading branch information...
commit 81c0449dbb540e33b9d54e16f1c2daf9cc24af31 2 parents c89ad05 + 1d935ba
@erikrose erikrose authored
View
2  docs/source/api.rst
@@ -64,6 +64,8 @@ so it'll be hard to miss.
.. automethod:: more_like_this(index, doc_type, id, fields, body=''[, other kwargs listed below])
+ .. automethod:: multi_get(ids, index=None, doc_type=None, fields=None[, other kwargs listed below])
+
.. automethod:: open_index(index)
.. automethod:: optimize(index=None[, other kwargs listed below])
View
38 pyelasticsearch/client.py
@@ -2,6 +2,7 @@
from __future__ import absolute_import
from datetime import datetime
+from operator import itemgetter
from functools import wraps
from logging import getLogger
import re
@@ -440,6 +441,43 @@ def get(self, index, doc_type, id, query_params=None):
return self.send_request('GET', [index, doc_type, id],
query_params=query_params)
+ @es_kwargs()
+ def multi_get(self, ids, index=None, doc_type=None, fields=None,
+ query_params=None):
+ """
+ Get multiple typed JSON documents from ES.
+
+ :arg ids: An iterable, each element of which can be either an a dict or
+ an id (int or string). IDs are taken to be document IDs. Dicts are
+ passed through the Multi Get API essentially verbatim, except that
+ any missing ``_type``, ``_index``, or ``fields`` keys are filled in
+ from the defaults given in the ``index``, ``doc_type``, and
+ ``fields`` args.
+ :arg index: Default index name from which to retrieve
+ :arg doc_type: Default type of document to get
+ :arg fields: Default fields to return
+
+ See `ES's Multi Get API`_ for more detail.
+
+ .. _`ES's Multi Get API`:
+ http://www.elasticsearch.org/guide/reference/api/multi-get.html
+ """
+ doc_template = dict(
+ filter(
+ itemgetter(1),
+ [('_index', index), ('_type', doc_type), ('fields', fields)]))
+
+ docs = []
+ for id in ids:
+ doc = doc_template.copy()
+ if isinstance(id, dict):
+ doc.update(id)
+ else:
+ doc['_id'] = id
+ docs.append(doc)
+
+ return self.send_request(
+ 'GET', ['_mget'], {'docs': docs}, query_params=query_params)
@es_kwargs('routing', 'parent', 'timeout', 'replication', 'consistency',
'percolate', 'refresh', 'retry_on_conflict', 'fields')
View
12 pyelasticsearch/tests.py
@@ -315,6 +315,18 @@ def testGetByID(self):
result = self.conn.get('test-index', 'test-type', 1)
self.assertResultContains(result, {'_type': 'test-type', '_id': '1', '_source': {'name': 'Joe Tester'}, '_index': 'test-index'})
+ def testMultiGetSimple(self):
+ result = self.conn.multi_get([1], index='test-index', doc_type='test-type')
+ self.assertResultContains(result, {'docs': [{'_type': 'test-type', '_id': '1', '_source': {'name': 'Joe Tester'}, '_index': 'test-index', "_version": 1, "exists": True}]})
+
+ def testMultiGetMix(self):
+ result = self.conn.multi_get([{'_type': 'test-type', '_id': 1}], index='test-index')
+ self.assertResultContains(result, {'docs': [{'_type': 'test-type', '_id': '1', '_source': {'name': 'Joe Tester'}, '_index': 'test-index', "_version": 1, "exists": True}]})
+
+ def testMultiGetCustom(self):
+ result = self.conn.multi_get([{'_type': 'test-type', '_id': 1, 'fields': ['name'], '_index': 'test-index'}])
+ self.assertResultContains(result, {'docs': [{'_type': 'test-type', '_id': '1', 'fields': {'name': 'Joe Tester'}, '_index': 'test-index', "_version": 1, "exists": True}]})
+
def testGetCountBySearch(self):
result = self.conn.count('name:joe', index='test-index')
self.assertResultContains(result, {'count': 1})
Please sign in to comment.
Something went wrong with that request. Please try again.