Skip to content

Commit

Permalink
Fixes count cache logic according to 7.x API
Browse files Browse the repository at this point in the history
  • Loading branch information
0bsearch authored and honzakral committed Jul 27, 2019
1 parent fa9f7d9 commit 2b63582
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions elasticsearch_dsl/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,8 @@ def count(self):
Return the number of hits matching the query and filters. Note that
only the actual number is returned.
"""
if hasattr(self, '_response'):
return self._response.hits.total
if hasattr(self, '_response') and self._response.hits.total.relation == 'eq':
return self._response.hits.total.value

es = connections.get_connection(self._using)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"mock",
"pytest>=3.0.0",
"pytest-cov",
"pytest-mock",
"pytz",
"coverage<5.0.0"
]
Expand Down
15 changes: 15 additions & 0 deletions test_elasticsearch_dsl/test_integration/test_count.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
from elasticsearch_dsl.search import Search, Q


def test_count_all(data_client):
s = Search(using=data_client).index('git')
assert 53 == s.count()


def test_count_prefetch(data_client, mocker):
mocker.spy(data_client, 'count')

search = Search(using=data_client).index('git')
search.execute()
assert search.count() == 53
assert data_client.count.call_count == 0

search._response.hits.total.relation = 'gte'
assert search.count() == 53
assert data_client.count.call_count == 1


def test_count_filter(data_client):
s = Search(using=data_client).index('git').filter(~Q('exists', field='parent_shas'))
# initial commit + repo document
Expand Down
8 changes: 1 addition & 7 deletions test_elasticsearch_dsl/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ def test_iter_iterates_over_hits():

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

def test_count_uses_cache():
s = search.Search()
s._response = utils.AttrDict({'hits': {'total': 42}})

assert 42 == s.count()

def test_cache_isnt_cloned():
s = search.Search()
s._response = object()
Expand Down Expand Up @@ -544,4 +538,4 @@ def test_update_from_dict():
'id',
'name'
]
} == s.to_dict()
} == s.to_dict()

0 comments on commit 2b63582

Please sign in to comment.