gpt4 book ai didi

lucene - 如何使用 Lucene Hnsw Graph 运行最近邻搜索

转载 作者:行者123 更新时间:2023-12-05 04:39:03 25 4
gpt4 key购买 nike

我想使用 Lucene 运行最近邻搜索。我在 JVM 11 上使用 Lucene 9.0.0。我没有找到太多文档,主要尝试使用现有的 tests 来拼凑东西。 .

我编写了一个准备HnswGraph 的小测试,但到目前为止搜索没有产生预期的结果。我设置了一组随机向量并添加了一个非常接近我的搜索目标的最终向量 (0.99f,0.01f)。不幸的是,搜索从未返回预期值。我不确定我的错误在哪里。我认为这可能与插入和文档 ID 顺序有关。

也许对lucene比较熟悉的人可以提供一些反馈。我的方法正确吗?我仅将文档用于持久性。

HnswGraphBuilder builder = new HnswGraphBuilder(vectors, similarityFunction, maxConn, beamWidth, seed);
HnswGraph hnsw = builder.build(vectors);

// Run a search
NeighborQueue nn = HnswGraph.search(
new float[] { 1, 0 },
10,
10,
vectors.randomAccess(), // ? Why do I need to specify the graph values again?
similarityFunction, // ? Why can I specify a different similarityFunction for search. Should that not be the same that was used for graph creation?
hnsw,
null,
new SplittableRandom(RandomUtils.nextLong()));

完整的测试源可以在这里找到: https://gist.github.com/Jotschi/cea21a72412bcba80c46b967e9c52b0f

最佳答案

我设法让它工作。

我现在不直接使用 HnswGraph API,而是使用 LeafReader#searchNearestVectors。在调试时,我注意到 Lucene90HnswVectorsWriter 使用 HnswGraph API 调用额外的步骤。我假设这样做是为了在插入的向量和文档 ID 之间建立关联。我使用 HnswGraph#search 检索到的 nodeId 从未与向量 ID 匹配。我不知道是否需要额外的步骤来设置图形,或者是否需要以某种方式在之后创建相关性。

好消息是 LeafReader#searchNearestVectors 方法有效。我更新了现在也使用 Lucene 文档的示例。

@Test
public void testWriteAndQueryIndex() throws IOException {
// Persist and read the data
try (MMapDirectory dir = new MMapDirectory(indexPath)) {
// Write index
int indexedDoc = writeIndex(dir, vectors);
// Read index
readAndQuery(dir, vectors, indexedDoc);
}
}

具有 [0.97|0.02] 的矢量 7 非常接近搜索查询目标 [0.98|0.01]。

Test vectors:
0 => [0.13|0.37]
1 => [0.99|0.49]
2 => [0.98|0.57]
3 => [0.23|0.64]
4 => [0.72|0.92]
5 => [0.08|0.74]
6 => [0.50|0.27]
7 => [0.97|0.02]
8 => [0.90|0.21]
9 => [0.89|0.09]
10 => [0.11|0.95]

Doc Based Search:
Searching for NN of [0.98 | 0.01]
TotalHits: 11
7 => [0.97|0.02]
9 => [0.89|0.09]

完整示例: https://gist.github.com/Jotschi/d8a91758c84203d172f818c8be4964e4

关于lucene - 如何使用 Lucene Hnsw Graph 运行最近邻搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70477808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com