Skip to content

Commit 2b63582

Browse files
0bsearchhonzakral
authored andcommitted
Fixes count cache logic according to 7.x API
1 parent fa9f7d9 commit 2b63582

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

elasticsearch_dsl/search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,8 @@ def count(self):
666666
Return the number of hits matching the query and filters. Note that
667667
only the actual number is returned.
668668
"""
669-
if hasattr(self, '_response'):
670-
return self._response.hits.total
669+
if hasattr(self, '_response') and self._response.hits.total.relation == 'eq':
670+
return self._response.hits.total.value
671671

672672
es = connections.get_connection(self._using)
673673

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"mock",
2323
"pytest>=3.0.0",
2424
"pytest-cov",
25+
"pytest-mock",
2526
"pytz",
2627
"coverage<5.0.0"
2728
]

test_elasticsearch_dsl/test_integration/test_count.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
from elasticsearch_dsl.search import Search, Q
22

3+
34
def test_count_all(data_client):
45
s = Search(using=data_client).index('git')
56
assert 53 == s.count()
67

8+
9+
def test_count_prefetch(data_client, mocker):
10+
mocker.spy(data_client, 'count')
11+
12+
search = Search(using=data_client).index('git')
13+
search.execute()
14+
assert search.count() == 53
15+
assert data_client.count.call_count == 0
16+
17+
search._response.hits.total.relation = 'gte'
18+
assert search.count() == 53
19+
assert data_client.count.call_count == 1
20+
21+
722
def test_count_filter(data_client):
823
s = Search(using=data_client).index('git').filter(~Q('exists', field='parent_shas'))
924
# initial commit + repo document

test_elasticsearch_dsl/test_search.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ def test_iter_iterates_over_hits():
3535

3636
assert [1, 2, 3] == list(s)
3737

38-
def test_count_uses_cache():
39-
s = search.Search()
40-
s._response = utils.AttrDict({'hits': {'total': 42}})
41-
42-
assert 42 == s.count()
43-
4438
def test_cache_isnt_cloned():
4539
s = search.Search()
4640
s._response = object()
@@ -544,4 +538,4 @@ def test_update_from_dict():
544538
'id',
545539
'name'
546540
]
547-
} == s.to_dict()
541+
} == s.to_dict()

0 commit comments

Comments
 (0)