Skip to content

Commit

Permalink
Python [FT.CREATE] command added(Created for release-1.2 branch) (#2426)
Browse files Browse the repository at this point in the history
* Python [FT.CREATE] command added

Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>

---------

Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>
  • Loading branch information
prateek-kumar-improving authored Oct 11, 2024
1 parent a2ea32e commit 8fea303
Show file tree
Hide file tree
Showing 6 changed files with 654 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Python: Python: Added FT.CREATE command([#2413](https://github.com/valkey-io/valkey-glide/pull/2413))
* Python: Add JSON.ARRLEN command ([#2403](https://github.com/valkey-io/valkey-glide/pull/2403))
* Python: Add JSON.CLEAR command ([#2418](https://github.com/valkey-io/valkey-glide/pull/2418))

Expand Down
34 changes: 33 additions & 1 deletion python/python/glide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@
InsertPosition,
UpdateOptions,
)
from glide.async_commands.server_modules import json
from glide.async_commands.server_modules import ft, json
from glide.async_commands.server_modules.ft_options.ft_create_options import (
DataType,
DistanceMetricType,
Field,
FieldType,
FtCreateOptions,
NumericField,
TagField,
TextField,
VectorAlgorithm,
VectorField,
VectorFieldAttributes,
VectorFieldAttributesFlat,
VectorFieldAttributesHnsw,
VectorType,
)
from glide.async_commands.sorted_set import (
AggregationType,
GeoSearchByBox,
Expand Down Expand Up @@ -185,6 +201,7 @@
"InfoSection",
"InsertPosition",
"json",
"ft",
"LexBoundary",
"Limit",
"ListDirection",
Expand Down Expand Up @@ -233,4 +250,19 @@
"GlideError",
"RequestError",
"TimeoutError",
# Ft
"DataType",
"DistanceMetricType",
"Field",
"FieldType",
"FtCreateOptions",
"NumericField",
"TagField",
"TextField",
"VectorAlgorithm",
"VectorField",
"VectorFieldAttributes",
"VectorFieldAttributesFlat",
"VectorFieldAttributesHnsw",
"VectorType",
]
56 changes: 56 additions & 0 deletions python/python/glide/async_commands/server_modules/ft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
"""
module for `vector search` commands.
"""

from typing import List, Optional, cast

from glide.async_commands.server_modules.ft_constants import (
CommandNames,
FtCreateKeywords,
)
from glide.async_commands.server_modules.ft_options.ft_create_options import (
Field,
FtCreateOptions,
)
from glide.constants import TOK, TEncodable
from glide.glide_client import TGlideClient


async def create(
client: TGlideClient,
indexName: TEncodable,
schema: List[Field],
options: Optional[FtCreateOptions] = None,
) -> TOK:
"""
Creates an index and initiates a backfill of that index.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name for the index to be created
schema (List[Field]): The fields of the index schema, specifying the fields and their types.
options (Optional[FtCreateOptions]): Optional arguments for the [FT.CREATE] command.
Returns:
If the index is successfully created, returns "OK".
Examples:
>>> from glide.async_commands.server_modules import ft
>>> schema: List[Field] = []
>>> field: TextField = TextField("title")
>>> schema.append(field)
>>> prefixes: List[str] = []
>>> prefixes.append("blog:post:")
>>> index = "idx"
>>> result = await ft.create(glide_client, index, schema, FtCreateOptions(DataType.HASH, prefixes))
b'OK' # Indicates successful creation of index named 'idx'
"""
args: List[TEncodable] = [CommandNames.FT_CREATE, indexName]
if options:
args.extend(options.toArgs())
if schema:
args.append(FtCreateKeywords.SCHEMA)
for field in schema:
args.extend(field.toArgs())
return cast(TOK, await client.custom_command(args))
32 changes: 32 additions & 0 deletions python/python/glide/async_commands/server_modules/ft_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0


class CommandNames:
"""
Command name constants for vector search.
"""

FT_CREATE = "FT.CREATE"


class FtCreateKeywords:
"""
Keywords used in the [FT.CREATE] command statment.
"""

SCHEMA = "SCHEMA"
AS = "AS"
SORTABLE = "SORTABLE"
UNF = "UNF"
NO_INDEX = "NOINDEX"
ON = "ON"
PREFIX = "PREFIX"
SEPARATOR = "SEPARATOR"
CASESENSITIVE = "CASESENSITIVE"
DIM = "DIM"
DISTANCE_METRIC = "DISTANCE_METRIC"
TYPE = "TYPE"
INITIAL_CAP = "INITIAL_CAP"
M = "M"
EF_CONSTRUCTION = "EF_CONSTRUCTION"
EF_RUNTIME = "EF_RUNTIME"
Loading

0 comments on commit 8fea303

Please sign in to comment.