Lucene 4.0

关于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(); }

 

posted on 2013-11-19 08:41  无觉-李敏  阅读(332)  评论(1编辑  收藏  举报