Skip to content

Commit

Permalink
add example for tair_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
DuanxinCao committed Mar 6, 2023
1 parent 2b2cf68 commit 57a9a66
Showing 1 changed file with 98 additions and 4 deletions.
102 changes: 98 additions & 4 deletions examples/tair_vector.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,111 @@
#!/usr/bin/env python
from random import random

from conf_examples import get_tair

from tair import ResponseError

dim = 4
queries = [[random() for _ in range(dim)] for _ in range(2)]

# create an index
# @param index_name the name of index
# @param dims the dimension of vector
# @return success: True, fail: False.
def create_index(index_name: str, dims: str):
def create_index(index_name: str):
try:
tair = get_tair()
index_params = {
"M": 32,
"ef_construct": 200,
}
# index_params the params of index
return tair.tvs_create_index(index_name, dims, **index_params)
return tair.tvs_create_index(index_name, dim, **index_params)
except ResponseError as e:
print(e)
return None

def get_index(index_name: str):
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_get_index(index_name)
except ResponseError as e:
print(e)
return None

def scan_index():
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_scan_index()
except ResponseError as e:
print(e)
return None

def hset(index_name:str):
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_hset(index_name,"key","[1.1,1.1,1.1,1.1]")
except ResponseError as e:
print(e)
return None

def hget(index_name:str):
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_hgetall(index_name,"key")
except ResponseError as e:
print(e)
return None

def scan(index_name:str):
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_scan(index_name)
except ResponseError as e:
print(e)
return None

def knnsearch(index_name:str):
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_knnsearch(index_name,1,queries[0])
except ResponseError as e:
print(e)
return None


def mknnsearch(index_name:str):
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_mknnsearch(index_name,1,queries)
except ResponseError as e:
print(e)
return None

def mindexknnsearch():
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_mindexknnsearch(["test", "test2"],1,queries[0])
except ResponseError as e:
print(e)
return None

def mindexmknnsearch():
try:
tair = get_tair()
# index_params the params of index
return tair.tvs_mindexmknnsearch(["test", "test2"],1,queries)
except ResponseError as e:
print(e)
return None

# delete an index
# @param index_name the name of index
Expand All @@ -33,7 +118,16 @@ def delete_index(index_name: str):
print(e)
return False


if __name__ == "__main__":
create_index("test", 4)
create_index("test")
create_index("test2")
get_index("test")
scan_index()
hset("test")
hget("test")
scan("test")
knnsearch("test")
mknnsearch("test")
mindexknnsearch()
mindexmknnsearch()
delete_index("test")

0 comments on commit 57a9a66

Please sign in to comment.