Lucene的deleteDocuments的一个疑问?

问题:

为什么调用了删除部分文档后,全部文档都找不到了?

项目已上传


索引文件:

/Users/mac/Lucene/data/data_04.txt

/Users/mac/Lucene/data/data_05.txt

/Users/mac/Lucene/data/data_02.txt

/Users/mac/Lucene/data/data_03.txt

/Users/mac/Lucene/data/data_01.txt

索引文档结构:

private Document getDocument(File file) throws IOException {

Document document = new Document();

// index file contents

Field contentField = new Field(Constants.CONTENTS, new FileReader(file));

// index file name

Field fileNameField = new Field(Constants.FILE_NAME, file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED);

// index file path

Field filePathField = new Field(Constants.FILE_PATH, file.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED);

// index file length

Field fileLengthField = new Field(Constants.FILE_LENGTH, file.length() + "", Field.Store.YES, Field.Index.NOT_ANALYZED);

document.add(contentField);

document.add(fileNameField);

document.add(filePathField);

document.add(fileLengthField);

return document;

}

创建文档方法:

private void indexFile(File file) throws IOException {

log.info("Indexing -> " + file.getCanonicalPath());

Document document = getDocument(file);

writer.addDocument(document);

}

查询文档方法:

public TopDocs searchUsingPrefixQuery(String searchQuery) throws IOException {

Term term = new Term(Constants.FILE_NAME, searchQuery);

Query query = new PrefixQuery(term);

return indexSearcher.search(query, Constants.MAX_SEARCH);

}

删除文档方法:

private void deleteFile(String name) throws IOException {

writer.deleteDocuments(new PrefixQuery(new Term(Constants.FILE_NAME, name)));

writer.commit();

}

测试入口及输出:

public static void main(String[] args) {

Test tester;

try {

tester = new Test();

tester.createIndex();

tester.search("data");

tester.deleteIndex("data_01.txt");

tester.search("data");

} catch (IOException e) {

e.printStackTrace();

} catch (ParseException e) {

e.printStackTrace();

}

}

20:08:12.624 [main] INFO com.neo.code.lucene.demo01.Test - [searching files]: 5

20:08:12.634 [main] INFO com.neo.code.lucene.demo01.Test - [searching files]: 0

以上是 Lucene的deleteDocuments的一个疑问? 的全部内容, 来源链接: utcz.com/p/944119.html

回到顶部