XU XUv0.16.0

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

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

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

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

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

示例代码 · API 声明

示例代码

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

51_standalone_meilisearch/main.xu

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

Xu本地 SDK 示例
@main

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

fn verify_meilisearch_pool_and_models() {
    println("--- 1. Testing Meilisearch Protocol & Pool ---")
    $p := meilisearch.open_pool("http://127.0.0.1:7700", "masterKey", 5)

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

    fd2 := p.get_conn()
    if fd2 < 0 {
        println("   [FAIL] Failed to re-borrow Meilisearch 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)

    // 3. Real Operations via Pool
    h := p.health()
    if h {
        println("   [PASS] Meilisearch health check: available")
    } else {
        println("   [FAIL] Meilisearch health check failed")
    }

    p.create_index("books", "id") >>> (err) {
        println("   [FAIL] Failed to create index: " + err.msg())
        p.close()
        <<
    }
    docs := "[{\"id\":101,\"title\":\"Antigravity Architecture\"},{\"id\":102,\"title\":\"Compiler Design\"}]"
    p.add_documents("books", docs) >>> (err) {
        println("   [FAIL] Failed to add documents: " + err.msg())
        p.close()
        <<
    }
    println("   [PASS] Meilisearch index & docs added via pool")

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

fn main() {
    println("=== Xu Meilisearch Native Driver Verification ===\n")
    verify_meilisearch_pool_and_models()
    println("\n[MEILISEARCH VERIFICATION 100% COMPLETED]")
}

API 声明

client

XuAPI
pub fn client(endpoint string, api_key string) MeiliClient

用途

客户端。

参数

endpoint string
endpoint(string)
api_key string
api_key(string)

返回值

MeiliClient — 该类型的值

pool

XuAPI
pub fn pool(endpoint string, api_key string, max_conns int) MeiliPool

用途

执行 pool 处理。

参数

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

返回值

MeiliPool — 该类型的值

struct MeiliClient

XuAPI
pub struct MeiliClient {
    pub host string
    pub port int
    pub secure bool
    pub api_key string
}

用途

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

公开字段

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

new_client

XuAPI
pub fn new_client(endpoint string, api_key string) MeiliClient

用途

构造 client。

参数

endpoint string
endpoint(string)
api_key string
api_key(string)

返回值

MeiliClient — 该类型的值

(MeiliClient) connect

XuAPI
pub fn (c MeiliClient) connect() int

用途

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

参数

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

返回值

int — 该类型的值

(MeiliClient) do_request_on_fd

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

用途

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

参数

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

返回值

string — 结果字符串

(MeiliClient) do_request

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

用途

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

参数

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

返回值

string — 结果字符串

(MeiliClient) health

XuAPI
pub fn (c MeiliClient) health() bool

用途

在 MeiliClient 上执行 health 操作。

参数

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

返回值

bool — 是否成立

(MeiliClient) create_index

XuAPI
pub fn (c MeiliClient) create_index(uid string, primary_key string) bool

用途

MeiliClient 上的操作:创建下标。失败时抛出错误,调用方需处理或继续传播。

参数

c MeiliClient
本次调用使用的 MeiliClient 实例;按值接收。
uid string
uid(string)
primary_key string
primary_key(string)

返回值

bool — 是否成立

(MeiliClient) delete_index

XuAPI
pub fn (c MeiliClient) delete_index(uid string) bool

用途

MeiliClient 上的操作:删除下标。失败时抛出错误,调用方需处理或继续传播。

参数

c MeiliClient
本次调用使用的 MeiliClient 实例;按值接收。
uid string
uid(string)

返回值

bool — 是否成立

(MeiliClient) add_documents

XuAPI
pub fn (c MeiliClient) add_documents(uid string, json_docs string) bool

用途

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

参数

c MeiliClient
本次调用使用的 MeiliClient 实例;按值接收。
uid string
uid(string)
json_docs string
json_docs(string)

返回值

bool — 是否成立

struct MeiliPool

XuAPI
pub struct MeiliPool {
    pub client MeiliClient
    pub state chan pool.State
    pub max_idle int
}

用途

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

公开字段

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

open_pool

XuAPI
pub fn open_pool(endpoint string, api_key string, max_idle int) MeiliPool

用途

执行 open pool 操作。

参数

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

返回值

MeiliPool — 该类型的值

查看用法示例

(&MeiliPool) dup

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

用途

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

参数

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

返回值

MeiliPool — 该类型的值

(&MeiliPool) get_conn

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

用途

&MeiliPool 上的操作:读取 conn。

参数

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

返回值

int — 该类型的值

(&MeiliPool) put_conn

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

用途

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

参数

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

返回值

不返回值。

(&MeiliPool) discard_conn

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

用途

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

参数

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

返回值

不返回值。

(&MeiliPool) health

XuAPI
pub fn (p &MeiliPool) health() bool

用途

在 &MeiliPool 上执行 health 操作。

参数

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

返回值

bool — 是否成立

(&MeiliPool) create_index

XuAPI
pub fn (p &MeiliPool) create_index(uid string, primary_key string) bool

用途

&MeiliPool 上的操作:创建下标。失败时抛出错误,调用方需处理或继续传播。

参数

p &MeiliPool
本次调用使用的 MeiliPool 实例;以只读借用传入,调用方仍持有实例。
uid string
uid(string)
primary_key string
primary_key(string)

返回值

bool — 是否成立

(&MeiliPool) delete_index

XuAPI
pub fn (p &MeiliPool) delete_index(uid string) bool

用途

&MeiliPool 上的操作:删除下标。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

bool — 是否成立

(&MeiliPool) add_documents

XuAPI
pub fn (p &MeiliPool) add_documents(uid string, json_docs string) bool

用途

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

参数

p &MeiliPool
本次调用使用的 MeiliPool 实例;以只读借用传入,调用方仍持有实例。
uid string
uid(string)
json_docs string
json_docs(string)

返回值

bool — 是否成立

(&MeiliPool) close

XuAPI
pub fn (p &MeiliPool) close()

用途

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

参数

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

返回值

不返回值。

struct SearchHit

XuAPI
pub struct SearchHit {
    pub id int
    pub title string
    pub raw string
}

用途

承载 id、title、raw 等属性的结构体。

公开字段

id int
字段(int)
title string
字段(string)
raw string
字段(string)

struct SearchResult

XuAPI
pub struct SearchResult {
    pub query string
    pub total_hits int
    pub hits []SearchHit
}

用途

承载 query、total_hits、hits 等属性的结构体。

公开字段

query string
字段(string)
total_hits int
字段(int)
hits []SearchHit
字段([]SearchHit)