Skip to content

[Fix #1091] Index.document() does not set index on Document class #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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', '*'),
Expand Down
7 changes: 7 additions & 0 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 24 additions & 3 deletions test_elasticsearch_dsl/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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'}}}}
Expand All @@ -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):
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions test_elasticsearch_dsl/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down