{
  "id": "schema",
  "title": "Schema",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/0.8.1/api/schema/",
  "summary": "",
  "content": "\n\nSchema in RedisVL provides a structured format to define index settings and\nfield configurations using the following three components:\n\n| Component   | Description                                                                        |\n|-------------|------------------------------------------------------------------------------------|\n| version     | The version of the schema spec. Current supported version is 0.1.0.                |\n| index       | Index specific settings like name, key prefix, key separator, and storage type.    |\n| fields      | Subset of fields within your data to include in the index and any custom settings. |\n\n## IndexSchema\n\n\u003ca id=\"indexschema-api\"\u003e\u003c/a\u003e\n\n### `class IndexSchema(*, index, fields={}, version='0.1.0')`\n\nA schema definition for a search index in Redis, used in RedisVL for\nconfiguring index settings and organizing vector and metadata fields.\n\nThe class offers methods to create an index schema from a YAML file or a\nPython dictionary, supporting flexible schema definitions and easy\nintegration into various workflows.\n\nAn example schema.yaml file might look like this:\n\n```yaml\nversion: '0.1.0'\n\nindex:\n    name: user-index\n    prefix: user\n    key_separator: \":\"\n    storage_type: json\n\nfields:\n    - name: user\n      type: tag\n    - name: credit_score\n      type: tag\n    - name: embedding\n      type: vector\n      attrs:\n        algorithm: flat\n        dims: 3\n        distance_metric: cosine\n        datatype: float32\n```\n\nLoading the schema for RedisVL from yaml is as simple as:\n\n```python\nfrom redisvl.schema import IndexSchema\n\nschema = IndexSchema.from_yaml(\"schema.yaml\")\n```\n\nLoading the schema for RedisVL from dict is as simple as:\n\n```python\nfrom redisvl.schema import IndexSchema\n\nschema = IndexSchema.from_dict({\n    \"index\": {\n        \"name\": \"user-index\",\n        \"prefix\": \"user\",\n        \"key_separator\": \":\",\n        \"storage_type\": \"json\",\n    },\n    \"fields\": [\n        {\"name\": \"user\", \"type\": \"tag\"},\n        {\"name\": \"credit_score\", \"type\": \"tag\"},\n        {\n            \"name\": \"embedding\",\n            \"type\": \"vector\",\n            \"attrs\": {\n                \"algorithm\": \"flat\",\n                \"dims\": 3,\n                \"distance_metric\": \"cosine\",\n                \"datatype\": \"float32\"\n            }\n        }\n    ]\n})\n```\n\n#### `NOTE`\nThe fields attribute in the schema must contain unique field names to ensure\ncorrect and unambiguous field references.\n\nCreate a new model by parsing and validating input data from keyword arguments.\n\nRaises [ValidationError][pydantic_core.ValidationError] if the input data cannot be\nvalidated to form a valid model.\n\nself is explicitly positional-only to allow self as a field name.\n\n* **Parameters:**\n  * **index** (*IndexInfo*)\n  * **fields** (*Dict* *[* *str* *,* *BaseField* *]*)\n  * **version** (*Literal* *[* *'0.1.0'* *]*)\n\n#### `add_field(field_inputs)`\n\nAdds a single field to the index schema based on the specified field\ntype and attributes.\n\nThis method allows for the addition of individual fields to the schema,\nproviding flexibility in defining the structure of the index.\n\n* **Parameters:**\n  **field_inputs** (*Dict* *[* *str* *,* *Any* *]*) – A field to add.\n* **Raises:**\n  **ValueError** – If the field name or type are not provided or if the name\n      already exists within the schema.\n\n```python\n# Add a tag field\nschema.add_field({\"name\": \"user\", \"type\": \"tag})\n\n# Add a vector field\nschema.add_field({\n    \"name\": \"user-embedding\",\n    \"type\": \"vector\",\n    \"attrs\": {\n        \"dims\": 1024,\n        \"algorithm\": \"flat\",\n        \"datatype\": \"float32\"\n    }\n})\n```\n\n#### `add_fields(fields)`\n\nExtends the schema with additional fields.\n\nThis method allows dynamically adding new fields to the index schema. It\nprocesses a list of field definitions.\n\n* **Parameters:**\n  **fields** (*List* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – A list of fields to add.\n* **Raises:**\n  **ValueError** – If a field with the same name already exists in the\n      schema.\n\n```python\nschema.add_fields([\n    {\"name\": \"user\", \"type\": \"tag\"},\n    {\"name\": \"bio\", \"type\": \"text\"},\n    {\n        \"name\": \"user-embedding\",\n        \"type\": \"vector\",\n        \"attrs\": {\n            \"dims\": 1024,\n            \"algorithm\": \"flat\",\n            \"datatype\": \"float32\"\n        }\n    }\n])\n```\n\n#### `classmethod from_dict(data)`\n\nCreate an IndexSchema from a dictionary.\n\n* **Parameters:**\n  **data** (*Dict* *[* *str* *,* *Any* *]*) – The index schema data.\n* **Returns:**\n  The index schema.\n* **Return type:**\n  [IndexSchema](#indexschema)\n\n```python\nfrom redisvl.schema import IndexSchema\n\nschema = IndexSchema.from_dict({\n    \"index\": {\n        \"name\": \"docs-index\",\n        \"prefix\": \"docs\",\n        \"storage_type\": \"hash\",\n    },\n    \"fields\": [\n        {\n            \"name\": \"doc-id\",\n            \"type\": \"tag\"\n        },\n        {\n            \"name\": \"doc-embedding\",\n            \"type\": \"vector\",\n            \"attrs\": {\n                \"algorithm\": \"flat\",\n                \"dims\": 1536\n            }\n        }\n    ]\n})\n```\n\n#### `classmethod from_yaml(file_path)`\n\nCreate an IndexSchema from a YAML file.\n\n* **Parameters:**\n  **file_path** (*str*) – The path to the YAML file.\n* **Returns:**\n  The index schema.\n* **Return type:**\n  [IndexSchema](#indexschema)\n\n```python\nfrom redisvl.schema import IndexSchema\nschema = IndexSchema.from_yaml(\"schema.yaml\")\n```\n\n#### `remove_field(field_name)`\n\nRemoves a field from the schema based on the specified name.\n\nThis method is useful for dynamically altering the schema by removing\nexisting fields.\n\n* **Parameters:**\n  **field_name** (*str*) – The name of the field to be removed.\n\n#### `to_dict()`\n\nSerialize the index schema model to a dictionary, handling Enums\nand other special cases properly.\n\n* **Returns:**\n  The index schema as a dictionary.\n* **Return type:**\n  Dict[str, Any]\n\n#### `to_yaml(file_path, overwrite=True)`\n\nWrite the index schema to a YAML file.\n\n* **Parameters:**\n  * **file_path** (*str*) – The path to the YAML file.\n  * **overwrite** (*bool*) – Whether to overwrite the file if it already exists.\n* **Raises:**\n  **FileExistsError** – If the file already exists and overwrite is False.\n* **Return type:**\n  None\n\n#### `property field_names: List[str]`\n\nA list of field names associated with the index schema.\n\n* **Returns:**\n  A list of field names from the schema.\n* **Return type:**\n  List[str]\n\n#### `fields: Dict[str, BaseField]`\n\nFields associated with the search index and their properties\n\n#### `index: IndexInfo`\n\nDetails of the basic index configurations.\n\n#### `model_config: ClassVar[ConfigDict] = {}`\n\nConfiguration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].\n\n#### `version: Literal['0.1.0']`\n\nVersion of the underlying index schema.\n\n## Defining Fields\n\nFields 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.\n\n**YAML Example**:\n\n```yaml\n- name: title\n  type: text\n  path: $.document.title\n  attrs:\n    weight: 1.0\n    no_stem: false\n    withsuffixtrie: true\n```\n\n**Python Dictionary Example**:\n\n```python\n{\n    \"name\": \"location\",\n    \"type\": \"geo\",\n    \"attrs\": {\n        \"sortable\": true\n    }\n}\n```\n\n## Supported Field Types and Attributes\n\nEach field type supports specific attributes that customize its behavior. Below are the field types and their available attributes:\n\n**Text Field Attributes**:\n\n- weight: Importance of the field in result calculation.\n- no_stem: Disables stemming during indexing.\n- withsuffixtrie: Optimizes queries by maintaining a suffix trie.\n- phonetic_matcher: Enables phonetic matching.\n- sortable: Allows sorting on this field.\n\n**Tag Field Attributes**:\n\n- separator: Character for splitting text into individual tags.\n- case_sensitive: Case sensitivity in tag matching.\n- withsuffixtrie: Suffix trie optimization for queries.\n- sortable: Enables sorting based on the tag field.\n\n**Numeric and Geo Field Attributes**:\n\n- Both numeric and geo fields support the sortable attribute, enabling sorting on these fields.\n\n**Common Vector Field Attributes**:\n\n- dims: Dimensionality of the vector.\n- algorithm: Indexing algorithm (flat or hnsw).\n- datatype: Float datatype of the vector (bfloat16, float16, float32, float64).\n- distance_metric: Metric for measuring query relevance (COSINE, L2, IP).\n\n**HNSW Vector Field Specific Attributes**:\n\n- m: Max outgoing edges per node in each layer.\n- ef_construction: Max edge candidates during build time.\n- ef_runtime: Max top candidates during search.\n- epsilon: Range search boundary factor.\n\nNote:\n: See fully documented Redis-supported fields and options here: [https://redis.io/commands/ft.create/](https://redis.io/commands/ft.create/)\n",
  "tags": [],
  "last_updated": "2026-04-01T08:10:08-05:00"
}

