跳到主要内容
版本:V1.1.0

通过 SQL 快速体验混合搜索

seekdb 支持通过 SDK 或者 SQL 方式使用。本文介绍如何通过 SQL 快速体验混合搜索。

信息

有关通过 SDK 使用 seekdb 的介绍,参见 通过 Python SDK 体验嵌入式 seekdb

在本示例中,我们将进行以下操作:

  1. 部署服务器模式 seekdb。
  2. 连接到 seekdb。
  3. 创建数据库。
  4. 创建表并插入数据。
  5. 执行混合搜索。
  6. 进行搜索优化。
  7. 清理环境。

部署模式

seekdb 提供了多种灵活的部署模式,从快速原型开发到支撑海量用户,全面满足您的应用需求。

  • 嵌入式模式

    seekdb 以轻量级库的形式嵌入您的应用,支持 pip 一键安装。适用于个人学习、快速原型开发,并可高效运行于多种端侧设备。

  • 服务器模式

    推荐用于测试及生产环境的部署模式,轻量易用,适合稳定高效地提供服务。

信息

关于 seekdb 更详细和更全面的部署方式,参见 部署概述

步骤一:部署服务器模式 seekdb

前提条件

  • 您使用的环境是 RPM 平台系统,目前已验证支持的系统如下。

    • Anolis OS 8.X 版本(内核 Linux 4.19 版本及以上)

    • Anolis OS 23.X 版本(内核 Linux 6.6 版本及以上)

    • Centos Linux 7.X、9.X 版本(内核 Linux 4.19 版本及以上)

    • openEuler 22.03 和 24.03 版本(内核 Linux 5.10.0 版本及以上)

  • 当前环境中 CPU 最低要求 1 核。

  • 当前环境中的可用内存最低要求 2G。

  • 您环境中已安装 MySQL 客户端。

  • 您使用的用户已具有执行 sudo 命令的权限。

  • 您环境中已安装 jq 命令行工具,并正确配置了 systemd 作为系统和服务管理器。

快速部署 seekdb 环境

  1. 安装 seekdb。

    curl -fsSL https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/download-center/opensource/seekdb/seekdb_install.sh | sudo bash
  2. 启动 seekdb。

    sudo systemctl start seekdb
  3. 查看 seekdb 的启动状态。

    sudo systemctl status seekdb

    当状态显示为 Service is ready 时,表示 seekdb 已正常启动。

步骤二:连接 seekdb

使用 MySQL 客户端连接 seekdb,具体连接命令如下:

mysql -h127.0.0.1 -uroot -P2881 -p****** -A

步骤三:创建数据库

创建名为 my_test 的数据库。您也可以选择使用 seekdb 的默认数据库 test 进行体验。

  1. 创建数据库。

    create database my_test;
  2. 切换到自定义数据库。

    use my_test;

步骤四:创建示例表并插入数据

  1. 创建一个包含标量列、向量列和全文索引列的文档表,用于演示全文搜索带标量过滤条件和混合搜索。

    CREATE TABLE doc_table(
    c1 INT,
    vector VECTOR(3),
    query VARCHAR(255),
    content VARCHAR(255),
    VECTOR INDEX idx1(vector) WITH (distance=l2, type=hnsw, lib=vsag),
    FULLTEXT INDEX idx2(query),
    FULLTEXT INDEX idx3(content)
    )
    ORGANIZATION HEAP;
  2. 插入数据。

    INSERT INTO doc_table VALUES
    (1, '[1,2,3]', "hello world", "seekdb Elasticsearch database"),
    (2, '[1,2,1]', "hello world, what is your name", "seekdb database"),
    (3, '[1,1,1]', "hello world, how are you", "seekdb mysql database"),
    (4, '[1,3,1]', "real world, where are you from", "postgres mysql database"),
    (5, '[1,3,2]', "real world, how old are you", "redis mysql database"),
    (6, '[2,1,1]', "hello world, where are you from", "starrocks seekdb database");

步骤五:执行混合搜索

  1. 设置搜索参数。

    SET @parm = '{
    "query": {
    "query_string": {
    "fields": ["query", "content"],
    "query": "hello oceanbase"
    }
    },
    "knn" : {
    "field": "vector",
    "k": 5,
    "query_vector": [1,2,3]
    }
    }';
  2. 执行查询并返回查询结果。

    SELECT json_pretty(DBMS_HYBRID_SEARCH.SEARCH('doc_table', @parm)); 

    返回结果如下:

    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | json_pretty(DBMS_HYBRID_SEARCH.SEARCH('doc_table', @parm)) |
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | [
    {
    "c1": 1,
    "query": "hello world",
    "_score": 1.3716216216216217,
    "vector": "[1,2,3]",
    "content": "seekdb Elasticsearch database"
    },
    {
    "c1": 2,
    "query": "hello world, what is your name",
    "_score": 0.6646586312048193,
    "vector": "[1,2,1]",
    "content": "seekdb database"
    },
    {
    "c1": 3,
    "query": "hello world, how are you",
    "_score": 0.6593354613375797,
    "vector": "[1,1,1]",
    "content": "seekdb mysql database"
    },
    {
    "c1": 5,
    "query": "real world, how old are you",
    "_score": 0.41421356,
    "vector": "[1,3,2]",
    "content": "redis mysql database"
    },
    {
    "c1": 6,
    "query": "hello world, where are you from",
    "_score": 0.3503184713375797,
    "vector": "[2,1,1]",
    "content": "starrocks seekdb database"
    },
    {
    "c1": 4,
    "query": "real world, where are you from",
    "_score": 0.30901699,
    "vector": "[1,3,1]",
    "content": "postgres mysql database"
    }
    ] |
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set

步骤六:进行搜索优化

在混合搜索中,可以通过 boost 参数调整全文搜索和向量搜索的权重比例,以优化搜索结果。例如提高全文搜索的权重:

  1. 设置搜索参数。

    SET @parm = '{
    "query": {
    "query_string": {
    "fields": ["query", "content"],
    "query": "hello oceanbase",
    "boost": 2.0
    }
    },
    "knn" : {
    "field": "vector",
    "k": 5,
    "query_vector": [1,2,3],
    "boost": 1.0
    }
    }';
  2. 执行查询并返回查询结果。

    SELECT json_pretty(DBMS_HYBRID_SEARCH.SEARCH('doc_table', @parm));

    返回结果如下:

    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | json_pretty(DBMS_HYBRID_SEARCH.SEARCH('doc_table', @parm)) |
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | [
    {
    "c1": 1,
    "query": "hello world",
    "_score": 1.7432432432432434,
    "vector": "[1,2,3]",
    "content": "seekdb Elasticsearch database"
    },
    {
    "c1": 3,
    "query": "hello world, how are you",
    "_score": 1.0096539326751595,
    "vector": "[1,1,1]",
    "content": "seekdb mysql database"
    },
    {
    "c1": 2,
    "query": "hello world, what is your name",
    "_score": 0.9959839324096386,
    "vector": "[1,2,1]",
    "content": "seekdb database"
    },
    {
    "c1": 6,
    "query": "hello world, where are you from",
    "_score": 0.7006369426751594,
    "vector": "[2,1,1]",
    "content": "starrocks seekdb database"
    },
    {
    "c1": 5,
    "query": "real world, how old are you",
    "_score": 0.41421356,
    "vector": "[1,3,2]",
    "content": "redis mysql database"
    },
    {
    "c1": 4,
    "query": "real world, where are you from",
    "_score": 0.30901699,
    "vector": "[1,3,1]",
    "content": "postgres mysql database"
    }
    ] |
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set

通过调整 boost 参数,可以控制关键词搜索和语义搜索在最终排序中的权重。例如,如果更关注关键词匹配,可以提高 query_stringboost 值;如果更关注语义相似性,可以提高 knnboost 值。

步骤七:清理环境

如果您不再需要上述示例数据库和表,可以执行以下操作删除。

  1. 删除示例表。

    DROP TABLE doc_table;
  2. 删除自定义数据库。

    DROP DATABASE my_test;

更多操作

您可以进一步体验 seekdb 的 AI Native 特性以及尝试基于 seekdb 搭建 AI 应用: