Skip to content

Commit ec9f9c0

Browse files
authored
Merge branch 'master' into connection_interface_usage
2 parents 85fb761 + 2b63582 commit ec9f9c0

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

elasticsearch_dsl/field.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from datetime import date, datetime
1010

1111
from dateutil import parser, tz
12-
from six import string_types, iteritems
12+
from six import string_types, iteritems, integer_types
1313
from six.moves import map
1414

1515
from .query import Q
@@ -251,7 +251,7 @@ def _deserialize(self, data):
251251
return data
252252
if isinstance(data, date):
253253
return data
254-
if isinstance(data, int):
254+
if isinstance(data, integer_types):
255255
# Divide by a float to preserve milliseconds on the datetime.
256256
return datetime.utcfromtimestamp(data / 1000.0)
257257

elasticsearch_dsl/index.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from .update_by_query import UpdateByQuery
77
from .utils import merge
88

9-
DEFAULT_DOC_TYPE = 'doc'
109

1110
class IndexTemplate(object):
1211
def __init__(self, name, template, index=None, order=None, **kwargs):
@@ -32,8 +31,10 @@ def to_dict(self):
3231
return d
3332

3433
def save(self, using=None):
34+
3535
es = get_connection(using or self._index._using)
36-
es.indices.put_template(name=self._template_name, body=self.to_dict())
36+
return es.indices.put_template(name=self._template_name, body=self.to_dict())
37+
3738

3839
class Index(object):
3940
def __init__(self, name, using='default'):
@@ -255,7 +256,7 @@ def create(self, using=None, **kwargs):
255256
Any additional keyword arguments will be passed to
256257
``Elasticsearch.indices.create`` unchanged.
257258
"""
258-
self._get_connection(using).indices.create(index=self._name, body=self.to_dict(), **kwargs)
259+
return self._get_connection(using).indices.create(index=self._name, body=self.to_dict(), **kwargs)
259260

260261
def is_closed(self, using=None):
261262
state = self._get_connection(using).cluster.state(index=self._name, metric='metadata')

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 = 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_index.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from elasticsearch_dsl import Document, Index, Text, Date, analyzer, Mapping, \
2-
exceptions
3-
4-
from random import choice
51
import string
2+
from random import choice
63

74
from pytest import raises
85

6+
from elasticsearch_dsl import Date, Document, Index, IndexTemplate, Text, analyzer
7+
8+
99
class Post(Document):
1010
title = Text()
1111
published_from = Date()
1212

13+
1314
def test_multiple_doc_types_will_combine_mappings():
1415
class User(Document):
1516
username = Text()
@@ -27,12 +28,14 @@ class User(Document):
2728
}
2829
} == i.to_dict()
2930

31+
3032
def test_search_is_limited_to_index_name():
3133
i = Index('my-index')
3234
s = i.search()
3335

3436
assert s._index == ['my-index']
3537

38+
3639
def test_cloned_index_has_copied_settings_and_using():
3740
client = object()
3841
i = Index('my-index', using=client)
@@ -45,6 +48,7 @@ def test_cloned_index_has_copied_settings_and_using():
4548
assert i._settings == i2._settings
4649
assert i._settings is not i2._settings
4750

51+
4852
def test_cloned_index_has_analysis_attribute():
4953
"""
5054
Regression test for Issue #582 in which `Index.clone()` was not copying
@@ -75,6 +79,7 @@ def test_settings_are_saved():
7579
}
7680
} == i.to_dict()
7781

82+
7883
def test_registered_doc_type_included_in_to_dict():
7984
i = Index('i', using='alias')
8085
i.document(Post)
@@ -88,6 +93,7 @@ def test_registered_doc_type_included_in_to_dict():
8893
}
8994
} == i.to_dict()
9095

96+
9197
def test_registered_doc_type_included_in_search():
9298
i = Index('i', using='alias')
9399
i.document(Post)
@@ -135,13 +141,15 @@ def test_analyzers_returned_from_to_dict():
135141

136142
assert index.to_dict()["settings"]["analysis"]["analyzer"][random_analyzer_name] == {"filter": ["standard"], "type": "custom", "tokenizer": "standard"}
137143

144+
138145
def test_conflicting_analyzer_raises_error():
139146
i = Index('i')
140147
i.analyzer('my_analyzer', tokenizer='whitespace', filter=['lowercase', 'stop'])
141148

142149
with raises(ValueError):
143150
i.analyzer('my_analyzer', tokenizer='keyword', filter=['lowercase', 'stop'])
144151

152+
145153
def test_index_template_can_have_order():
146154
i = Index('i-*')
147155
it = i.as_template('i', order=2)
@@ -150,3 +158,9 @@ def test_index_template_can_have_order():
150158
"index_patterns": ["i-*"],
151159
"order": 2
152160
} == it.to_dict()
161+
162+
163+
def test_index_template_save_result(mock_client):
164+
it = IndexTemplate('test-template', 'test-*')
165+
166+
assert it.save(using='mock') == mock_client.indices.put_template()

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)