XU XUv0.16.0

#github.com/xulang-org/packages/milvus

导入方式: milvus #github.com/xulang-org/packages/milvus

公开 API: 4 函数, 16 方法, 6 类型, 0 常量, 0 方法集

扩展包 `milvus #github.com/xulang-org/packages/milvus`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

50_standalone_milvus/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@main

milvus #github.com/xulang-org/packages/milvus
#errors
#strings

fn verify_milvus_pool_and_entities() {
    println("--- 1. Testing Milvus Vector Protocol & Pool ---")
    $p := milvus.open_pool("http://127.0.0.1:19530", 5)

    // 1. Connection Reuse
    fd1 := p.get_conn()
    if fd1 < 0 {
        println("   [FAIL] Failed to connect to Milvus port 19530")
        <<
    }
    println("   Borrow 1 fd: " + str(fd1))
    p.put_conn(fd1)

    fd2 := p.get_conn()
    if fd2 < 0 {
        println("   [FAIL] Failed to re-borrow Milvus connection")
        <<
    }
    println("   Borrow 2 (after return) fd: " + str(fd2))
    if fd1 == fd2 {
        println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
    } else {
        println("   [WARN] Different fd: " + str(fd1) + " vs " + str(fd2))
    }

    // 2. Concurrency
    fd3 := p.get_conn()
    if fd3 < 0 {
        println("   [FAIL] Failed to borrow concurrent connection")
        <<
    }
    println("   Borrow 3 (concurrently) fd: " + str(fd3))
    if fd2 != fd3 {
        println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
    } else {
        println("   [FAIL] Expected distinct fds for concurrent connections!")
    }
    p.put_conn(fd2)
    p.put_conn(fd3)

    // 2. Data model validation
    cfg := milvus.CollectionConfig{
        name: "test_articles",
        dimension: 4,
        metric_type: "COSINE"
    }
    if cfg.name == "test_articles" && cfg.dimension == 4 && cfg.metric_type == "COSINE" {
        println("   [PASS] Milvus CollectionConfig verified: 4-dim COSINE")
    }

    e1 := milvus.VectorEntity{
        id: 101,
        vector: ["0.15", "0.25", "0.35", "0.45"],
        text: "Cloud Native Vector Search"
    }
    if e1.id == 101 && e1.vector.len == 4 {
        println("   [PASS] Milvus VectorEntity serialized cleanly: id=101, dim=4")
    }

    p.close()
    println("   [PASS] Milvus Pool closed cleanly.")
}

fn main() {
    println("=== Xu Milvus Native Driver Verification ===\n")
    verify_milvus_pool_and_entities()
    println("\n[MILVUS VERIFICATION 100% COMPLETED]")
}

API 声明

client

XuAPI
pub fn client(endpoint string) MilvusClient

用途

客户端。

参数

endpoint string
endpoint(string)

返回值

MilvusClient — 该类型的值

pool

XuAPI
pub fn pool(endpoint string, max_conns int) MilvusPool

用途

执行 pool 处理。

参数

endpoint string
endpoint(string)
max_conns int
max_conns(int)

返回值

MilvusPool — 该类型的值

struct MilvusClient

XuAPI
pub struct MilvusClient {
    pub host string
    pub port int
    pub secure bool
    pub token string
}

用途

Milvus 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
token string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string) MilvusClient

用途

构造 client。

参数

endpoint string
endpoint(string)

返回值

MilvusClient — 该类型的值

(MilvusClient) connect

XuAPI
pub fn (c MilvusClient) connect() int

用途

建立与目标服务端点的网络通信连接。失败时抛出错误,调用方需处理或继续传播。

参数

c MilvusClient
本次调用使用的 MilvusClient 实例;按值接收。

返回值

int — 该类型的值

(MilvusClient) do_request_on_fd

XuAPI
pub fn (c MilvusClient) do_request_on_fd(fd int, method string, path string, body string) string

用途

MilvusClient 上的操作:执行 do request on fd 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c MilvusClient
本次调用使用的 MilvusClient 实例;按值接收。
fd int
文件描述符(int)
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)

返回值

string — 结果字符串

(MilvusClient) do_request

XuAPI
pub fn (c MilvusClient) do_request(method string, path string, body string) string

用途

MilvusClient 上的操作:执行一次请求。失败时抛出错误,调用方需处理或继续传播。

参数

c MilvusClient
本次调用使用的 MilvusClient 实例;按值接收。
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)

返回值

string — 结果字符串

(MilvusClient) create_collection

XuAPI
pub fn (c MilvusClient) create_collection(cfg CollectionConfig) bool

用途

MilvusClient 上的操作:执行 create collection 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c MilvusClient
本次调用使用的 MilvusClient 实例;按值接收。
cfg CollectionConfig
cfg(CollectionConfig)

返回值

bool — 是否成立

(MilvusClient) drop_collection

XuAPI
pub fn (c MilvusClient) drop_collection(name string) bool

用途

MilvusClient 上的操作:执行 drop collection 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c MilvusClient
本次调用使用的 MilvusClient 实例;按值接收。
name string
名字(string)

返回值

bool — 是否成立

(MilvusClient) insert

XuAPI
pub fn (c MilvusClient) insert(collection string, entities []VectorEntity) bool

用途

MilvusClient 上的操作:插入。失败时抛出错误,调用方需处理或继续传播。

参数

c MilvusClient
本次调用使用的 MilvusClient 实例;按值接收。
collection string
collection(string)
entities []VectorEntity
entities([]VectorEntity)

返回值

bool — 是否成立

struct MilvusPool

XuAPI
pub struct MilvusPool {
    pub client MilvusClient
    pub state chan pool.State
    pub max_idle int
}

用途

Milvus 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client MilvusClient
字段(MilvusClient)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

open_pool

XuAPI
pub fn open_pool(endpoint string, max_idle int) MilvusPool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
max_idle int
max_idle(int)

返回值

MilvusPool — 该类型的值

查看用法示例

(&MilvusPool) dup

XuAPI
pub fn (p &MilvusPool) dup() MilvusPool

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。

返回值

MilvusPool — 该类型的值

(&MilvusPool) get_conn

XuAPI
pub fn (p &MilvusPool) get_conn() int

用途

&MilvusPool 上的操作:读取 conn。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&MilvusPool) put_conn

XuAPI
pub fn (p &MilvusPool) put_conn(fd int)

用途

&MilvusPool 上的操作:执行 put conn 操作。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&MilvusPool) discard_conn

XuAPI
pub fn (p &MilvusPool) discard_conn(fd int)

用途

&MilvusPool 上的操作:丢弃连接。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&MilvusPool) create_collection

XuAPI
pub fn (p &MilvusPool) create_collection(cfg CollectionConfig) bool

用途

&MilvusPool 上的操作:执行 create collection 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。
cfg CollectionConfig
cfg(CollectionConfig)

返回值

bool — 是否成立

(&MilvusPool) drop_collection

XuAPI
pub fn (p &MilvusPool) drop_collection(name string) bool

用途

&MilvusPool 上的操作:执行 drop collection 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

bool — 是否成立

(&MilvusPool) insert

XuAPI
pub fn (p &MilvusPool) insert(collection string, entities []VectorEntity) bool

用途

&MilvusPool 上的操作:插入。失败时抛出错误,调用方需处理或继续传播。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。
collection string
collection(string)
entities []VectorEntity
entities([]VectorEntity)

返回值

bool — 是否成立

(&MilvusPool) close

XuAPI
pub fn (p &MilvusPool) close()

用途

&MilvusPool 上的操作:关闭资源。

参数

p &MilvusPool
本次调用使用的 MilvusPool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct CollectionConfig

XuAPI
pub struct CollectionConfig {
    pub name string
    pub dimension int
    pub metric_type string // "COSINE", "L2", "IP"
}

用途

Milvus 配置参数与选项集合。

公开字段

name string
名字
dimension int
字段(int)
metric_type string // "COSINE", "L2", "IP"
字段(string // "COSINE", "L2", "IP")

查看用法示例

struct VectorEntity

XuAPI
pub struct VectorEntity {
    pub id int
    pub vector []string
    pub text string
}

用途

承载 id、vector、text 等属性的结构体。

公开字段

id int
字段(int)
vector []string
字段([]string)
text string
文本

查看用法示例

struct SearchHit

XuAPI
pub struct SearchHit {
    pub id int
    pub distance string
    pub text string
}

用途

承载 id、distance、text 等属性的结构体。

公开字段

id int
字段(int)
distance string
字段(string)
text string
文本

struct SearchResult

XuAPI
pub struct SearchResult {
    pub hits []SearchHit
}

用途

承载 hits 等属性的结构体。

公开字段

hits []SearchHit
字段([]SearchHit)