Update test query for Elasticsearch-DSL package
authorThierry Florac <thierry.florac@onf.fr>
Wed, 14 Jun 2017 15:31:07 +0200
changeset 8 dc4ca82098cc
parent 7 01016144130c
child 9 3ad78ffa0f7f
Update test query for Elasticsearch-DSL package
src/pyams_content_es/zmi/test.py
--- a/src/pyams_content_es/zmi/test.py	Tue Jun 06 17:22:31 2017 +0200
+++ b/src/pyams_content_es/zmi/test.py	Wed Jun 14 15:31:07 2017 +0200
@@ -9,7 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-
+from onf_website.shared.news import WfNewsEvent
 
 __docformat__ = 'restructuredtext'
 
@@ -21,6 +21,7 @@
 from zope.intid.interfaces import IIntIds
 
 # import packages
+from elasticsearch_dsl import Search, A
 from pyams_utils.registry import get_utility
 from pyramid.response import Response
 from pyramid.view import view_config
@@ -38,18 +39,23 @@
         #               .size(100)
         # return query.execute(fields=['title.fr']).raw
 
-        query = client.query() \
-                      .filter_term('_type', 'WfNewsEvent') \
-                      .filter_terms('workflow.status', ['published', 'retiring']) \
-                      .add_term_aggregate('status', 'workflow.status') \
-                      .add_date_aggregate('wf_date', 'workflow.date') \
-                      .size(100)
+        # query = client.query() \
+        #               .filter_term('_type', 'WfNewsEvent') \
+        #               .filter_terms('workflow.status', ['published', 'retiring']) \
+        #               .add_term_aggregate('status', 'workflow.status') \
+        #               .add_date_aggregate('wf_date', 'workflow.date') \
+        #               .size(100)
+
+        query = Search(using=client.es).query('term', **{'_type': WfNewsEvent.__name__}) \
+                                       .query('terms', **{'workflow.status': ['published', 'retiring']}) \
+                                       .source(['internal_id'])
+        agg = A('terms', field='workflow.status')
+        query.aggs.bucket('status', agg)
         intids = get_utility(IIntIds)
-        for result in query.execute(fields=['internal_id']):
+        for result in query.execute():
             if not result.internal_id:
                 continue
-            target = intids.queryObject(result.internal_id[0])
+            target = intids.queryObject(result.internal_id)
             yield II18n(target).query_attribute('title').encode() + b'\n'
-        # return pformat(query.execute(fields=['title.fr', 'workflow.status', 'internal_id']).raw)
 
     return Response(app_iter=get_response())