如何在 Xapian 中使用不同的 id?
How to use different ids in Xapian?
我正在尝试使用 Xapian 执行搜索。我的文档有自己的 ID,即字符串。我已经按照教程说的做了:
db.replace_document(doc.docno, doc_x)
其中 doc.docno 是标识文档的字符串。
但是当我搜索时:
for match in enquire.get_mset(0, 10):
print match.document.get_docid()
恢复的docid只是一个简单的数字。
有人知道我是否需要做其他事情吗?
Xapian 文档 ID 始终是数字,但它提供了一种机制,让您可以按术语和 ID 来寻址文档。所以 replace_document()
和 delete_document()
可以被赋予一个字符串,就像你所做的那样,它们将找到所有匹配该术语的现有文档,并将它们从数据库中删除。 replace_document()
然后将创建一个新文档,re-using 最低匹配(数字)文档 ID,或者如果没有文档匹配则使用新 ID。
documentation for this variant of replace_document()
说:
One common use is to allow UIDs from another system to easily be mapped to terms in Xapian. Note that this method doesn't automatically add unique_term as a term, so you'll need to call document.add_term(unique_term)
first when using replace_document()
in this way.
如果您正在使用 QueryParser
,或者以其他方式遵循很多 Xapian 系统遵循的 term prefixing convention,那么通常使用 Q
作为前缀.这意味着您可能希望在调用 replace_document()
:
之前执行以下操作
doc_x.add_term('X' + doc.docno)
然后,当您查询数据库时,您将需要再次获取您的文档 ID。您可以通过读取术语列表来完成此操作,但这有点繁琐,因此更常见的是将您的 "external" id(Xapian 外部)存储在 Document
数据中。 (我经常将 JSON 存储在那里,为我需要的内容提供一些增长空间;例如,有时将呈现搜索结果所需的所有信息包含在文档数据中是很有用的。)
Xapian's FAQ on working with existing unique ids 中介绍了这种方法,特别是第二部分 "Using a term for the external unique id"。
我正在尝试使用 Xapian 执行搜索。我的文档有自己的 ID,即字符串。我已经按照教程说的做了:
db.replace_document(doc.docno, doc_x)
其中 doc.docno 是标识文档的字符串。 但是当我搜索时:
for match in enquire.get_mset(0, 10):
print match.document.get_docid()
恢复的docid只是一个简单的数字。 有人知道我是否需要做其他事情吗?
Xapian 文档 ID 始终是数字,但它提供了一种机制,让您可以按术语和 ID 来寻址文档。所以 replace_document()
和 delete_document()
可以被赋予一个字符串,就像你所做的那样,它们将找到所有匹配该术语的现有文档,并将它们从数据库中删除。 replace_document()
然后将创建一个新文档,re-using 最低匹配(数字)文档 ID,或者如果没有文档匹配则使用新 ID。
documentation for this variant of replace_document()
说:
One common use is to allow UIDs from another system to easily be mapped to terms in Xapian. Note that this method doesn't automatically add unique_term as a term, so you'll need to call
document.add_term(unique_term)
first when usingreplace_document()
in this way.
如果您正在使用 QueryParser
,或者以其他方式遵循很多 Xapian 系统遵循的 term prefixing convention,那么通常使用 Q
作为前缀.这意味着您可能希望在调用 replace_document()
:
doc_x.add_term('X' + doc.docno)
然后,当您查询数据库时,您将需要再次获取您的文档 ID。您可以通过读取术语列表来完成此操作,但这有点繁琐,因此更常见的是将您的 "external" id(Xapian 外部)存储在 Document
数据中。 (我经常将 JSON 存储在那里,为我需要的内容提供一些增长空间;例如,有时将呈现搜索结果所需的所有信息包含在文档数据中是很有用的。)
Xapian's FAQ on working with existing unique ids 中介绍了这种方法,特别是第二部分 "Using a term for the external unique id"。