java教程

lucene 批量增加索引性能的提升

位置:首页 > java教程 > java技巧,2019-03-25 11:34
lucene 批量增加索引性能的提升,不用每加一条索引都提交

lucene 批量增加索引性能的提升,不用每加一条索引都提交


public String fulucene() throws Exception {
		
				
			 Directory directory = null;
         	  String rootlucene = ServletActionContext.getServletContext().getRealPath("/lucenexinwen/");
         	 IndexWriter writerlucene=null;
				try {
					directory = FSDirectory.open(new File(rootlucene));
					 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, new IKAnalyzer());
				   	  config.setOpenMode(OpenMode.CREATE_OR_APPEND);
				   	 writerlucene = new IndexWriter(directory, config);
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
			 
			 
	
	 
				 
				
					list = db.findPage(Xinwen.class,"select * from xinwen order by id desc",0,0);

					 Integer size=list.size();
					 Xinwen prdlucene;
			     for(int a=0;a<size;a++){
			    
				 
			    	 prdlucene=(Xinwen)list.get(a);
		
			    try {
			    	
					
					Document doc1 = new Document();
					doc1.add(new Field("jianjie", prdlucene.getJianjie(), Field.Store.YES,
							Field.Index.NO, Field.TermVector.NO));
					doc1.add(new Field("url", prdlucene.getUrl(), Field.Store.YES,
							Field.Index.NO, Field.TermVector.NO));
					doc1.add(new Field("title", prdlucene.getTitle(), Field.Store.YES,
							Field.Index.ANALYZED,
							Field.TermVector.WITH_POSITIONS_OFFSETS));
					doc1.add(new Field("id", prdlucene.getId().toString(), Field.Store.YES,
							Field.Index.NOT_ANALYZED));
					doc1.add(new Field("time", prdlucene.getTime(), Field.Store.YES,
							Field.Index.NO, Field.TermVector.NO));
					
					doc1.add(new Field("leixingid", prdlucene.getLeixingid().toString(), Field.Store.YES,
							Field.Index.NOT_ANALYZED));
					
					writerlucene.addDocument(doc1);
					//System.out.println("索引id:" + prdlucene.getId());

				} catch (Exception e) {
					System.out.println("创建索引失败:" + prdlucene.getId()+","+e.getMessage());
				}
			    
				
			 }
			
			     if(writerlucene!=null){
			    	 try {
			    		writerlucene.optimize();
			    	} catch (CorruptIndexException e) {
			    		// TODO Auto-generated catch block
			    		e.printStackTrace();
			    	} catch (IOException e) {
			    		// TODO Auto-generated catch block
			    		e.printStackTrace();
			    	}
			    		
			    		try {
			    			writerlucene.close();
			    		} catch (CorruptIndexException e) {
			    			// TODO Auto-generated catch block
			    			e.printStackTrace();
			    		} catch (IOException e) {
			    			// TODO Auto-generated catch block
			    			e.printStackTrace();
			    		}
			    	 }
				
				System.out.println("ok");
			

					//重设索引
		 
		 

				message = "创建索引成功";
		return super.execute();
		
	}


TAGS:lucene 批量增加索引

猜你喜欢

NewHot