zoukankan      html  css  js  c++  java
  • 5.Lucene修改索引

    在我们存储的数据更新了时,我们要把索引和文档页更新,这时我们就可以使用修改索引的方式来更新。

    修改索引和创建索引一致,只需要把 writer.addDocument(document); 改成下面的即可:

    // 把文档根据条件更新到IndexWriter
    writer.updateDocument(new Term("id", "1"), document);
    

    修改索引本质上是在末尾添加一条新纪录,删除旧的记录的过程,并不是真的去修改记录。

    Term 表示根据什么条件去更新记录,id这个字段就是我们添加设置的,这个id不能重复,不然符合条件的记录都会被删除,只会保留你更新的这条记录。

    附录:完整代码

    @Test
    public void updateIndex() {
        // lucene索引目录位置
        String indexDir = "E:\develop\demo\lucene-learn\lucene-index";
        File luceneIndexDirectory = new File(indexDir);
        // 打开索引目录
        try (FSDirectory fsd = FSDirectory.open(luceneIndexDirectory.toPath())) {
            // 创建索引写入
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new IKAnalyzer());
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
            IndexWriter writer = new IndexWriter(fsd, indexWriterConfig);
    
            // 定义字段
            // 1. id:存储、索引、不分词
            FieldType idFieldType = new FieldType();
            idFieldType.setStored(true);
            idFieldType.setTokenized(false);
            idFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
            // 2. title:存储、分词、索引
            FieldType titleFieldType = new FieldType();
            titleFieldType.setStored(true);
            titleFieldType.setTokenized(true);
            titleFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
            // 3. content:存储、分词、索引
            FieldType contentFieldType = new FieldType();
            contentFieldType.setStored(true);
            contentFieldType.setTokenized(true);
            contentFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    
            // 创建文档对象,把字段内容写入到文档对象
            Document document = new Document();
            document.add(new Field("id", "1", idFieldType));
            document.add(new Field("title", "el-dialog里面的组件不刷新问题、梦幻诛仙", titleFieldType));
            document.add(new Field("content", "el-dialog里面的内容是带缓存的,也就是说除了第一次打开会初始化,其他次打开都是直接加载缓存的。我们想每次打开el-dialog都要初始化,比如用户的权限弹框,每次打开我们都要把用户拥有的权限选中,类似的操作,如果加载了缓存,这就会导致每个用户回填的权限都一样了。 如果我们在每次关闭弹框时把弹框里面的内容删掉,这样打开时就会初始化了。", contentFieldType));
    
            // 把文档根据条件更新到IndexWriter
            writer.updateDocument(new Term("id", "1"), document);
    
            // 提交保存索引
            writer.flush();
            writer.commit();
            writer.close();
        } catch (IOException e) {
            System.err.println("打开索引目录失败");
            e.printStackTrace();
        }
    }
    
  • 相关阅读:
    User Get 'Access Denied' with Excel Service WebPart
    How To Search and Restore files from Site Collection Recycle Bin
    How To Collect ULS Log from SharePoint Farm
    How To Restart timer service on all servers in farm
    How to Operate SharePoint User Alerts with PowerShell
    How to get Timer Job History
    Synchronization Service Manager
    SharePoint 2007 Full Text Searching PowerShell and CS file content with SharePoint Search
    0x80040E14 Caused by Max Url Length bug
    SharePoint 2007 User Re-created in AD with new SID issue on MySite
  • 原文地址:https://www.cnblogs.com/lixingwu/p/13870592.html
Copyright © 2011-2022 走看看