diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index 1c2b7cdc7..fa6868954 100644 --- a/elasticsearch_dsl/document.py +++ b/elasticsearch_dsl/document.py @@ -37,8 +37,10 @@ def __new__(cls, name, bases, attrs): new_cls = super(IndexMeta, cls).__new__(cls, name, bases, attrs) if cls._document_initialized: index_opts = attrs.pop('Index', None) - new_cls._index = cls.construct_index(index_opts, bases) - new_cls._index.document(new_cls) + index = cls.construct_index(index_opts, bases) + new_cls._index = index + if index._name: + index.document(new_cls) cls._document_initialized = True return new_cls @@ -48,8 +50,9 @@ def construct_index(cls, opts, bases): for b in bases: if hasattr(b, '_index'): return b._index - # create an all-matching index pattern - return Index('*') + + # Set None as Index name so it will set _all while making the query + return Index(name=None) i = Index( getattr(opts, 'name', '*'), diff --git a/elasticsearch_dsl/index.py b/elasticsearch_dsl/index.py index bfb123582..4e47dd4d4 100644 --- a/elasticsearch_dsl/index.py +++ b/elasticsearch_dsl/index.py @@ -159,6 +159,13 @@ class Post(Document): 'Index object cannot have multiple types, %s already set, ' 'trying to assign %s.' % (doc_type, name)) self._doc_types.append(document) + + # If the document index does not have any name, that means the user + # did not set any index already to the document. + # So set this index as document index + if document._index._name is None: + document._index = self + return document doc_type = document diff --git a/test_elasticsearch_dsl/test_document.py b/test_elasticsearch_dsl/test_document.py index 515e24ea6..4aad5bb22 100644 --- a/test_elasticsearch_dsl/test_document.py +++ b/test_elasticsearch_dsl/test_document.py @@ -37,6 +37,9 @@ class Comment(document.InnerDoc): class DocWithNested(document.Document): comments = field.Nested(Comment) + class Index: + name = 'test-doc-with-nested' + class SimpleCommit(document.Document): files = field.Text(multi=True) @@ -59,15 +62,27 @@ def _deserialize(self, data): class SecretDoc(document.Document): title = SecretField(index='no') + class Index: + name = 'test-secret-doc' + class NestedSecret(document.Document): secrets = field.Nested(SecretDoc) + class Index: + name = 'test-nested-secret' + class OptionalObjectWithRequiredField(document.Document): comments = field.Nested(properties={'title': field.Keyword(required=True)}) + class Index: + name = 'test-required' + class Host(document.Document): ip = field.Ip() + class Index: + name = 'test-host' + def test_range_serializes_properly(): class D(document.Document): lr = field.LongRange() @@ -103,6 +118,10 @@ class D(document.Document): kw = field.Keyword() class Meta: doc_type = 'not-doc' + + class Index: + name = 'test-not-doc-index' + assert D._index._get_doc_type() == 'not-doc' assert D._index.to_dict() == { 'mappings': {'not-doc': {'properties': {'kw': {'type': 'keyword'}}}} @@ -111,7 +130,8 @@ class Meta: def test_document_cannot_specify_different_doc_type_if_index_defined(): # this will initiate ._index with doc_type = 'doc' class C(document.Document): - pass + class Index: + name = 'test-doc-type-c' with raises(IllegalOperation): class D(C): @@ -416,7 +436,7 @@ def test_to_dict_is_recursive_and_can_cope_with_multi_values(): } == md.to_dict() def test_to_dict_ignores_empty_collections(): - md = MyDoc(name='', address={}, count=0, valid=False, tags=[]) + md = MySubDoc(name='', address={}, count=0, valid=False, tags=[]) assert {'name': '', 'count': 0, 'valid': False} == md.to_dict() @@ -582,7 +602,8 @@ def test_from_es_respects_underscored_non_meta_fields(): } class Company(document.Document): - pass + class Index: + name = 'test-company' c = Company.from_es(doc) diff --git a/test_elasticsearch_dsl/test_integration/test_faceted_search.py b/test_elasticsearch_dsl/test_integration/test_faceted_search.py index 3ada515e1..23bbdcd0a 100644 --- a/test_elasticsearch_dsl/test_integration/test_faceted_search.py +++ b/test_elasticsearch_dsl/test_integration/test_faceted_search.py @@ -21,10 +21,17 @@ class Repos(Document): is_public = Boolean() created_at = Date() + class Index: + name = 'git' + + class Commit(Document): files = Keyword() committed_date = Date() + class Index: + name = 'git' + class RepoSearch(FacetedSearch): index = 'git' doc_types = [Repos] diff --git a/test_elasticsearch_dsl/test_result.py b/test_elasticsearch_dsl/test_result.py index e4faf8854..cd1394809 100644 --- a/test_elasticsearch_dsl/test_result.py +++ b/test_elasticsearch_dsl/test_result.py @@ -150,6 +150,10 @@ def test_bucket_response_can_be_iterated_over(agg_response): def test_bucket_keys_get_deserialized(aggs_data, aggs_search): class Commit(Document): info = Object(properties={'committed_date': Date()}) + + class Index: + name = 'test-commit' + aggs_search = aggs_search.doc_type(Commit) agg_response = response.Response(aggs_search, aggs_data)