From c47e35ad73d25a617d28a5917d68527120c98821 Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 02:58:07 +0600 Subject: [PATCH 1/7] [Fix #1091] Index.document() does not set index on Document class --- elasticsearch_dsl/document.py | 11 +++++++---- elasticsearch_dsl/index.py | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index 1c2b7cdc7..07595ebee 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) + if index: + new_cls._index = index + 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('*') + + # return None as there are no index_opts + return None i = Index( getattr(opts, 'name', '*'), diff --git a/elasticsearch_dsl/index.py b/elasticsearch_dsl/index.py index bfb123582..56b2055e5 100644 --- a/elasticsearch_dsl/index.py +++ b/elasticsearch_dsl/index.py @@ -159,6 +159,11 @@ 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 does not have any index, set this index as document index + if document._index is None: + document._index = self + return document doc_type = document From 778d7f586e6b48f35409337f37128ba1f29295d9 Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 03:23:35 +0600 Subject: [PATCH 2/7] more fixup --- elasticsearch_dsl/document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index 07595ebee..43ce19ed2 100644 --- a/elasticsearch_dsl/document.py +++ b/elasticsearch_dsl/document.py @@ -38,8 +38,8 @@ def __new__(cls, name, bases, attrs): if cls._document_initialized: index_opts = attrs.pop('Index', None) index = cls.construct_index(index_opts, bases) + new_cls._index = index if index: - new_cls._index = index index.document(new_cls) cls._document_initialized = True return new_cls From 43412cf9e5856d4c2356ff30c0bea5cf9b9e037b Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 03:47:23 +0600 Subject: [PATCH 3/7] adding index in document --- test_elasticsearch_dsl/test_document.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test_elasticsearch_dsl/test_document.py b/test_elasticsearch_dsl/test_document.py index 515e24ea6..861d87cfe 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() From d0a0dee6c93e324008b5bca6b9b355287d6a4a91 Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 03:58:53 +0600 Subject: [PATCH 4/7] fixing more tests --- test_elasticsearch_dsl/test_document.py | 13 +++++++++++-- .../test_integration/test_faceted_search.py | 7 +++++++ test_elasticsearch_dsl/test_result.py | 4 ++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/test_elasticsearch_dsl/test_document.py b/test_elasticsearch_dsl/test_document.py index 861d87cfe..f413ca496 100644 --- a/test_elasticsearch_dsl/test_document.py +++ b/test_elasticsearch_dsl/test_document.py @@ -18,6 +18,9 @@ class MyDoc(document.Document): created_at = field.Date() inner = field.Object(MyInner) + class Index: + name = 'my-doc' + class MySubDoc(MyDoc): name = field.Keyword() @@ -118,6 +121,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'}}}} @@ -126,7 +133,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): @@ -597,7 +605,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) From ca2ad8ecfa422d4d8b32bb72c747b947718a3ea8 Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 04:04:30 +0600 Subject: [PATCH 5/7] fixing tests --- test_elasticsearch_dsl/test_document.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test_elasticsearch_dsl/test_document.py b/test_elasticsearch_dsl/test_document.py index f413ca496..4aad5bb22 100644 --- a/test_elasticsearch_dsl/test_document.py +++ b/test_elasticsearch_dsl/test_document.py @@ -18,9 +18,6 @@ class MyDoc(document.Document): created_at = field.Date() inner = field.Object(MyInner) - class Index: - name = 'my-doc' - class MySubDoc(MyDoc): name = field.Keyword() @@ -439,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() From 55ed31a19c55d1248c729f483f9c6e57a115ac76 Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 05:49:38 +0600 Subject: [PATCH 6/7] set index name to None --- elasticsearch_dsl/document.py | 4 ++-- elasticsearch_dsl/index.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index 43ce19ed2..ab7630885 100644 --- a/elasticsearch_dsl/document.py +++ b/elasticsearch_dsl/document.py @@ -51,8 +51,8 @@ def construct_index(cls, opts, bases): if hasattr(b, '_index'): return b._index - # return None as there are no index_opts - return None + # 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 56b2055e5..4e47dd4d4 100644 --- a/elasticsearch_dsl/index.py +++ b/elasticsearch_dsl/index.py @@ -160,8 +160,10 @@ class Post(Document): 'trying to assign %s.' % (doc_type, name)) self._doc_types.append(document) - # If the document does not have any index, set this index as document index - if document._index is None: + # 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 From 7d71f61962f4e362b27c5dbe0e85d19ea09e6562 Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Mon, 31 Dec 2018 06:57:49 +0600 Subject: [PATCH 7/7] more update --- elasticsearch_dsl/document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index ab7630885..fa6868954 100644 --- a/elasticsearch_dsl/document.py +++ b/elasticsearch_dsl/document.py @@ -39,7 +39,7 @@ def __new__(cls, name, bases, attrs): index_opts = attrs.pop('Index', None) index = cls.construct_index(index_opts, bases) new_cls._index = index - if index: + if index._name: index.document(new_cls) cls._document_initialized = True return new_cls