I tried to use the new 6.2 release and found that the following code crashes:
from elasticsearch_dsl import Document, Mapping
class MyDoc(Document):
class Meta:
mapping = Mapping('a')
With the following trace:
/lib/python3.6/site-packages/elasticsearch_dsl/document.py in __new__(cls, name, bases, attrs)
30 new_cls = super(IndexMeta, cls).__new__(cls, name, bases, attrs)
31 new_cls._index = cls.construct_index(index_opts, bases)
---> 32 new_cls._index.document(new_cls)
33 return new_cls
34
/lib/python3.6/site-packages/elasticsearch_dsl/index.py in document(self, document)
121 raise IllegalOperation(
122 'Index object cannot have multiple types, %s already set, '
--> 123 'trying to assign %s.' % (self._mapping.doc_type, name))
124 self._doc_types.append(document)
125 # TODO: do this at save time to allow Document to be modified after
IllegalOperation: Index object cannot have multiple types, doc already set, trying to assign a.
I tried to add doc_type='a' to the Meta class, but it didn't help. As far as I have tried, there's absolutely no way to have a mapping which is not named 'doc'. This is also incompatible with existing indexes.
Thanks for the report, this is indeed a bug as we need to make sure we expose the doc_type option on class Index. I will provide a fix and release 6.2.1 asap.
Hey @HonzaKral thank you for the fix. Do you have an example of how the doc_type should be specified in 6.2.1? I am using that version but get a similar IllegalOperation error when using the provided code:
class MyDoc(Document):
class Meta:
mapping = Mapping('a')
If I use the following code block, I get a slightly different error:
class MyDoc(Document):
class Index:
doc_type = "a"
With the following trace:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File ".../lib/python3.6/site-packages/elasticsearch_dsl/document.py", line 32, in __new__
new_cls._index.document(new_cls)
File ...lib/python3.6/site-packages/elasticsearch_dsl/index.py", line 123, in document
'trying to assign %s.' % (self._mapping.doc_type, name))
elasticsearch_dsl.exceptions.IllegalOperation: Index object cannot have multiple types, a already set, trying to assign doc.
Thanks for the help!
@jason-huling I'm having the same problem. Please let me know if you find a solution.
Hey @rafaelcaricio, I don't know if this will help you, but my use case looks like the following:
response = MyDoc.search().query(q).execute()
I was able to get around the doc_type issue by not defining it in MyDoc, but instead passing it in like the following.
response = MyDoc.search().doc_type("a").query(q).execute()
Hopefully there is a better solution available that someone else can provide, but maybe this can help in the interim.
@rafaelcaricio, turns out I should have troubleshot a little longer 馃槃 ...
The following worked for me to avoid elasticsearch_dsl.exceptions.IllegalOperation:
class MyDoc(Document):
class Index:
doc_type = "a"
class Meta:
doc_type = "a"
I noticed that the following gave the error a already set, trying to assign doc
class MyDoc(Document):
class Index:
doc_type = "a"
And this gave the same error but reversed doc already set, trying to assign a
class MyDoc(Document):
class Meta:
doc_type = "a"
So setting doc_type in both Index and Meta is required to align them and prevent the error.
@HonzaKral do you know if this behavior is intentional?
@jason-huling A-HA, thank you. This should solve my case too.
Thank you all for your patience, I just pushed a fix that should make the part in class Index optional, so just defining a class Meta: doc_type = "a" should be enough.
I also cleaned up a lot of faulty logic when it came to Index handling. If you could test the current master (69f5ca2dc7657c26a19f11d75b7c2080afcfcdd6) I would be most grateful! After it has been tested I will release it as 6.3.0
Following didn't work for me:
class CustomerDocument(Document):
class Index:
name = 'testIndex'
doc_type = "customer"
class Meta:
doc_type = "customer"
Below runs without error but doesn't change _type to customer:
class CustomerDocument(Document):
class Index:
name = 'testIndex'
class Meta:
doc_type = "customer"
ES: 6.8.8
ES-DSL: 6.4.0
UPDATE:
I had done:
CustomerDocument().init()
Changing it to:
CustomerDocument.init()
Fixed it.
Most helpful comment
Thank you all for your patience, I just pushed a fix that should make the part in
class Indexoptional, so just defining aclass Meta: doc_type = "a"should be enough.I also cleaned up a lot of faulty logic when it came to
Indexhandling. If you could test the currentmaster(69f5ca2dc7657c26a19f11d75b7c2080afcfcdd6) I would be most grateful! After it has been tested I will release it as6.3.0