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.