FT.CREATE
Syntax
FT.CREATE index
[ON HASH | JSON]
[PREFIX count prefix [prefix ...]]
SCHEMA field_name [AS alias] TEXT | TAG | NUMERIC | VECTOR [ SORTABLE ]
[NOINDEX] [ field_name [AS alias] TEXT | TAG | NUMERIC | VECTOR [ SORTABLE ] [NOINDEX] ...]
Time complexity: O(K) at creation where K is the number of fields, O(N) if scanning the keyspace is triggered, where N is the number of keys in the keyspace.
Important: New in Dragonfly v1.13. Currently, Dragonfly Search is in Beta.
Description
Create an index with the given specification. For usage, see examples below.
Required arguments
index
is index name to create. If such index already exists, returns an error reply.
SCHEMA field_name [AS alias] TEXT | TAG | NUMERIC | VECTOR [ SORTABLE ]
after the SCHEMA keyword, declares which fields to index:
{identifier}
is the name of the field to index:- For Hash values, identifier is a field name within the Hash.
- For JSON values, the identifier is a JSONPath expression.
AS {attribute}
defines the attribute associated to the identifier. For example, you can use this feature to alias a complex JSONPath expression with more memorable (and easier to type) name.
Field types are:
TEXT
- Allows searching for words against the text value in this attribute.TAG
- Allows exact-match queries, such as categories or primary keys, against the value in this attribute. For more information, see tag fields.NUMERIC
- Allows numeric range queries against the value in this attribute. See query syntax for details on how to use numeric ranges.VECTOR
- Allows vector similarity queries against the value in this attribute. For more information, see vector fields.
VECTOR
- Full documentation on vector options is available here.
- Currently, Dragonfly has limited support for vector options.
- You can specify either the
FLAT
or theHNSW
index type.- For both index types,
DIM
,DISTANCE_METRIC
, andINITIAL_CAP
options can be specified. - For the
DISTANCE_METRIC
option, onlyL2
andCOSINE
are supported.
- For both index types,
Field options are:
SORTABLE
-NUMERIC
,TAG
,TEXT
attributes can have an optional SORTABLE argument. As the user sorts the results by the value of this attribute, the results are available with very low latency. Note that his adds memory overhead, so consider not declaring it on large text attributes. You can sort an attribute without theSORTABLE
option, but the latency is not as good as withSORTABLE
.
SORTABLE
Dragonfly does not support sorting without the SORTABLE
option.
NOINDEX
- Attributes can have theNOINDEX
option, which means they will not be indexed. This is useful in conjunction withSORTABLE
, to create attributes whose update using PARTIAL will not cause full reindexing of the document. If an attribute has NOINDEX and doesn't have SORTABLE, it will just be ignored by the index.
Optional arguments
ON data_type
currently supports HASH
(default) and JSON
.
PREFIX count prefix
tells the index which keys it should index.
You can add several prefixes to index.
Because the argument is optional, the default is *
(all keys).
PREFIX
Currently, Dragonfly supports only one prefix (i.e., PREFIX 1 my_prefix
), if the PREFIX
option is used.
Return
FT.CREATE
returns a simple string reply OK
if executed correctly, or an error reply otherwise.
Examples
Create index on HASH
Create an index that stores the title, publication date, and categories of blog post hashes whose keys start with blog:post:
(i.e., blog:post:1
).
dragonfly> FT.CREATE idx ON HASH PREFIX 1 blog:post: SCHEMA title TEXT SORTABLE published_at NUMERIC SORTABLE category TAG SORTABLE
OK
Index the sku
attribute from a hash as both a TEXT
and as TAG
:
dragonfly> FT.CREATE idx ON HASH PREFIX 1 blog:post: SCHEMA sku AS sku_text TEXT sku AS sku_tag TAG SORTABLE
Create index on JSON
Index a JSON document using a JSONPath expression.
dragonfly> FT.CREATE idx ON JSON SCHEMA $.title AS title TEXT $.categories AS categories TAG