Skip to main content

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.

About VECTOR
  • Full documentation on vector options is available here.
  • Currently, Dragonfly has limited support for vector options.
  • You can specify either the FLAT or the HNSW index type.
    • For both index types, DIM, DISTANCE_METRIC, and INITIAL_CAP options can be specified.
    • For the DISTANCE_METRIC option, only L2 and COSINE are supported.

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 the SORTABLE option, but the latency is not as good as with SORTABLE.
About SORTABLE

Dragonfly does not support sorting without the SORTABLE option.

  • NOINDEX - Attributes can have the NOINDEX option, which means they will not be indexed. This is useful in conjunction with SORTABLE, 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).

About 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

See also

FT.SEARCH | FT.DROPINDEX