-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Pydantic integration #3086
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
Open
miguelgrinberg
wants to merge
7
commits into
elastic:main
Choose a base branch
from
miguelgrinberg:dsl-support-annotated-syntax
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pydantic integration #3086
miguelgrinberg
wants to merge
7
commits into
elastic:main
from
miguelgrinberg:dsl-support-annotated-syntax
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4ae0574
to
891205c
Compare
1f3f66c
to
b9ada0f
Compare
f9ddddc
to
c552171
Compare
2d90d7a
to
5f41106
Compare
112b8d4
to
51be343
Compare
51be343
to
1a4822c
Compare
fe23bb4
to
8902d66
Compare
🔍 Preview links for changed docs |
8902d66
to
d8ec4fe
Compare
d8ec4fe
to
df630e3
Compare
df630e3
to
c15e7b5
Compare
c15e7b5
to
4884de8
Compare
4884de8
to
b19fa58
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change adds a few features that support the use of Pydantic models with the DSL module, instead of the standard models defined as subclasses of the
AsyncDocument
class.As part of this work some additions have been made to the typing implementation of DSL documents.
Annotated
syntax when defining document fields in the DSL module. Examples:New
BaseESModel
andAsyncBaseESModel
classes that inherit from Pydantic'sBaseModel
and add Elasticsearch superpowers. In particular, any model defined with one of these as its base class will havemeta
and_doc
private attributes andto_doc()
andfrom_doc()
methods. Themeta
attribute includes metadata for each document, things such asid
orscore
. The_doc
attribute is a dynamically generatedDocument
orAsyncDocument
instance that can be used whenever access to the Elasticsearch index is needed. The methods convert between Pydantic models and ES documents.Aside from the extra attributes, this class works exactly like
BaseModel
and can be used to define data attributes and their validation rules, and the ES document is derived from them automatically. In particular, this class can be used in FastAPI routes, as shown in thequotes
example included in this PR. Any annotations intended for the DSL module can be included in theAnnotated[]
type hint of the respective fields. TheIndex
inner class can be included as well.