User iterators to get query params
authorThierry Florac <thierry.florac@onf.fr>
Mon, 26 Nov 2018 11:08:36 +0100
changeset 110 d1122192d6f8
parent 109 b9885a695f88
child 111 c0ce2972d333
User iterators to get query params
src/pyams_content_es/shared/view/__init__.py
--- a/src/pyams_content_es/shared/view/__init__.py	Mon Nov 26 10:16:04 2018 +0100
+++ b/src/pyams_content_es/shared/view/__init__.py	Mon Nov 26 11:08:36 2018 +0100
@@ -14,14 +14,15 @@
 
 from datetime import datetime
 
-from elasticsearch_dsl import Search, Q
+from elasticsearch_dsl import Q, Search
 from pyramid.threadlocal import get_current_registry
 from pyramid_es import get_client
 
 from pyams_catalog.query import CatalogResultSet
-from pyams_content.shared.view.interfaces import IWfView, IViewQuery, IViewQueryEsParamsExtension, \
-    IViewQueryFilterExtension
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_content.features.search import ISearchFolder
+from pyams_content.shared.view.interfaces import IViewQuery, IViewQueryEsParamsExtension, IViewQueryFilterExtension, \
+    IViewUserQuery, IWfView
+from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.list import unique_iter
 from pyams_utils.request import check_request
 from pyams_utils.timezone import tztime
@@ -32,7 +33,7 @@
 class EsViewQuery(ContextAdapter):
     """View query for Elasticsearch"""
 
-    def get_es_params(self, context):
+    def get_params(self, context):
         view = self.context
         registry = get_current_registry()
         # check publication dates
@@ -47,11 +48,11 @@
         do_search = True
         for name, adapter in sorted(registry.getAdapters((view,), IViewQueryEsParamsExtension),
                                     key=lambda x: x[1].weight):
-            new_params = adapter.get_es_params(context)
-            if isinstance(new_params, tuple):
-                new_params, do_search = new_params
-            if new_params:
-                params &= new_params
+            for new_params in adapter.get_es_params(context):
+                if isinstance(new_params, tuple):
+                    new_params, do_search = new_params
+                if new_params:
+                    params &= new_params
             if not do_search:
                 break
         # activate search
@@ -72,7 +73,7 @@
         request = check_request()
         registry = request.registry
         client = get_client(request)
-        params = self.get_es_params(context)
+        params = self.get_params(context)
         search = Search(using=client.es, index=client.index) \
             .query(params) \
             .sort({
@@ -89,3 +90,29 @@
                                     key=lambda x: x[1].weight):
             items = adapter.filter(context, items)
         return unique_iter(items)
+
+
+@adapter_config(name='es', context=ISearchFolder, provides=IViewQuery)
+class EsSearchFolderQuery(EsViewQuery):
+    """Search folder view query for Elasticsearch"""
+
+    def get_params(self, context):
+        params = super(EsSearchFolderQuery, self).get_params(context)
+        request = check_request()
+        registry = request.registry
+        for name, adapter in registry.getAdapters((self,), IViewUserQuery):
+            for user_param in adapter.get_user_params(request):
+                params &= user_param
+        return params
+
+
+@adapter_config(name='user_search', context=EsSearchFolderQuery, provides=IViewUserQuery)
+class EsSearchFolderUserQuery(ContextAdapter):
+    """Search folder user query for Elasticsearch"""
+
+    @staticmethod
+    def get_user_params(request):
+        params = request.params
+        fulltext = params.get('user_search')
+        if fulltext is not None:
+            yield Q('simple_query_string', **{'query': fulltext})