X-Git-Url: https://scm.cri.ensmp.fr/git/Plinn.git/blobdiff_plain/e9f249dc773d6b5b9648310e7bfcc5a2d3cbb330..b48c89e2b2b3426316e49cb1756793b352fe6abf:/catalog.py diff --git a/catalog.py b/catalog.py index 382fc57..4b925bf 100644 --- a/catalog.py +++ b/catalog.py @@ -11,6 +11,13 @@ from Products.ZCatalog.Catalog import Catalog import transaction from solr import * +# imports for Catalog class +from Products.PluginIndexes.interfaces import ILimitedResultIndex +from Products.ZCatalog.Lazy import LazyMap, LazyCat, LazyValues +from BTrees.IIBTree import intersection, IISet +from BTrees.IIBTree import weightedIntersection +import warnings + class SolrTransactionHook : ''' commit solr couplé sur le commit de la ZODB ''' def __init__(self, connection) : @@ -24,7 +31,7 @@ class SolrTransactionHook : self.connection.close() class CatalogTool(BaseCatalogTool) : - meta_type = 'Legivoc Catalog' + meta_type = 'Plinn Catalog' security = ClassSecurityInfo() manage_options = (BaseCatalogTool.manage_options[:5] + ({'label' : 'Solr', 'action' : 'manage_solr'},) + @@ -34,7 +41,7 @@ class CatalogTool(BaseCatalogTool) : def __init__(self, idxs=[]) : super(CatalogTool, self).__init__() - self._catalog = DelegatedCatalog() + self._catalog = DelegatedCatalog(self) self.solr_url = 'http://localhost:8983/solr' self.delegatedIndexes = ('Title', 'Description', 'SearchableText') @@ -100,6 +107,30 @@ InitializeClass(CatalogTool) class DelegatedCatalog(Catalog) : '''C'est ici qu'on délègue effectivement à Solr ''' + def __init__(self, zcat, brains=None) : + Catalog.__init__(self, brains=brains) + self.zcat = zcat + + def delegateSearch(self, query, plan) : + ''' + retours faux : + None signifie : pas de délégation, il faut continuer à interroger les autres index. + IISet() vide : pas de résultat lors de la délégation, on peut arrêter la recherche. + ''' + indexes = set(query.keys()).intersection(set(self.zcat.delegatedIndexes)) + if not indexes : + return None + delegatedQuery = {} + for i in indexes : + delegatedQuery[i] = query.pop(i) + try : plan.remove(i) + except ValueError : pass + c = SolrConnection(self.zcat.solr_url) + q =' AND '.join(['%s:"%s"' % item for item in delegatedQuery.items()]) + resp = c.query(q, fields='id', rows=len(self)) + c.close() + return IISet(filter(None, [self.uids.get(r['id']) for r in resp.results])) + def search(self, query, sort_index=None, reverse=0, limit=None, merge=1): """Iterate through the indexes, applying the query to each one. If merge is true then return a lazy result set (sorted if appropriate) @@ -129,6 +160,11 @@ class DelegatedCatalog(Catalog) : plan = cr.plan() if not plan: plan = self._sorted_search_indexes(query) + + # délégation + rs = self.delegateSearch(query, plan) + if rs is not None and not rs : + return LazyCat([]) indexes = self.indexes.keys() for i in plan: