我有一些文档存储在带有docId字段的Lucene索引中。 我想将所有docId存储在索引中。还有一个问题。文件数量约为30万,所以我更愿意将这个文件放在500块大小的文件中。是否可以这样做?
答案 0 :(得分:47)
IndexReader reader = // create IndexReader
for (int i=0; i<reader.maxDoc(); i++) {
if (reader.isDeleted(i))
continue;
Document doc = reader.document(i);
String docId = doc.get("docId");
// do something with docId here...
}
答案 1 :(得分:16)
Lucene 4
Bits liveDocs = MultiFields.getLiveDocs(reader);
for (int i=0; i<reader.maxDoc(); i++) {
if (liveDocs != null && !liveDocs.get(i))
continue;
Document doc = reader.document(i);
}
有关详细信息,请参阅此页面上的LUCENE-2600:https://lucene.apache.org/core/4_0_0/MIGRATE.html
答案 2 :(得分:6)
有一个名为MatchAllDocsQuery
的查询类,我认为在这种情况下可以使用它:
Query query = new MatchAllDocsQuery();
TopDocs topDocs = getIndexSearcher.search(query, RESULT_LIMIT);
答案 3 :(得分:2)
文档编号(或ID)将是从0到IndexReader.maxDoc() - 1的后续数字。这些数字不是持久的,仅对打开的IndexReader有效。您可以使用IndexReader.isDeleted(int documentNumber)方法检查文档是否已删除
答案 4 :(得分:0)
如果您使用上述示例中的.document(i)并跳过已删除的文档,请谨慎使用此方法进行分页结果。 即:你有10个文档/每页列表,你需要获得文档。对于第6页。您的输入可能是这样的:offset = 60,count = 10(文档从60到70)。
IndexReader reader = // create IndexReader
for (int i=offset; i<offset + 10; i++) {
if (reader.isDeleted(i))
continue;
Document doc = reader.document(i);
String docId = doc.get("docId");
}
删除的内容会有一些问题,因为你不应该从offset = 60开始,而是从offset = 60 + 60之前出现的已删除文档的数量开始。
我找到的替代方案是这样的:
is = getIndexSearcher(); //new IndexSearcher(indexReader)
//get all results without any conditions attached.
Term term = new Term([[any mandatory field name]], "*");
Query query = new WildcardQuery(term);
topCollector = TopScoreDocCollector.create([[int max hits to get]], true);
is.search(query, topCollector);
TopDocs topDocs = topCollector.topDocs(offset, count);
注意:用[[]]替换自己的值。 以150万条目在大型指数上进行此操作,并在不到一秒的时间内随机获得10个结果。 同意速度较慢,但如果您需要分页,至少可以忽略已删除的文档。