← 中文资源

简体中文版本

向量数据库

向量数据库存储嵌入向量并回答最近邻查询,是语义检索与检索增强生成背后的核心原语。

阅读英文原文

概念概述

向量数据库(Vector Databases)指的是:向量数据库存储嵌入向量并回答最近邻查询,是语义检索与检索增强生成背后的核心原语。

从更严格的定义看,A vector database is a system specialised for storing high-dimensional vectors and efficiently finding the vectors closest to a query vector under a chosen distance metric. 该概念在工程实践中的价值,在于它能帮助团队用统一的语言描述一个具体的质量属性或设计约束,从而在评审与协作中减少歧义。

为什么需要它

It indexes embeddings produced by models so applications can run similarity search at scale. 它与 Semantic Search、Text Embeddings、Tokenization and Subword Units 等概念经常一起出现,用于解决相近的工程问题。理解它的适用场景,有助于在多种备选方案之间做出有依据的取舍,而不是凭直觉选择技术方案。

常见误解

  • exact nearest neighbour is always affordable
  • cosine similarity and Euclidean distance always rank identically

需要避免的反模式

  • choosing an index without measuring recall against the workload
  • storing raw vectors without metadata for filtering

实践要点

  1. Approximate nearest-neighbour indexes trade a small recall loss for large speed gains, so recall must be measured against a brute-force baseline for the real data.
  2. Distance metric matters: normalised vectors are often compared by cosine or dot product, and mixing metrics between indexing and querying breaks ranking.
  3. Metadata filtering and hybrid search, combining vectors with keyword conditions, are usually needed because pure similarity ignores hard constraints.

参考来源

https://en.wikipedia.org/wiki/Vector_database

代码示例

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcDatabaseQuery {

    record User(int id, String name, String email) {}

    public static List<User> findActiveUsers(String url, String user, String pass) {
        String sql = "SELECT id, name, email FROM users WHERE active = ? ORDER BY name";

        List<User> users = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(url, user, pass);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setBoolean(1, true);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    users.add(new User(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("email")
                    ));
                }
            }
        } catch (SQLException e) {
            System.err.println("Database error: " + e.getMessage());
        }
        return users;
    }

    public static void main(String[] args) {
        System.out.println("JDBC query example ready to connect");
    }
}