Skip to content
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

Persistence: Field called "meta" in Document #1969

Open
sbrandtb opened this issue Feb 14, 2025 · 1 comment
Open

Persistence: Field called "meta" in Document #1969

sbrandtb opened this issue Feb 14, 2025 · 1 comment

Comments

@sbrandtb
Copy link

I would like to define a field called "meta" on my Document. I read and understood the documentation that states:

Having all metadata accessible through meta means that this name is reserved and you shouldn’t have a field called meta on your document. If you, however, need it you can still access the data using the get item (as opposed to attribute) syntax: post['meta'].

Let's suppose I would like to define following Document:

class MyDocument(elastic.Document):
    meta = elastic.Keyword()  

I would like to define my mapping, including the field called "meta". How do I do that? I am ripping my hair out of this since I cannot find any way to do that, like meta_ = elastic.Keyword(actual_field_name='meta'), which would be comparable to other libraries' options (like Pydantic) in DocumentOptions or anything.

I would be also happy if someone could point out a way how I could add it to the mapping in some other way that does not blow elasticsearch-dsl's internals.

Changing the field name in my schema would be possible, but a hassle and from an ideal point of view, I do not want to.


However, I would also be curious about the decision making in this case. If I understood correctly, we are solely talking about providing an accessor for metadata for "ORM" model instances and what we are discussing here is not at all touching Elasticsearch's internals since it happily accepts a field called meta. Furthermore, "meta" does not sound like a terrible unlikely name for a document's field. So, if I understand correctly, no one who wants to use elasticsearch-dsl and its declarative persistence can ever declare this field.

Would it not make more sense to provide metadata in some way that has less likelihood of collisions? post.meta_ or post.elastic_meta or elasticsearch.meta(post)?

@miguelgrinberg
Copy link
Collaborator

miguelgrinberg commented Feb 14, 2025

I would like to define my mapping, including the field called "meta". How do I do that?

This appears to work:

import os
from elasticsearch_dsl import Document, field, connections

class MyDoc(Document):
    name: str = field.Keyword()
    meta: str = field.Keyword()

    class Index:
        name = "my-index"

connections.create_connection(hosts=os.environ['ELASTICSEARCH_URL'])
MyDoc.init()
doc = MyDoc(name="susan")
doc['meta'] = "foo"
doc.save()

for doc in MyDoc.search():
    print(f'{doc.name} {doc["meta"]}')

However, I would also be curious about the decision making in this case.
Would it not make more sense to provide metadata in some way that has less likelihood of collisions?

Hard to say. I have not participated in these decisions, which were made many years ago and predate my involvement in this project. I can be wrong on this, but I do not recall this ever being raised before as a big issue, because it is assumed that some concessions have to be made to allow this package to do its work in the most idiomatic way. You always have the option to send raw requests using the regular Python client if the DSL package does not work for you.

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

No branches or pull requests

2 participants