content/develop/ai/redisvl/0.8.1/api/schema.md
Schema in RedisVL provides a structured format to define index settings and field configurations using the following three components:
| Component | Description |
|---|---|
| version | The version of the schema spec. Current supported version is 0.1.0. |
| index | Index specific settings like name, key prefix, key separator, and storage type. |
| fields | Subset of fields within your data to include in the index and any custom settings. |
<a id="indexschema-api"></a>
class IndexSchema(*, index, fields={}, version='0.1.0')A schema definition for a search index in Redis, used in RedisVL for configuring index settings and organizing vector and metadata fields.
The class offers methods to create an index schema from a YAML file or a Python dictionary, supporting flexible schema definitions and easy integration into various workflows.
An example schema.yaml file might look like this:
version: '0.1.0'
index:
name: user-index
prefix: user
key_separator: ":"
storage_type: json
fields:
- name: user
type: tag
- name: credit_score
type: tag
- name: embedding
type: vector
attrs:
algorithm: flat
dims: 3
distance_metric: cosine
datatype: float32
Loading the schema for RedisVL from yaml is as simple as:
from redisvl.schema import IndexSchema
schema = IndexSchema.from_yaml("schema.yaml")
Loading the schema for RedisVL from dict is as simple as:
from redisvl.schema import IndexSchema
schema = IndexSchema.from_dict({
"index": {
"name": "user-index",
"prefix": "user",
"key_separator": ":",
"storage_type": "json",
},
"fields": [
{"name": "user", "type": "tag"},
{"name": "credit_score", "type": "tag"},
{
"name": "embedding",
"type": "vector",
"attrs": {
"algorithm": "flat",
"dims": 3,
"distance_metric": "cosine",
"datatype": "float32"
}
}
]
})
NOTEThe fields attribute in the schema must contain unique field names to ensure correct and unambiguous field references.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
add_field(field_inputs)Adds a single field to the index schema based on the specified field type and attributes.
This method allows for the addition of individual fields to the schema, providing flexibility in defining the structure of the index.
# Add a tag field
schema.add_field({"name": "user", "type": "tag})
# Add a vector field
schema.add_field({
"name": "user-embedding",
"type": "vector",
"attrs": {
"dims": 1024,
"algorithm": "flat",
"datatype": "float32"
}
})
add_fields(fields)Extends the schema with additional fields.
This method allows dynamically adding new fields to the index schema. It processes a list of field definitions.
schema.add_fields([
{"name": "user", "type": "tag"},
{"name": "bio", "type": "text"},
{
"name": "user-embedding",
"type": "vector",
"attrs": {
"dims": 1024,
"algorithm": "flat",
"datatype": "float32"
}
}
])
classmethod from_dict(data)Create an IndexSchema from a dictionary.
from redisvl.schema import IndexSchema
schema = IndexSchema.from_dict({
"index": {
"name": "docs-index",
"prefix": "docs",
"storage_type": "hash",
},
"fields": [
{
"name": "doc-id",
"type": "tag"
},
{
"name": "doc-embedding",
"type": "vector",
"attrs": {
"algorithm": "flat",
"dims": 1536
}
}
]
})
classmethod from_yaml(file_path)Create an IndexSchema from a YAML file.
from redisvl.schema import IndexSchema
schema = IndexSchema.from_yaml("schema.yaml")
remove_field(field_name)Removes a field from the schema based on the specified name.
This method is useful for dynamically altering the schema by removing existing fields.
to_dict()Serialize the index schema model to a dictionary, handling Enums and other special cases properly.
to_yaml(file_path, overwrite=True)Write the index schema to a YAML file.
property field_names: List[str]A list of field names associated with the index schema.
fields: Dict[str, BaseField]Fields associated with the search index and their properties
index: IndexInfoDetails of the basic index configurations.
model_config: ClassVar[ConfigDict] = {}Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
version: Literal['0.1.0']Version of the underlying index schema.
Fields in the schema can be defined in YAML format or as a Python dictionary, specifying a name, type, an optional path, and attributes for customization.
YAML Example:
- name: title
type: text
path: $.document.title
attrs:
weight: 1.0
no_stem: false
withsuffixtrie: true
Python Dictionary Example:
{
"name": "location",
"type": "geo",
"attrs": {
"sortable": true
}
}
Each field type supports specific attributes that customize its behavior. Below are the field types and their available attributes:
Text Field Attributes:
Tag Field Attributes:
Numeric and Geo Field Attributes:
Common Vector Field Attributes:
HNSW Vector Field Specific Attributes:
Note: : See fully documented Redis-supported fields and options here: https://redis.io/commands/ft.create/