Added content path query filter
authorThierry Florac <thierry.florac@onf.fr>
Fri, 30 Nov 2018 14:36:43 +0100
changeset 112 88ef502db9b1
parent 111 c0ce2972d333
child 113 0baf6315d838
Added content path query filter
src/pyams_content_es/shared/view/__init__.py
--- a/src/pyams_content_es/shared/view/__init__.py	Fri Nov 30 14:36:17 2018 +0100
+++ b/src/pyams_content_es/shared/view/__init__.py	Fri Nov 30 14:36:43 2018 +0100
@@ -21,7 +21,7 @@
 from pyams_catalog.query import CatalogResultSet
 from pyams_content.features.search import ISearchFolder
 from pyams_content.shared.view.interfaces import IViewQuery, IViewQueryEsParamsExtension, IViewQueryFilterExtension, \
-    IViewUserQuery, IWfView
+    IViewUserQuery, IWfView, RELEVANCE_ORDER
 from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.list import unique_iter
 from pyams_utils.request import check_request
@@ -59,6 +59,10 @@
         if do_search:
             params &= Q('bool', must=Q('range', **{'workflow.push_end_date': {'gte': now}})) | \
                       Q('bool', must_not=Q('exists', **{'field': 'workflow.push_end_date'}))
+            # check content path
+            content_path = view.get_content_path(context)
+            if content_path is not None:
+                params &= Q('term', **{'parent_ids': content_path})
             # check content types
             content_types = view.get_content_types(context)
             if content_types:
@@ -76,20 +80,23 @@
         params = self.get_params(context)
         search = Search(using=client.es, index=client.index) \
             .query(params) \
-            .sort({
+            .source(['internal_id'])
+        if sort_index != RELEVANCE_ORDER:
+            search = search.sort({
                 'workflow.{0}'.format(sort_index): {
                     'order': 'desc' if reverse else 'asc',
                     'unmapped_type': 'date'
                 }
-            }) \
-            .source(['internal_id'])
+            })
         if limit:
             search = search[:limit]
+        else:
+            search = search[:999]
         items = CatalogResultSet([result.internal_id for result in search])
         for name, adapter in sorted(registry.getAdapters((self.context,), IViewQueryFilterExtension),
                                     key=lambda x: x[1].weight):
             items = adapter.filter(context, items)
-        return unique_iter(items)
+        return unique_iter(items), search.count()
 
 
 @adapter_config(name='es', context=ISearchFolder, provides=IViewQuery)
@@ -114,5 +121,5 @@
     def get_user_params(request):
         params = request.params
         fulltext = params.get('user_search')
-        if fulltext is not None:
+        if fulltext:
             yield Q('simple_query_string', **{'query': fulltext})