Skip to content

Document mapping doesn't accept any name but 'doc' #929

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
noamkush opened this issue Jul 3, 2018 · 8 comments
Closed

Document mapping doesn't accept any name but 'doc' #929

noamkush opened this issue Jul 3, 2018 · 8 comments

Comments

@noamkush
Copy link

noamkush commented Jul 3, 2018

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.

@honzakral
Copy link
Contributor

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.

@jhulndev
Copy link

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!

@rafaelcaricio
Copy link

@jason-huling I'm having the same problem. Please let me know if you find a solution.

@jhulndev
Copy link

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.

@jhulndev
Copy link

@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?

@rafaelcaricio
Copy link

@jason-huling A-HA, thank you. This should solve my case too.

@honzakral
Copy link
Contributor

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 (69f5ca2) I would be most grateful! After it has been tested I will release it as 6.3.0

0bsearch pushed a commit to 0bsearch/elasticsearch-dsl-py that referenced this issue Mar 2, 2019
@asifali22
Copy link

asifali22 commented Jun 3, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants