Add ORQuery API
authorJonathan Griffin <jgriffin@mozilla.com>
Tue, 07 Jun 2011 12:17:34 -0700
changeset 29 3c5f736ea442b7133be171d432d1b31d9933f738
parent 28 ccb8ae2df8567fbbb3ae1ff9e7286450f1a12fa1
child 30 931345d8607ecd15ea57258374b47783cee7aa21
push id30
push userjgriffin@mozilla.com
push dateTue, 07 Jun 2011 19:17:46 +0000
Add ORQuery API
mozautoeslib/eslib.py
setup.py
--- a/mozautoeslib/eslib.py
+++ b/mozautoeslib/eslib.py
@@ -69,16 +69,59 @@ class ESLib(object):
     boolquery = BoolQuery()
     self._add_fieldlist_to_boolquery(boolquery, include, True)
     self._add_fieldlist_to_boolquery(boolquery, exclude, False)
     if sort:
       boolquery.sort = sort
 
     return boolquery
 
+  def ORQuery(self, ORItems, size=10000, doc_type=None):
+    """Return a list of hits that match any of the combination of terms
+       specified in the ORItems list of dicts.
+
+       Example:
+         return hits that match any of the following mahine/starttime
+         combinations:
+
+       result = eslib.ORQuery([
+        {'machine': 'talos-r1', 'starttime': '1306918341'},
+        {'machine': 'talos-r2', 'starttime': '1259351812'},
+       ])
+    """
+
+    if doc_type:
+      self.doc_type = doc_type
+
+    resultlist = []
+
+    orList = []
+    for item in ORItems:
+      andList = []
+      for key in item:
+        andList.append(TermFilter(key, item[key]))
+      orList.append(ANDFilter(andList))
+    orq = ORFilter(orList)
+
+    q = FilteredQuery(MatchAllQuery(), orq)
+    result = self.connection.search(query=q,
+                                    indexes=[self.index],
+                                    doc_types=self.doc_type)
+
+    if result and result['hits'] and result['hits']['hits']:
+      # partially flatten the data
+      for hit in result['hits']['hits']:
+        if not '_source' in hit:
+          raise Exception("Key ['_source'] not found in response hit")
+        resultlist.append(hit['_source'])
+    else:
+      raise Exception("Key ['hits']['hits'] not found in response data")
+
+    return resultlist
+
   def query(self, include={}, exclude={}, size=None, doc_type=None, sort=None,
             withSource=False):
     """Return a list of hits which match all the fields in 'include',
        but none of the fields in 'exclude', up to a maximum of 'size' hits,
        or all hits when 'size' is None.
 
        Each field in 'include' and 'exclude' can be one of three types:
          - string: the field MUST include that string
--- a/setup.py
+++ b/setup.py
@@ -33,17 +33,17 @@
 # the provisions above, a recipient may use your version of this file under
 # the terms of any one of the MPL, the GPL or the LGPL.
 #
 # ***** END LICENSE BLOCK *****
 
 import sys
 from setuptools import setup, find_packages
 
-version = '0.1.0'
+version = '0.1.1'
 
 deps = ['pyes == 0.15']
 
 if sys.version < '2.5' or sys.version >= '3.0':
     print >>sys.stderr, '%s requires Python >= 2.5 nd < 3.0' % _PACKAGE_NAME
     sys.exit(1)
 
 try: