Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index comments on solr #19

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ Changelog
2.1.1 (unreleased)
------------------

- Nothing changed yet.
- Fix a bug on the index operations optimization: attributes are
expected to be a list
[gforcada]

- Bug: do not downgrade full reindexes to partial ones
[gforcada]

- Patch CatalogAware objects as well, so comments are also indexed.
[gforcada]

- Handle comments' security as well.
[gforcada]

- Make archetypes optional.
[gforcada]

2.1 (2018-07-20)
----------------
Expand Down
13 changes: 11 additions & 2 deletions src/collective/indexing/indexer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from zope.interface import implements
from Products.Archetypes.CatalogMultiplex import CatalogMultiplex
from Products.CMFCore.CMFCatalogAware import CMFCatalogAware
from Products.CMFCore.CMFCatalogAware import CatalogAware
from collective.indexing.interfaces import IIndexQueueProcessor

try:
from Products.Archetypes.CatalogMultiplex import CatalogMultiplex
HAS_ARCHETYPES = True
except ImportError:
CatalogMultiplex = object()
HAS_ARCHETYPES = False

# container to hold references to the original and "monkeyed" indexing methods
# these are populated by `collective.indexing.monkey`
catalogMultiplexMethods = {}
catalogAwareMethods = {}
cmfcatalogAwareMethods = {}
monkeyMethods = {}


Expand All @@ -23,9 +30,11 @@ def getOwnIndexMethod(obj, name):

def getDispatcher(obj, name):
""" return named indexing method according on the used mixin class """
if isinstance(obj, CatalogMultiplex):
if HAS_ARCHETYPES and isinstance(obj, CatalogMultiplex):
op = catalogMultiplexMethods.get(name, None)
elif isinstance(obj, CMFCatalogAware):
op = cmfcatalogAwareMethods.get(name, None)
elif isinstance(obj, CatalogAware):
op = catalogAwareMethods.get(name, None)
else:
op = None
Expand Down
44 changes: 36 additions & 8 deletions src/collective/indexing/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@
from Acquisition import aq_base
from collective.indexing.indexer import catalogAwareMethods
from collective.indexing.indexer import catalogMultiplexMethods
from collective.indexing.indexer import cmfcatalogAwareMethods
from collective.indexing.indexer import monkeyMethods
from collective.indexing.queue import getQueue
from collective.indexing.queue import processQueue
from collective.indexing.subscribers import filterTemporaryItems
from logging import getLogger
# set up dispatcher containers for the original methods and
# hook up the new methods if that hasn't been done before...
from Products.Archetypes.BaseBTreeFolder import BaseBTreeFolder
from Products.Archetypes.CatalogMultiplex import CatalogMultiplex
try:
from Products.Archetypes.BaseBTreeFolder import BaseBTreeFolder
from Products.Archetypes.CatalogMultiplex import CatalogMultiplex
HAS_ARCHETYPES = True
except ImportError:
HAS_ARCHETYPES = False
from Products.CMFCore.CMFCatalogAware import CMFCatalogAware
from Products.CMFCore.CMFCatalogAware import CatalogAware
# patch CatalogTool.(unrestricted)searchResults to flush the queue
# before issuing a query
from Products.CMFPlone.CatalogTool import CatalogTool
from plone.app.discussion.interfaces import IConversation


logger = getLogger(__name__)
Expand Down Expand Up @@ -63,12 +70,28 @@ def reindexObjectSecurity(self, skip_self=False):
def _reindex(obj, path):
obj.reindexObject(idxs=self._cmf_security_indexes)

self.ZopeFindAndApply(self, search_sub=True, apply_func=_reindex)
if getattr(self, 'ZopeFindAndApply', None):
self.ZopeFindAndApply(self, search_sub=True, apply_func=_reindex)

try:
conversation = IConversation(self)
except TypeError:
return

for comment in conversation.getComments():
comment.reindexObject(idxs=self._cmf_security_indexes)


MODULES = [
(CMFCatalogAware, cmfcatalogAwareMethods),
(CatalogAware, catalogAwareMethods),
]

if HAS_ARCHETYPES:
MODULES.append((CatalogMultiplex, catalogMultiplexMethods))
MODULES.append((BaseBTreeFolder, {}))

for module, container in ((CMFCatalogAware, catalogAwareMethods),
(CatalogMultiplex, catalogMultiplexMethods),
(BaseBTreeFolder, {})):
for module, container in MODULES:
if not container and module is not None:
container.update({
'index': module.indexObject,
Expand Down Expand Up @@ -143,8 +166,13 @@ def unpatch():
was using collective.indexing (maybe indirectly through collective.solr).
"""
# remove the indexing patches
for module, container in ((CMFCatalogAware, catalogAwareMethods),
(CatalogMultiplex, catalogMultiplexMethods)):
MODULES = [
(CMFCatalogAware, cmfcatalogAwareMethods),
(CatalogAware, catalogAwareMethods),
]
if HAS_ARCHETYPES:
MODULES.append((CatalogMultiplex, catalogMultiplexMethods))
for module, container in MODULES:
module.indexObject = container['index']
module.reindexObject = container['reindex']
module.unindexObject = container['unindex']
Expand Down
9 changes: 5 additions & 4 deletions src/collective/indexing/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ def optimize(self):
op = min(max(op, UNINDEX), INDEX) # operator always between -1 and 1

# Handle attributes, None means all fields, and takes presedence
if isinstance(attr, (tuple, list)) and isinstance(iattr, (tuple, list)):
attr = tuple(set(attr).union(iattr))
if attr and iattr and isinstance(attr, (tuple, list)) and \
isinstance(iattr, (tuple, list)):
attr = list(set(attr).union(iattr))
else:
attr = None
attr = []

res[oid] = (op, obj, attr)

Expand All @@ -160,7 +161,7 @@ def process(self):
for op, obj, attributes in self.queue:
for name, util in utilities:
if op == INDEX:
util.index(obj, attributes)
util.index(obj, None)
elif op == REINDEX:
util.reindex(obj, attributes)
elif op == UNINDEX:
Expand Down