[es7] Adapt to es7 new total hits mechanic

authorGuillaume Vandevelde <gvandevelde@logilab.fr>
changeset8b9ee18ceccb
branchdefault
phasedraft
hiddenyes
parent revision#60c7c2fb71f5 [es7] Prepare support for elasticsearch 7.0.0
child revision#845565b8c1c0 [es7] Remove standard filter
files modified by this revision
cubicweb_elasticsearch/views.py
test/test_elastic_search.py
# HG changeset patch
# User Guillaume Vandevelde <gvandevelde@logilab.fr>
# Date 1571315433 -7200
# Thu Oct 17 14:30:33 2019 +0200
# Node ID 8b9ee18ceccb3b508346ed402ebe450db0d08543
# Parent 60c7c2fb71f5f1c5893b1a8eddbf409f5cd8ff5c
[es7] Adapt to es7 new total hits mechanic

diff --git a/cubicweb_elasticsearch/views.py b/cubicweb_elasticsearch/views.py
@@ -45,22 +45,30 @@
1      def __init__(self, query=None,
2                   filters={},
3                   doc_types=None,
4                   index=None,
5                   form=None,
6 +                 track_total_hits=True,
7                   **kwargs):
8          if index:
9              self.index = index
10          if doc_types:
11              self.doc_types = doc_types
12          if form:
13              self.form = form
14          else:
15              self.form = {}
16 +        # Count all the hits by default
17 +        self.track_total_hits = track_total_hits
18          self.extra_kwargs = kwargs
19          super(CWFacetedSearch, self).__init__(query, filters)
20 
21 +    def search(self):
22 +        # override methods to add custom pieces
23 +        s = super(CWFacetedSearch, self).search()
24 +        return s.extra(track_total_hits=self.track_total_hits)
25 +
26      def query(self, search, query):
27          if query:
28              common = 'debug-es-disable-common' not in self.form  # default True
29              phrase = 'debug-es-disable-phrase' not in self.form  # default True
30              fuzzy = 'fuzzy' in self.form  # default False
@@ -142,14 +150,14 @@
31          # TODO if no ES configuration, redirect or display warning
32          self.render_search_comp()
33          query_string = self._cw.form.get('q', self._cw.form.get('search', ''))
34          self.w(u'<h1>%s</h1>' % self._cw._(self.title))
35          response = self.do_search(query_string)
36 -        if response.hits.total:
37 +        if response.hits.total.value:
38              self.w(u'<h2>Resultats pour : <em>%s</em></h2>' %
39                     xml_escape(query_string))
40 -            self.w(u'Resultats: %s' % response.hits.total)
41 +            self.w(u'Resultats: %s' % response.hits.total.value)
42              if hasattr(response, 'facets'):
43                  self.display_facets(response)
44          self.display_results(response)
45 
46      def customize_search(self, query_string, facet_selections,
@@ -215,22 +223,22 @@
47 
48      def pagination(self, response):
49          '''
50          Pagination HTML generation
51          '''
52 -        if response.hits.total <= 10:
53 +        if response.hits.total.value <= 10:
54              return
55          url_params = self._cw.form.copy()
56          with t.ul(self.w, klass="pagination") as ul:
57              current_page = int(url_params.get('page', 1))
58              url_params['page'] = current_page - 1
59              if current_page - 1 >= 1:
60                  ul(t.li(t.a(self.previous_link,
61                              href=xml_escape(self._cw.build_url(**url_params)))))
62              else:
63                  ul(t.li(t.a(self.previous_link)))
64 -            total_pages = min((response.hits.total // 10) + 2, 1000)
65 +            total_pages = min((response.hits.total.value // 10) + 2, 1000)
66              page_padding = 3
67 
68              if current_page > page_padding:
69                  for page in range(1,
70                                    min(page_padding + 1,
diff --git a/test/test_elastic_search.py b/test/test_elastic_search.py
@@ -138,11 +138,14 @@
71 
72      search = Search(doc_type='_doc', index='unittest_index_name')
73      return FacetedResponse(search, {
74          'hits': {
75              'hits': [_result(i) for i in range(count)],
76 -            'total': count,
77 +            'total': {
78 +                "value": count,
79 +                "relation": "eq"
80 +            },
81          }
82      })
83 
84 
85  def mock_cnx(*args, **kwargs):