[Zope-Checkins] CVS: Zope2 - UnIndex.py:1.1.2.4

andreas@serenade.digicool.com andreas@serenade.digicool.com
Fri, 18 May 2001 11:16:42 -0400


Update of /cvs-repository/Zope2/lib/python/Products/PluginIndexes/common
In directory serenade:/tmp/cvs-serv2227/common

Modified Files:
      Tag: ajung-dropin-registry
	UnIndex.py 
Log Message:
yeah.....KeywordIndexes are now able to "and" and "or" search results



--- Updated File UnIndex.py in package Zope2 --
--- UnIndex.py	2001/05/16 19:27:04	1.1.2.3
+++ UnIndex.py	2001/05/18 15:16:42	1.1.2.4
@@ -97,10 +97,10 @@
 
 from BTrees.OOBTree import OOBTree, OOSet
 from BTrees.IOBTree import IOBTree
-from BTrees.IIBTree import IITreeSet, IISet, union
+from BTrees.IIBTree import IITreeSet, IISet, union, intersection
 import BTrees.Length
 
-import sys
+import sys,exceptions
 
 _marker = []
 
@@ -146,6 +146,9 @@
         self.ignore_ex=ignore_ex        # currently unimplimented
         self.call_methods=call_methods
 
+        self.operators = ['or','and'] 
+        self.useOperator = 'or'
+
         self.__len__=BTrees.Length.Length() # see __len__ method docstring
         self.clear()
 
@@ -351,6 +354,11 @@
         column + '_usage', it is sniffed for information on how to
         handle applying the index.
 
+        If the request contains a parameter with the name of the 
+        column = '_operator' this overrides the default method
+        ('or') to combine search results. Valid values are "or"
+        and "and".
+
         If None is not returned as a result of the abovementioned
         constraints, two objects are returned.  The first object is a
         ResultSet containing the record numbers of the matching
@@ -384,11 +392,26 @@
         r = None
         opr = None
 
+        if request.has_key(id+"_operator"):
+            # check if someone overrides the "or" parameter
+            # for combining search results 
+            operator = request[id+"_operator"].lower()
+            if not operator in self.operators :
+                raise exepctions.RuntimeError,"operator not valid: %s" % op
+        else:
+            operator = self.useOperator
+
+        # depending on the operator we use intersection of union
+        if operator=="or":  set_func = union
+        else:               set_func = intersection
+
+
         if request.has_key(id+'_usage'):
             # see if any usage params are sent to field
             opr=string.split(string.lower(request[id+"_usage"]),':')
             opr, opr_args=opr[0], opr[1:]
 
+
         if opr=="range":   # range search
             if 'min' in opr_args: lo = min(keys)
             else: lo = None
@@ -402,14 +425,14 @@
             for k, set in setlist:
                 if type(set) is IntType:
                     set = IISet((set,))
-                r = union(r, set)
+                r = set_func(r, set)
         else: # not a range search
             for key in keys:
                 set=index.get(key, None)
                 if set is not None:
                     if type(set) is IntType:
                         set = IISet((set,))
-                    r = union(r, set)
+                    r = set_func(r, set)
 
         if type(r) is IntType:  r=IISet((r,))
         if r is None: