Added view query adapter to use Elasticsearch index
authorThierry Florac <thierry.florac@onf.fr>
Tue, 27 Jun 2017 13:33:54 +0200
changeset 10 78a475f0423e
parent 9 3ad78ffa0f7f
child 11 2dfb793b4e0f
Added view query adapter to use Elasticsearch index
src/pyams_content_es/component/view.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content_es/component/view.py	Tue Jun 27 13:33:54 2017 +0200
@@ -0,0 +1,69 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.shared.view.interfaces import IWfView, IViewQuery, IViewQueryEsParamsExtension, \
+    IViewQueryFilterExtension
+from pyams_workflow.interfaces import IWorkflow
+
+# import packages
+from elasticsearch_dsl import Search, Q
+from pyams_catalog.query import CatalogResultSet
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.list import unique
+from pyams_utils.request import check_request
+from pyramid.threadlocal import get_current_registry
+from pyramid_es import get_client
+
+
+@adapter_config(name='es', context=IWfView, provides=IViewQuery)
+class EsViewQuery(ContextAdapter):
+    """View query for Elasticsearch"""
+
+    def get_es_params(self, context):
+        view = self.context
+        registry = get_current_registry()
+        params = Q('terms', **{'content_type': view.selected_content_types})
+        wf_params = []
+        for workflow in registry.getAllUtilitiesRegisteredFor(IWorkflow):
+            wf_params.extend(workflow.published_states)
+        params &= Q('terms', **{'workflow.status': wf_params})
+        for name, adapter in sorted(registry.getAdapters((view,), IViewQueryEsParamsExtension),
+                                    key=lambda x: x[1].weight):
+            new_params = adapter.get_es_params(context)
+            if new_params:
+                params &= new_params
+        return params
+
+    def get_results(self, context, limit=None):
+        view = self.context
+        request = check_request()
+        registry = request.registry
+        client = get_client(request)
+        params = self.get_es_params(context)
+        search = Search(using=client.es, index=client.index) \
+            .query(params) \
+            .sort('{0}workflow.{1}'.format('-' if view.reversed_order else '',
+                                           view.order_by)) \
+            .source(['internal_id'])
+        if limit:
+            search = search[:limit]
+        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(items)