查询索引数据的修改索引

/**
     * 更新索引
     * 本质先删除再添加
     * 先删除所有满足条件的文档,再创建文档
     * 因此,更新索引通常要根据唯一字段
     * @throws IOException
     */
    @Test
    public void testUpdate() throws IOException{
         
        // 创建文档对象
        Document document = new Document();
        document.add(new StringField("id", "9", Store.YES));
        document.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Store.YES));
         
        // 索引库对象
        Directory directory = FSDirectory.open(new File("C:\\tmp\\index"));
        // 索引写入器配置对象
        IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());
        // 索引写入器对象
        IndexWriter indexWriter = new IndexWriter(directory, conf);
         
        // 执行更新操作
        indexWriter.updateDocument(new Term("id", "1"), document);
        // 提交
        indexWriter.commit();
        // 关闭
        indexWriter.close();
         
    }

猜你喜欢

转载自blog.csdn.net/qq_40208605/article/details/89632415