Commit bbddef1c authored by Andreas Jung's avatar Andreas Jung

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().
parent e9c8fea9
...@@ -446,7 +446,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -446,7 +446,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
## on below here... Most of this stuff came from ZTables with tweaks. ## on below here... Most of this stuff came from ZTables with tweaks.
## But I worry about :-) ## 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. Iterate through the indexes, applying the query to each one.
""" """
...@@ -454,6 +454,23 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -454,6 +454,23 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
rs = None # resultset rs = None # resultset
data = self.data 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={} if used is None: used={}
for i in self.indexes.keys(): for i in self.indexes.keys():
...@@ -464,8 +481,10 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -464,8 +481,10 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# Optimization: we check if there is some work for the index. # 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) r=index._apply_index(request)
if r is not None: if r is not None:
...@@ -564,7 +583,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -564,7 +583,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
key = (key,id(lm)) key = (key,id(lm))
append((key,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: # Get search arguments:
if REQUEST is None and not kw: if REQUEST is None and not kw:
...@@ -601,7 +620,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -601,7 +620,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# Perform searches with indexes and sort_index # Perform searches with indexes and sort_index
r=[] r=[]
used=self._indexedSearch(kw, sort_index, r.append, used) used=self._indexedSearch(kw, sort_index, r.append, used, optimize)
if not r: if not r:
return LazyCat(r) return LazyCat(r)
......
...@@ -565,14 +565,14 @@ class ZCatalog(Folder, Persistent, Implicit): ...@@ -565,14 +565,14 @@ class ZCatalog(Folder, Persistent, Implicit):
'width': 8}) 'width': 8})
return r 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 the catalog according to the ZTables search interface.
Search terms can be passed in the REQUEST or as keyword Search terms can be passed in the REQUEST or as keyword
arguments. arguments.
""" """
return apply(self._catalog.searchResults, (REQUEST,used), kw) return apply(self._catalog.searchResults, (REQUEST,used,optimize), kw)
__call__=searchResults __call__=searchResults
......
...@@ -41,3 +41,11 @@ ZCatalog - searchResults: specifying parameters for a search query ...@@ -41,3 +41,11 @@ ZCatalog - searchResults: specifying parameters for a search query
'level' -- only applies to Path Index. Specifies the directory 'level' -- only applies to Path Index. Specifies the directory
level to start searching. (optional, default: 0) level to start searching. (optional, default: 0)
Parameters for searchResults():
'optimize' -- ZCatalog performs a query optimization for queries
not passed through the web (either as dictionary or mapping). To
disable this optimization set optimize to 0 (optional, default: 1).
Optimization is disabled for searches through the web.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment