Skip to content

Commit

Permalink
Merge branch 'master' into connection_interface_usage
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed Jul 27, 2019
2 parents 85fb761 + 2b63582 commit ec9f9c0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
4 changes: 2 additions & 2 deletions elasticsearch_dsl/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import date, datetime

from dateutil import parser, tz
from six import string_types, iteritems
from six import string_types, iteritems, integer_types
from six.moves import map

from .query import Q
Expand Down Expand Up @@ -251,7 +251,7 @@ def _deserialize(self, data):
return data
if isinstance(data, date):
return data
if isinstance(data, int):
if isinstance(data, integer_types):
# Divide by a float to preserve milliseconds on the datetime.
return datetime.utcfromtimestamp(data / 1000.0)

Expand Down
7 changes: 4 additions & 3 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from .update_by_query import UpdateByQuery
from .utils import merge

DEFAULT_DOC_TYPE = 'doc'

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

def save(self, using=None):

es = get_connection(using or self._index._using)
es.indices.put_template(name=self._template_name, body=self.to_dict())
return es.indices.put_template(name=self._template_name, body=self.to_dict())


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

def is_closed(self, using=None):
state = self._get_connection(using).cluster.state(index=self._name, metric='metadata')
Expand Down
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 = 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
22 changes: 18 additions & 4 deletions test_elasticsearch_dsl/test_index.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from elasticsearch_dsl import Document, Index, Text, Date, analyzer, Mapping, \
exceptions

from random import choice
import string
from random import choice

from pytest import raises

from elasticsearch_dsl import Date, Document, Index, IndexTemplate, Text, analyzer


class Post(Document):
title = Text()
published_from = Date()


def test_multiple_doc_types_will_combine_mappings():
class User(Document):
username = Text()
Expand All @@ -27,12 +28,14 @@ class User(Document):
}
} == i.to_dict()


def test_search_is_limited_to_index_name():
i = Index('my-index')
s = i.search()

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


def test_cloned_index_has_copied_settings_and_using():
client = object()
i = Index('my-index', using=client)
Expand All @@ -45,6 +48,7 @@ def test_cloned_index_has_copied_settings_and_using():
assert i._settings == i2._settings
assert i._settings is not i2._settings


def test_cloned_index_has_analysis_attribute():
"""
Regression test for Issue #582 in which `Index.clone()` was not copying
Expand Down Expand Up @@ -75,6 +79,7 @@ def test_settings_are_saved():
}
} == i.to_dict()


def test_registered_doc_type_included_in_to_dict():
i = Index('i', using='alias')
i.document(Post)
Expand All @@ -88,6 +93,7 @@ def test_registered_doc_type_included_in_to_dict():
}
} == i.to_dict()


def test_registered_doc_type_included_in_search():
i = Index('i', using='alias')
i.document(Post)
Expand Down Expand Up @@ -135,13 +141,15 @@ def test_analyzers_returned_from_to_dict():

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


def test_conflicting_analyzer_raises_error():
i = Index('i')
i.analyzer('my_analyzer', tokenizer='whitespace', filter=['lowercase', 'stop'])

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


def test_index_template_can_have_order():
i = Index('i-*')
it = i.as_template('i', order=2)
Expand All @@ -150,3 +158,9 @@ def test_index_template_can_have_order():
"index_patterns": ["i-*"],
"order": 2
} == it.to_dict()


def test_index_template_save_result(mock_client):
it = IndexTemplate('test-template', 'test-*')

assert it.save(using='mock') == mock_client.indices.put_template()
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 ec9f9c0

Please sign in to comment.