我有 1.2M 行要索引,每行在 lucene 索引编写器中被读取为一个文档。建立索引后,我尝试断言已索引的记录总数。这个数字小于 1.2M。
添加文件的方法如下:
Directory fsDir = FSDirectory.open(this.indexLoc, NoLockFactory.INSTANCE);
IndexWriterConfig iwConf = new IndexWriterConfig(analyzer);
iwConf.setOpenMode(mode);
IndexWriter indexWriter = new IndexWriter(fsDir, iwConf);
int count=0;
FileInputStream input;
input = new FileInputStream(new File(String.valueOf(dir)));
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
InputStreamReader isr = new InputStreamReader(input,decoder);
BufferedReader reader = new BufferedReader(isr);
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
Document d = new Document();
d.add(new TextField(this.fieldName, line, Store.NO));
indexWriter.addDocument(d);
count++;
}
indexWriter.commit();
indexWriter.close();
reader.close();
isr.close();
input.close();
}
我获取索引文档编号的方式如下:
IndexReader reader = DirectoryReader.open(FSDirectory.open(this.indexLoc));
int docNum = reader.getDocCount(this.fieldName);
我查到1.2M已经添加到文件d中了。但是,为什么变量docNum的值小于1.2M呢?
当我用小尺寸文档测试时,比如 1k,这两个数字是一致的。
另外,我使用的是 lucene 5.0。
最佳答案
IndexReader.getDocCount(String field)
将返回对该字段具有至少一个术语 的文档数。因此,如果 line
为空,则文档计数不会增加 addDocument
。
关于java - lucene index getDocCount() 返回的值与总输入文档数不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29764342/