Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubes
elasticsearch
Commits
f84df2d2d35d
Commit
dbb919a2
authored
Oct 17, 2019
by
Guillaume Vandevelde
Browse files
[es7] Adapt to es7 new total hits mechanic
parent
e275070e9e2f
Changes
2
Hide whitespace changes
Inline
Side-by-side
cubicweb_elasticsearch/views.py
View file @
f84df2d2
...
...
@@ -47,6 +47,7 @@ class CWFacetedSearch(FacetedSearch):
doc_types
=
None
,
index
=
None
,
form
=
None
,
track_total_hits
=
True
,
**
kwargs
):
if
index
:
self
.
index
=
index
...
...
@@ -56,9 +57,16 @@ class CWFacetedSearch(FacetedSearch):
self
.
form
=
form
else
:
self
.
form
=
{}
# Count all the hits by default
self
.
track_total_hits
=
track_total_hits
self
.
extra_kwargs
=
kwargs
super
(
CWFacetedSearch
,
self
).
__init__
(
query
,
filters
)
def
search
(
self
):
# override methods to add custom pieces
s
=
super
(
CWFacetedSearch
,
self
).
search
()
return
s
.
extra
(
track_total_hits
=
self
.
track_total_hits
)
def
query
(
self
,
search
,
query
):
if
query
:
common
=
'debug-es-disable-common'
not
in
self
.
form
# default True
...
...
@@ -144,10 +152,10 @@ class ElasticSearchView(StartupView):
query_string
=
self
.
_cw
.
form
.
get
(
'q'
,
self
.
_cw
.
form
.
get
(
'search'
,
''
))
self
.
w
(
u
'<h1>%s</h1>'
%
self
.
_cw
.
_
(
self
.
title
))
response
=
self
.
do_search
(
query_string
)
if
response
.
hits
.
total
:
if
response
.
hits
.
total
.
value
:
self
.
w
(
u
'<h2>Resultats pour : <em>%s</em></h2>'
%
xml_escape
(
query_string
))
self
.
w
(
u
'Resultats: %s'
%
response
.
hits
.
total
)
self
.
w
(
u
'Resultats: %s'
%
response
.
hits
.
total
.
value
)
if
hasattr
(
response
,
'facets'
):
self
.
display_facets
(
response
)
self
.
display_results
(
response
)
...
...
@@ -217,7 +225,7 @@ class ElasticSearchView(StartupView):
'''
Pagination HTML generation
'''
if
response
.
hits
.
total
<=
10
:
if
response
.
hits
.
total
.
value
<=
10
:
return
url_params
=
self
.
_cw
.
form
.
copy
()
with
t
.
ul
(
self
.
w
,
klass
=
"pagination"
)
as
ul
:
...
...
@@ -228,7 +236,7 @@ class ElasticSearchView(StartupView):
href
=
xml_escape
(
self
.
_cw
.
build_url
(
**
url_params
)))))
else
:
ul
(
t
.
li
(
t
.
a
(
self
.
previous_link
)))
total_pages
=
min
((
response
.
hits
.
total
//
10
)
+
2
,
1000
)
total_pages
=
min
((
response
.
hits
.
total
.
value
//
10
)
+
2
,
1000
)
page_padding
=
3
if
current_page
>
page_padding
:
...
...
test/test_elastic_search.py
View file @
f84df2d2
...
...
@@ -140,7 +140,10 @@ def mock_execute(count):
return
FacetedResponse
(
search
,
{
'hits'
:
{
'hits'
:
[
_result
(
i
)
for
i
in
range
(
count
)],
'total'
:
count
,
'total'
:
{
"value"
:
count
,
"relation"
:
"eq"
},
}
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment