X-Git-Url: https://scm.cri.ensmp.fr/git/Plinn.git/blobdiff_plain/e9f249dc773d6b5b9648310e7bfcc5a2d3cbb330..a2bbd1ad4d6c663e1b0e38eb62d47f386dd6e36e:/catalog.py?ds=inline diff --git a/catalog.py b/catalog.py index 382fc57..f2736cb 100644 --- a/catalog.py +++ b/catalog.py @@ -5,39 +5,74 @@ from Products.CMFCore.interfaces import IIndexableObject from Products.CMFCore.CatalogTool import CatalogTool as BaseCatalogTool from Products.CMFCore.CatalogTool import IndexableObjectWrapper from Products.PageTemplates.PageTemplateFile import PageTemplateFile -from Products.CMFCore.permissions import ModifyPortalContent +from Products.CMFCore.permissions import ModifyPortalContent, ManagePortal from zope.component import queryMultiAdapter 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 + +_VOLATILE_SOLR_NAME = '_v_solrConnection' + class SolrTransactionHook : ''' commit solr couplé sur le commit de la ZODB ''' - def __init__(self, connection) : - self.connection = connection + def __init__(self, context, con) : + self.context = context + self.con = con def __call__(self, status) : if status : - self.connection.commit() - self.connection.close() + self.con.commit() + self.con.close() else : - self.connection.close() + self.con.close() + try : + delattr(self.context, _VOLATILE_SOLR_NAME) + except AttributeError : + pass class CatalogTool(BaseCatalogTool) : - meta_type = 'Legivoc Catalog' + meta_type = 'Plinn Catalog' security = ClassSecurityInfo() manage_options = (BaseCatalogTool.manage_options[:5] + ({'label' : 'Solr', 'action' : 'manage_solr'},) + BaseCatalogTool.manage_options[5:]) - manage_solr = PageTemplateFile('www/manage_solr', globals()) + manage_solr = PageTemplateFile('www/manage_solr.pt', globals(), __name__='manage_solr') + 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') + security.declarePublic('getDelegatedIndexes') + def getDelegatedIndexes(self) : + """ read the method name """ + return self.delegatedIndexes + + security.declareProtected(ManagePortal, 'setDelegatedIndexes') + def setDelegatedIndexes(self, indexes, REQUEST=None) : + """setDelegatedIndexes documentation""" + self.delegatedIndexes = tuple([i.strip() for i in indexes if i.strip()]) + if REQUEST : + REQUEST.RESPONSE.redirect(self.absolute_url() + '/manage_solr?manage_tabs_message=Saved changes.') + + def _getSolrConnection(self) : + if not hasattr(self, _VOLATILE_SOLR_NAME) : + con = SolrConnection(self.solr_url) + setattr(self, _VOLATILE_SOLR_NAME, con) + txn = transaction.get() + txn.addAfterCommitHook(SolrTransactionHook(self, con)) + return getattr(self, _VOLATILE_SOLR_NAME) + security.declarePrivate('solrAdd') def solrAdd(self, object, idxs=[], uid=None) : if IIndexableObject.providedBy(object): @@ -54,10 +89,8 @@ class CatalogTool(BaseCatalogTool) : for name in idxs : attr = getattr(w, name, '') data[name] = attr() if callable(attr) else attr - c = SolrConnection(self.solr_url) + c = self._getSolrConnection() c.add(**data) - txn = transaction.get() - txn.addAfterCommitHook(SolrTransactionHook(c)) # PortalCatalog api overloads @@ -88,11 +121,9 @@ class CatalogTool(BaseCatalogTool) : """Remove from catalog. """ super(CatalogTool, self).unindexObject(object) - c = SolrConnection(self.solr_url) + c = self._getSolrConnection() url = self.__url(object) c.delete(id=url) - txn = transaction.get() - txn.addAfterCommitHook(SolrTransactionHook(c)) InitializeClass(CatalogTool) @@ -100,6 +131,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 +184,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: