@spec primary_key_field(t()) :: Milvex.Schema.Field.t() | nil
Returns the primary key field from the schema.
Returns nil if no primary key is defined.
Builder for Milvus collection schemas.
Provides a fluent API for constructing collection schema definitions with validation. Schemas define the structure of data stored in Milvus collections, including fields, primary keys, and vector configurations.
alias Milvex.Schema
alias Milvex.Schema.Field
# Using the builder pattern
schema =
Schema.new("movies")
|> Schema.description("Movie embeddings collection")
|> Schema.add_field(Field.primary_key("id", :int64, auto_id: true))
|> Schema.add_field(Field.varchar("title", 512))
|> Schema.add_field(Field.vector("embedding", 128))
|> Schema.enable_dynamic_field()
|> Schema.validate!()
# Using the build/1 helper
schema = Schema.build(
name: "movies",
description: "Movie embeddings collection",
enable_dynamic_field: true,
fields: [
Field.primary_key("id", :int64, auto_id: true),
Field.varchar("title", 512),
Field.vector("embedding", 128)
]
)
Adds a field to the schema.
Adds multiple fields to the schema.
Adds a function to the schema.
Builds a schema from a keyword list or map.
Builds a schema from options and raises on error.
Sets the collection description.
Enables or disables dynamic fields for the collection.
Returns the names of all fields in the schema.
Creates a Schema from a protobuf CollectionSchema struct.
Finds a field by name.
Creates a new schema with the given collection name.
Returns the primary key field from the schema.
Returns all scalar fields from the schema.
Returns all array_of_struct fields from the schema.
Converts the schema to a protobuf CollectionSchema struct.
Validates the schema configuration.
Validates the schema and raises on error.
Returns all vector fields from the schema.
@type t() :: %Milvex.Schema{ description: String.t() | nil, enable_dynamic_field: boolean(), fields: [Milvex.Schema.Field.t()], functions: [Milvex.Function.t()], name: String.t() }
@spec add_field(t(), Milvex.Schema.Field.t()) :: t()
Adds a field to the schema.
schema
|> Schema.add_field(Field.new("id", :int64) |> Field.primary_key())
|> Schema.add_field(Field.vector("embedding", 128))
@spec add_fields(t(), [Milvex.Schema.Field.t()]) :: t()
Adds multiple fields to the schema.
Schema.add_fields(schema, [
Field.primary_key("id", :int64),
Field.vector("embedding", 128)
])
@spec add_function(t(), Milvex.Function.t()) :: t()
Adds a function to the schema.
schema
|> Schema.add_function(Function.bm25("bm25_fn", input: "content", output: "sparse"))
@spec build(keyword() | map()) :: {:ok, t()} | {:error, Milvex.Error.t()}
Builds a schema from a keyword list or map.
:name - Collection name (required):description - Collection description:fields - List of Field structs (required):enable_dynamic_field - Enable dynamic fields (default: false)Schema.build(
name: "movies",
fields: [
Field.primary_key("id", :int64),
Field.vector("embedding", 128)
]
)
Builds a schema from options and raises on error.
Sets the collection description.
Enables or disables dynamic fields for the collection.
When enabled, the collection can store fields not defined in the schema.
Returns the names of all fields in the schema.
@spec from_proto(Milvex.Milvus.Proto.Schema.CollectionSchema.t() | nil) :: t() | nil
Creates a Schema from a protobuf CollectionSchema struct.
Returns nil if the input is nil.
Combines regular fields and struct_array_fields into a single fields list.
@spec get_field(t(), String.t()) :: Milvex.Schema.Field.t() | nil
Finds a field by name.
Returns nil if the field is not found.
Creates a new schema with the given collection name.
name - Collection name (1-255 characters)Schema.new("movies")
@spec primary_key_field(t()) :: Milvex.Schema.Field.t() | nil
Returns the primary key field from the schema.
Returns nil if no primary key is defined.
@spec scalar_fields(t()) :: [Milvex.Schema.Field.t()]
Returns all scalar fields from the schema.
@spec struct_array_fields(t()) :: [Milvex.Schema.Field.t()]
Returns all array_of_struct fields from the schema.
@spec to_proto(t()) :: Milvex.Milvus.Proto.Schema.CollectionSchema.t()
Converts the schema to a protobuf CollectionSchema struct.
Splits fields into regular fields and struct_array_fields as required by the Milvus proto schema.
@spec validate(t()) :: {:ok, t()} | {:error, Milvex.Error.t()}
Validates the schema configuration.
Checks:
Returns {:ok, schema} if valid, {:error, error} otherwise.
Validates the schema and raises on error.
@spec vector_fields(t()) :: [Milvex.Schema.Field.t()]
Returns all vector fields from the schema.