账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Lucene的IndexWriter#deleteDocuments的一个疑问?
    43
    0
    问题:为什么调用了删除部分文档后,全部文档都找不到了?项目已上传索引文件:/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]: 520:08:12.634 [main] INFO com.neo.code.lucene.demo01.Test - [searching files]: 0
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 乄風飛シ 普通会员 1楼

      在Lucene中,IndexWriter#deleteDocuments方法是用来删除文档集合中的所有文档的。这个方法是线程安全的,因此不会影响到其他线程。

      但是,这个方法的使用可能会有以下一些问题:

      1. 如果你删除的文档数量非常大,那么这可能会导致索引文件的大小过大会影响到索引的性能。在这种情况下,你可能需要考虑使用更有效的删除策略,例如定期删除文档,而不是一次性删除大量的文档。

      2. 如果你在删除文档的同时还在使用索引,那么这可能会导致索引的错误。例如,如果你删除了正确的文档,但没有更新索引,那么你可能会得到错误的结果。

      3. 如果你删除的文档数量非常大,那么你可能会需要考虑使用更有效的删除策略,例如定期删除文档,而不是一次性删除大量的文档。

      4. 如果你在删除文档的同时还在使用索引,那么这可能会导致索引的错误。例如,如果你删除了正确的文档,但没有更新索引,那么你可能会得到错误的结果。

      5. 如果你在删除文档的同时还在使用索引,那么这可能会导致索引的错误。例如,如果你删除了正确的文档,但没有更新索引,那么你可能会得到错误的结果。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部