关于4.0的Update Index  ,Create Index

    /*
     * Create Index     
     */
    public static void createIndex() throws IOException{

        try {
            Directory  directory=FSDirectory.open(new File(indexPath));
            IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_44,analyzer);
            config.setOpenMode(OpenMode.CREATE_OR_APPEND);
            IndexWriter writer=new IndexWriter(directory,config);
             Document document=new Document();
             document.add(new org.apache.lucene.document.TextField("content",strBuilder.toString(),Store.YES));
             document.add(new org.apache.lucene.document.StringField("path", indexPath, Store.YES)); 
             document.add(new org.apache.lucene.document.StringField("name", "lucene", Store.YES)); 
             writer.addDocument(document);   
             writer.close();  
            
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("created index fail");
            e.printStackTrace();
        }
    }

 


/** * update Index * */ public static void updateIndex() throws Exception{ Document document = new Document(); iW_config=new IndexWriterConfig(Version.LUCENE_44,analyzer); iW_config.setOpenMode(OpenMode.CREATE_OR_APPEND); directory=FSDirectory.open(new File(indexPath)); writer=new IndexWriter(directory,iW_config); document=searchDocument("name","lucene"); document.removeField("name"); //更新所以必须在Document中删除了才能奇效 document.add(new StringField("name", "anewfile", Store.YES)); writer.updateDocument(new Term("name","lucene"),document);//此处要指定他的值和类型 writer.commit(); writer.close(); }

 

相关文章:

  • 2021-05-27
  • 2022-01-27
  • 2021-05-23
  • 2021-12-04
  • 2021-06-28
猜你喜欢
  • 2021-07-27
  • 2021-12-12
  • 2022-01-27
  • 2022-12-23
  • 2021-09-30
  • 2021-07-31
相关资源
相似解决方案