Update views and search engine so that results displayed in a view component can be excluded from other search results
authorThierry Florac <tflorac@ulthar.net>
Fri, 14 Dec 2018 13:33:30 +0100
changeset 116 23855cc6ae6d
parent 115 6387a1195db4
child 117 4ec0f32ec869
Update views and search engine so that results displayed in a view component can be excluded from other search results
src/pyams_content_es/shared/view/__init__.py
src/pyams_content_es/shared/view/reference.py
--- a/src/pyams_content_es/shared/view/__init__.py	Fri Dec 14 11:12:42 2018 +0100
+++ b/src/pyams_content_es/shared/view/__init__.py	Fri Dec 14 13:33:30 2018 +0100
@@ -22,6 +22,7 @@
 from pyams_content.features.search import ISearchFolder
 from pyams_content.shared.view.interfaces import IViewQuery, IViewQueryEsParamsExtension, IViewQueryFilterExtension, \
     IViewUserQuery, IWfView, RELEVANCE_ORDER
+from pyams_content.shared.view.portlet import SEARCH_EXCLUDED_ITEMS
 from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.list import unique_iter
 from pyams_utils.timezone import tztime
@@ -103,6 +104,10 @@
         return unique_iter(items), search.count()
 
 
+#
+# Custom search folders extensions
+#
+
 @adapter_config(name='es', context=ISearchFolder, provides=IViewQuery)
 class EsSearchFolderQuery(EsViewQuery):
     """Search folder view query for Elasticsearch"""
@@ -116,9 +121,27 @@
         return params
 
 
+@adapter_config(name='exclusions', context=EsSearchFolderQuery, provides=IViewUserQuery)
+class EsSearchFolderExclusionsQuery(ContextAdapter):
+    """Search folder exclusions for Elasticsearch
+
+    This adapter is looking into request's annotations for items which should be excluded
+    from search.
+    """
+
+    @staticmethod
+    def get_user_params(request):
+        excluded_items = request.annotations.get(SEARCH_EXCLUDED_ITEMS)
+        if excluded_items:
+            yield Q('bool', must_not=Q('terms', **{'reference_id': tuple(excluded_items)}))
+
+
 @adapter_config(name='user_search', context=EsSearchFolderQuery, provides=IViewUserQuery)
 class EsSearchFolderUserQuery(ContextAdapter):
-    """Search folder user query for Elasticsearch"""
+    """Search folder user query for Elasticsearch
+
+    This adapter is looking for any fulltext search entered by user
+    """
 
     @staticmethod
     def get_user_params(request):
--- a/src/pyams_content_es/shared/view/reference.py	Fri Dec 14 11:12:42 2018 +0100
+++ b/src/pyams_content_es/shared/view/reference.py	Fri Dec 14 13:33:30 2018 +0100
@@ -32,7 +32,7 @@
         # check view settings
         if settings.exclude_context:
             intids = get_utility(IIntIds)
-            yield Q('bool', must_not=Q('term', internal_id=intids.register(context)))
+            yield Q('bool', must_not=Q('term', **{'internal_id': intids.register(context)}))
         # check view references mode
         if settings.references_mode == ONLY_REFERENCE_MODE:
             yield Q('terms', **{'reference_id': settings.references}), False