Updated views sort order so that imposed references are always in first positions
authorThierry Florac <thierry.florac@onf.fr>
Thu, 03 Jan 2019 17:48:02 +0100
changeset 128 8fbf305a5e25
parent 127 e5cc001c6476
child 129 eb2875558811
Updated views sort order so that imposed references are always in first positions
src/pyams_content_es/shared/view/__init__.py
--- a/src/pyams_content_es/shared/view/__init__.py	Sun Dec 30 18:08:28 2018 +0100
+++ b/src/pyams_content_es/shared/view/__init__.py	Thu Jan 03 17:48:02 2019 +0100
@@ -12,6 +12,7 @@
 
 __docformat__ = 'restructuredtext'
 
+from collections import OrderedDict
 from datetime import datetime
 
 from elasticsearch_dsl import Q, Search
@@ -20,8 +21,8 @@
 
 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, RELEVANCE_ORDER
+from pyams_content.shared.view.interfaces import IViewInternalReferencesSettings, IViewQuery, \
+    IViewQueryEsParamsExtension, IViewQueryFilterExtension, IViewUserQuery, IWfView, RELEVANCE_ORDER
 from pyams_content.shared.view.portlet import SEARCH_EXCLUDED_ITEMS
 from pyams_sequence.interfaces import ISequentialIntIds
 from pyams_utils.adapter import ContextAdapter, adapter_config
@@ -82,19 +83,43 @@
         search = Search(using=client.es, index=client.index) \
             .query(params) \
             .source(['internal_id'])
+        # Define sort order
+        sort_values = []
+        settings = IViewInternalReferencesSettings(self.context)
+        if settings.references:
+            sort_values.append({
+                '_script': {
+                    'type': 'number',
+                    'script': {
+                        'lang': 'painless',
+                        'source': """
+                            if (params.scores.containsKey(doc['reference_id'].value)) { 
+                                return params.scores[doc['reference_id'].value];
+                            }
+                            return 999;
+                        """,
+                        'params': {
+                            'scores': OrderedDict(((ref, order) for order, ref in enumerate(settings.references)))
+                        }
+                    }
+                }
+            })
         if (not sort_index) or (sort_index == RELEVANCE_ORDER):
-            search = search.sort({
+            sort_values.append({
                 '_score': {
                     'order': 'desc'
                 }
             })
         else:
-            search = search.sort({
+            sort_values.append({
                 'workflow.{0}'.format(sort_index): {
                     'order': 'desc' if reverse else 'asc',
                     'unmapped_type': 'date'
                 }
             })
+        if sort_values:
+            search = search.sort(*sort_values)
+        # Define limits
         if limit:
             search = search[:limit]
         else: