[Zope-Checkins] CVS: Zope/lib/python/Products/ZCatalog - Catalog.py:1.86 ZCatalog.py:1.108

Andreas Jung andreas@digicool.com
Fri, 3 May 2002 12:32:21 -0400


Update of /cvs-repository/Zope/lib/python/Products/ZCatalog
In directory cvs.zope.org:/tmp/cvs-serv26844

Modified Files:
	Catalog.py ZCatalog.py 
Log Message:
Eased the query optimization introduced with Zope 2.4.X
(revision 1.72 or Catalog.py). 

TTW searches are no longer optimized to restore the old 
behaviour (user does not fill out any form fields ->
return all hits)

For application related searches we keep the optimization
but it is possible to disable optimization by passing
'optimize=0' as additional parameters to searchResults().




=== Zope/lib/python/Products/ZCatalog/Catalog.py 1.85 => 1.86 ===
 ## But I worry about :-)
 
-    def _indexedSearch(self, request , sort_index, append, used):
+    def _indexedSearch(self, request , sort_index, append, used, optimize):
         """
         Iterate through the indexes, applying the query to each one.
         """
@@ -454,6 +454,23 @@
         rs   = None             # resultset
         data = self.data
 
+        # We can optimize queries by only calling index._apply_index()
+        # for indexes involved in a search (means the request object
+        # contains the Id of the corresponding index). But we must
+        # take care of two kind of searches:
+        #
+        # - searches through the web (empty input fields means   
+        #   the index should return *all* records). For such queries
+        #   we disable query optimization.
+        #
+        # - application-related searches (search queries are passed as
+        #   dictionary or mapping). Such queries usually define exactly
+        #   what they are looking for (WYGIWYSF - what you get is what
+        #   you search for).
+
+        if hasattr(request,'environ'):   # we have a request instance
+            optimize = 0
+
         if used is None: used={}
         for i in self.indexes.keys():
 
@@ -464,9 +481,11 @@
 
                 # Optimization: we check if there is some work for the index.
                 # 
-                if request.has_key(index.getId()) :
-                    if request[index.getId()] != '':
+
+                if optimize and request.has_key(index.getId()) :
                         r=index._apply_index(request)
+                else:
+                    r=index._apply_index(request)
 
                 if r is not None:
                     r, u = r
@@ -564,7 +583,7 @@
                     key = (key,id(lm))
                     append((key,lm))
 
-    def searchResults(self, REQUEST=None, used=None, **kw):
+    def searchResults(self, REQUEST=None, used=None, optimize=1, **kw):
         
         # Get search arguments:
         if REQUEST is None and not kw:
@@ -601,7 +620,7 @@
         # Perform searches with indexes and sort_index
         r=[]
         
-        used=self._indexedSearch(kw, sort_index, r.append, used)
+        used=self._indexedSearch(kw, sort_index, r.append, used, optimize)
         if not r:
             return LazyCat(r)
 


=== Zope/lib/python/Products/ZCatalog/ZCatalog.py 1.107 => 1.108 ===
         return r
 
-    def searchResults(self, REQUEST=None, used=None, **kw):
+    def searchResults(self, REQUEST=None, used=None, optimize=1, **kw):
         """
         Search the catalog according to the ZTables search interface.
         Search terms can be passed in the REQUEST or as keyword
         arguments. 
         """
 
-        return apply(self._catalog.searchResults, (REQUEST,used), kw)
+        return apply(self._catalog.searchResults, (REQUEST,used,optimize), kw)
 
     __call__=searchResults