gpt4 book ai didi

java - 如何使用字节数组作为 Lucene 索引字段?

转载 作者:行者123 更新时间:2023-11-30 07:55:26 24 4
gpt4 key购买 nike

我将 Lucene 索引从使用字符串文档 id 切换为字节数组。我遇到的问题是系统不再通过 ID 查找文档。我怀疑这是因为 lucene 代码没有执行 Array.equals(),而是执行标准 equals()。这是添加文档的代码:

Document doc = new Document();
byte[] key = indexData.getKey().toByteArray();
System.out.println(Arrays.toString(key));
doc.add(new StoredField(DOCUMENT_PRIMARY_KEY, new BytesRef(key)));
writer.addDocument(doc);

这是删除文档的代码。删除失败,因为找不到文档(尽管索引中确实存在)。

void prepareDelete(byte[] documentId) throws IOException {
System.out.println(Arrays.toString(documentId));
Term term =
new Term(DOCUMENT_PRIMARY_KEY, new BytesRef(documentId));
writer.deleteDocuments(term);
}

通过比较 print 语句的输出,我确定键是相同的(从某种意义上说,它们包含相同的字节),但它们不共享标识。

我使用的是 Lucene 4.10.3。

最佳答案

来自 Lucene 邮件列表上发布的答案:

You are indexing your field as a StoredField which means it's not actually indexed (just stored), so no query (nor IW.deleteDocument) will ever be able to find it.

Try StringField instead ... in recent versions you can pass a BytesRef value to that.

我更新到了 Lucene 5.3,其中 StringField 有一个采用 BytesRef 值的构造函数,这解决了问题。

关于java - 如何使用字节数组作为 Lucene 索引字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742909/

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