Implement Create/Drop Index/Metadata index in GO (#13791)

### What problem does this PR solve?

Implement Create/Drop Index/Metadata index in GO

New API handling in GO:
POST/kb/index 
DELETE /kb/index
POST /tenant/doc_meta_index
DELETE /tenant/doc_meta_index

CREATE INDEX FOR DATASET 'dataset_name' VECTOR_SIZE 1024;
DROP INDEX FOR DATASET 'dataset_name';
CREATE INDEX DOC_META;
DROP INDEX DOC_META;

### Type of change

- [x] Refactoring
This commit is contained in:
qinling0210
2026-03-26 11:54:10 +08:00
committed by GitHub
parent d19ca71b43
commit ebf36950e4
20 changed files with 1165 additions and 30 deletions

View File

@ -84,6 +84,10 @@ sql_command: login_user
| list_user_chats
| create_user_chat
| drop_user_chat
| create_index
| drop_index
| create_doc_meta_index
| drop_doc_meta_index
| list_user_model_providers
| list_user_default_models
| parse_dataset_docs
@ -176,6 +180,7 @@ IMPORT: "IMPORT"i
INTO: "INTO"i
IN: "IN"i
WITH: "WITH"i
VECTOR_SIZE: "VECTOR_SIZE"i
PARSER: "PARSER"i
PIPELINE: "PIPELINE"i
SEARCH: "SEARCH"i
@ -197,6 +202,8 @@ FINGERPRINT: "FINGERPRINT"i
LICENSE: "LICENSE"i
CHECK: "CHECK"i
CONFIG: "CONFIG"i
INDEX: "INDEX"i
DOC_META: "DOC_META"i
CHUNK: "CHUNK"i
CHUNKS: "CHUNKS"i
GET: "GET"i
@ -323,6 +330,10 @@ list_user_agents: LIST AGENTS ";"
list_user_chats: LIST CHATS ";"
create_user_chat: CREATE CHAT quoted_string ";"
drop_user_chat: DROP CHAT quoted_string ";"
create_index: CREATE INDEX FOR DATASET quoted_string VECTOR_SIZE NUMBER ";"
drop_index: DROP INDEX FOR DATASET quoted_string ";"
create_doc_meta_index: CREATE INDEX DOC_META ";"
drop_doc_meta_index: DROP INDEX DOC_META ";"
create_chat_session: CREATE CHAT quoted_string SESSION ";"
drop_chat_session: DROP CHAT quoted_string SESSION quoted_string ";"
list_chat_sessions: LIST CHAT quoted_string SESSIONS ";"
@ -650,6 +661,31 @@ class RAGFlowCLITransformer(Transformer):
chat_name = items[2].children[0].strip("'\"")
return {"type": "drop_user_chat", "chat_name": chat_name}
def create_index(self, items):
# items: CREATE, INDEX, FOR, DATASET, quoted_string, VECTOR_SIZE, NUMBER, ";"
dataset_name = None
vector_size = None
for i, item in enumerate(items):
if hasattr(item, 'data') and item.data == 'quoted_string':
dataset_name = item.children[0].strip("'\"")
if hasattr(item, 'type') and item.type == 'NUMBER':
if i > 0 and items[i-1].type == 'VECTOR_SIZE':
vector_size = int(item)
return {"type": "create_index", "dataset_name": dataset_name, "vector_size": vector_size}
def drop_index(self, items):
dataset_name = None
for item in items:
if hasattr(item, 'data') and item.data == 'quoted_string':
dataset_name = item.children[0].strip("'\"")
return {"type": "drop_index", "dataset_name": dataset_name}
def create_doc_meta_index(self, items):
return {"type": "create_doc_meta_index"}
def drop_doc_meta_index(self, items):
return {"type": "drop_doc_meta_index"}
def list_user_model_providers(self, items):
return {"type": "list_user_model_providers"}

View File

@ -1080,6 +1080,75 @@ class RAGFlowClient:
else:
print(f"Fail to create chat {chat_name}, code: {res_json['code']}, message: {res_json['message']}")
def create_index(self, command):
if self.server_type != "user":
print("This command is only allowed in USER mode")
return
dataset_name = command["dataset_name"]
vector_size = command.get("vector_size")
if not vector_size:
print("vector_size is required")
return
# Get dataset ID by name
dataset_id = self._get_dataset_id(dataset_name)
if dataset_id is None:
return
# Build payload
payload = {"kb_id": dataset_id, "vector_size": vector_size}
# Call API
response = self.http_client.request("POST", "/kb/index", json_body=payload,
use_api_base=False, auth_kind="web")
res_json = response.json()
if response.status_code == 200 and res_json.get("code") == 0:
print(f"Success to create index for dataset: {dataset_name}")
else:
print(f"Fail to create index for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")
def drop_index(self, command):
if self.server_type != "user":
print("This command is only allowed in USER mode")
return
dataset_name = command["dataset_name"]
# Get dataset ID by name
dataset_id = self._get_dataset_id(dataset_name)
if dataset_id is None:
return
# Call API to delete index
payload = {"kb_id": dataset_id}
response = self.http_client.request("DELETE", "/kb/index", json_body=payload,
use_api_base=False, auth_kind="web")
res_json = response.json()
if response.status_code == 200 and res_json.get("code") == 0:
print(f"Success to drop index for dataset: {dataset_name}")
else:
print(f"Fail to drop index for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")
def create_doc_meta_index(self, command):
if self.server_type != "user":
print("This command is only allowed in USER mode")
return
# Call API to create doc meta index
response = self.http_client.request("POST", "/tenant/doc_meta_index",
use_api_base=False, auth_kind="web")
res_json = response.json()
if response.status_code == 200 and res_json.get("code") == 0:
print("Success to create doc meta index")
else:
print(f"Fail to create doc meta index, code: {res_json.get('code')}, message: {res_json.get('message')}")
def drop_doc_meta_index(self, command):
if self.server_type != "user":
print("This command is only allowed in USER mode")
return
# Call API to delete doc meta index
response = self.http_client.request("DELETE", "/tenant/doc_meta_index",
use_api_base=False, auth_kind="web")
res_json = response.json()
if response.status_code == 200 and res_json.get("code") == 0:
print("Success to drop doc meta index")
else:
print(f"Fail to drop doc meta index, code: {res_json.get('code')}, message: {res_json.get('message')}")
def drop_user_chat(self, command):
if self.server_type != "user":
print("This command is only allowed in USER mode")
@ -1804,6 +1873,14 @@ def run_command(client: RAGFlowClient, command_dict: dict):
client.create_user_chat(command_dict)
case "drop_user_chat":
client.drop_user_chat(command_dict)
case "create_index":
client.create_index(command_dict)
case "drop_index":
client.drop_index(command_dict)
case "create_doc_meta_index":
client.create_doc_meta_index(command_dict)
case "drop_doc_meta_index":
client.drop_doc_meta_index(command_dict)
case "create_chat_session":
client.create_chat_session(command_dict)
case "drop_chat_session":
@ -1887,6 +1964,10 @@ LIST METADATA OF DATASETS <dataset>[, <dataset>]*
LIST METADATA SUMMARY OF DATASET <dataset> DOCUMENTS <doc_id>[, <doc_id>]*
GET CHUNK <chunk_id>
LIST CHUNKS OF DOCUMENT <doc_id> [PAGE <page>] [SIZE <size>] [KEYWORDS <keywords>] [AVAILABLE <0|1>]
CREATE INDEX FOR DATASET <dataset> VECTOR_SIZE <vector_size>
DROP INDEX FOR DATASET <dataset>
CREATE INDEX DOC_META
DROP INDEX DOC_META
Meta Commands:
\\?, \\h, \\help Show this help