package cn.tz.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.LongField; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import org.wltea.analyzer.lucene.IKAnalyzer; public class IndexManagerTest { @Test public void testIndexCreate() throws Exception{ //采集文件系統中的文檔數據到Lucene中 //創建文檔列表 List<Document> docList=new ArrayList<Document>(); //指定文件目錄 File dir=new File("C:\\Users\\admin\\searchsource"); //循環文件夾 for(File file:dir.listFiles()){ String fileName = file.getName(); String fileContent=FileUtils.readFileToString(file); Long fileSize=FileUtils.sizeOf(file); //創建文檔對象 Document document=new Document(); TextField namefield=new TextField("fileName",fileName,Store.YES); TextField contentField=new TextField("fileContent",fileContent,Store.YES); LongField sizeField=new LongField("fileSize",fileSize,Store.YES); //LongField document.add(namefield); document.add(contentField); document.add(sizeField); docList.add(document); } //創建分詞器Analyzer // Analyzer analyzer=new StandardAnalyzer(); //采用第三方的中文分詞器 IKAnalyzer Analyzer analyzer=new IKAnalyzer(); //指定索引和文檔的存儲目錄 FSDirectory desFile=FSDirectory.open(new File("d:\\lucene")); //創建寫對象的初始化對象 IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_3,analyzer); //創建索引和文檔的寫對象 IndexWriter writer=new IndexWriter(desFile,config); //將文檔 加到索引和文檔的寫對象中 for(Document doc:docList){ writer.addDocument(doc); } //提交 writer.commit(); //關閉流 writer.close(); } @Test public void testIndexDel() throws Exception{ Analyzer analyzer=new IKAnalyzer(); FSDirectory dir=FSDirectory.open(new File("d:\\lucene")); IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer); IndexWriter writer=new IndexWriter(dir, config); //刪除所有索引 //writer.deleteAll(); //刪除指定索引(根據域刪除) //Term("域名","搜索的關鍵字") writer.deleteDocuments(new Term("fileName","apache")); //提交 writer.commit(); //關閉 writer.close(); } /** * 更新操作<br>: * <li>按照Term進行指定域搜索關鍵字,如果查到記錄就刪除,然后將更新后的內容重新生成Document對象</li> * <li>如果沒有查到記錄,則直接將更新后的內容添加一個Document對象</li> */ @Test public void testIndexUpdate() throws Exception{ Analyzer analyzer=new IKAnalyzer(); //存儲目錄 FSDirectory dir=FSDirectory.open(new File("d:\\lucene")); IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer); IndexWriter writer=new IndexWriter(dir,config); //按照fileName域進行搜索關鍵字"web" Term term=new Term("fileName","哦哦"); Document doc=new Document(); doc.add(new TextField("fileName","not exit",Store.YES)); doc.add(new LongField("fileSize",100L,Store.YES)); doc.add(new StringField("fileContent", "egfao容", Store.YES)); //更新 writer.updateDocument(term, doc); //提交 writer.commit(); //關閉 writer.close(); } }