def __init__(self, id, extra=None, caller=None, index_factory=None, field_name=None, lexicon_id=None): self.id = id # Arguments can be passed directly to the constructor or # via the silly "extra" record. self._fieldname = field_name or getattr(extra, 'doc_attr', '') or id self._indexed_attrs = self._fieldname.split(',') self._indexed_attrs = [ attr.strip() for attr in self._indexed_attrs if attr ] lexicon_id = lexicon_id or getattr(extra, 'lexicon_id', '') lexicon = getattr(caller, lexicon_id, None) if lexicon is None: raise LookupError, 'Lexicon "%s" not found' % escape(lexicon_id) if not ILexicon.providedBy(lexicon): raise ValueError('Object "%s" does not implement ' 'ZCTextIndex Lexicon interface' % lexicon.getId()) self.lexicon_id = lexicon.getId() self._v_lexicon = lexicon if index_factory is None: if extra.index_type not in index_types.keys(): raise ValueError, 'Invalid index type "%s"' % escape( extra.index_type) self._index_factory = index_types[extra.index_type] self._index_type = extra.index_type else: self._index_factory = index_factory self.index = self._index_factory(aq_base(self.getLexicon()))
def getLexicon(self): """Get the lexicon for this index """ if hasattr(aq_base(self), 'lexicon'): # Fix up old ZCTextIndexes by removing direct lexicon ref # and changing it to an ID lexicon = getattr(aq_parent(aq_inner(self)), self.lexicon.getId()) self.lexicon_id = lexicon.getId() del self.lexicon if getattr(aq_base(self), 'lexicon_path', None): # Fix up slightly less old ZCTextIndexes by removing # the physical path and changing it to an ID. # There's no need to use a physical path, which otherwise # makes it difficult to move or rename ZCatalogs. self.lexicon_id = self.lexicon_path[-1] del self.lexicon_path try: return self._v_lexicon except AttributeError: lexicon = getattr(aq_parent(aq_inner(self)), self.lexicon_id) if not ILexicon.providedBy(lexicon): raise TypeError('Object "%s" is not a ZCTextIndex Lexicon' % repr(lexicon)) self._v_lexicon = lexicon return lexicon
def __init__(self, id, extra=None, caller=None, index_factory=None, field_name=None, lexicon_id=None): self.id = id # Arguments can be passed directly to the constructor or # via the silly "extra" record. self._fieldname = field_name or getattr(extra, 'doc_attr', '') or id self._indexed_attrs = self._fieldname.split(',') self._indexed_attrs = [attr.strip() for attr in self._indexed_attrs if attr] lexicon_id = lexicon_id or getattr(extra, 'lexicon_id', '') lexicon = getattr(caller, lexicon_id, None) if lexicon is None: raise LookupError('Lexicon "%s" not found' % escape(lexicon_id)) if not ILexicon.providedBy(lexicon): raise ValueError('Object "%s" does not implement ' 'ZCTextIndex Lexicon interface' % lexicon.getId()) self.lexicon_id = lexicon.getId() self._v_lexicon = lexicon if index_factory is None: if extra.index_type not in index_types.keys(): raise ValueError('Invalid index type "%s"' % escape( extra.index_type)) self._index_factory = index_types[extra.index_type] self._index_type = extra.index_type else: self._index_factory = index_factory self.index = self._index_factory(aq_base(self.getLexicon()))
def fix_searchable_text(context=None): # Fix bytes in opkapiindex # See https://github.com/plone/Products.CMFPlone/issues/2905 from Products.ZCTextIndex.interfaces import IZCTextIndex from Products.ZCTextIndex.interfaces import ILexicon catalog = api.portal.get_tool('portal_catalog') zctextindex = catalog._catalog.indexes['SearchableText'] opkapiindex = zctextindex.index values = opkapiindex._docwords.values() first_item = values[0] if isinstance(first_item, bytes): log.info('Rebuilding ZCTextIndexes. First item is a byte:') log.info(first_item) lexica = [i for i in catalog.values() if ILexicon.providedBy(i)] for lexicon in lexica: lexicon.clear() indexes = [ i for i in catalog.Indexes.objectValues() if IZCTextIndex.providedBy(i) ] for index in indexes: try: index.clear() except AttributeError as e: log.info(e) log.info('rebuilding {}'.format(index.__name__)) catalog._catalog.reindexIndex(index.__name__, getRequest()) else: log.info('Not rebuilding ZCTextIndexes. First item is not bytes:') log.info(first_item)
def getLexicon(self): """Get the lexicon for this index """ try: return ZCTextIndex.getLexicon(self) except: lexicon = getattr(getToolByName(getSite(), 'portal_catalog'), self.lexicon_id) if not ILexicon.providedBy(lexicon): raise TypeError('Object "%s" is not a ZCTextIndex Lexicon' % repr(lexicon)) self._v_lexicon = lexicon return lexicon