package Java.se.lucene; //主类 import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.StaleReaderException; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.Version; public class index { private String[] ids={"1","2","3","4","5","6"}; private String[] emails={"[email protected]","[email protected]", "[email protected]","[email protected]","[email protected]","[email protected]"}; private String[] contents={"i like gdsfgfds","i like fsdfs","i like fdsfsd", "i like fdsfsd","i like like fdfs","i like like like fsefsdfg"}; private int[] attachs={1,2,3,4,5,6}; private String[] names={"liwu","zhangsan","xiaoqinag","laona", "dabao","lisi"}; private Directory directory=null; private IndexWriter writer=null; private Map<String,Float> scores=new HashMap<String,Float>(); public index() { try { // scores.put("aa.com", 2.0f); // scores.put("bb.com", 1.0f); // scores.put("cc.com", 3.0f); // scores.put("dd.com", 4.0f); scores.put("ee.com", 5.0f); scores.put("ff.com", 6.0f); directory=FSDirectory.open(new File("f:/lucene/Index04")); } catch (IOException e) { e.printStackTrace(); } } //建立索引 public void Index() { Document document=null; try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36))); writer.deleteAll();//更新索引 for(int i=0;i<ids.length;i++) { document=new Document(); document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS )); document.add(new Field("email",emails[i], Field.Store.YES,Field.Index.NOT_ANALYZED)); document.add(new Field("content", contents[i], Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("name",names[i], Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); String str=emails[i].substring(emails[i].lastIndexOf("@")+1); System.out.println(str); // if(scores.containsKey(str)) // { // document.setBoost(scores.get(str)); // }else{ // document.setBoost(0.5f); // } writer.addDocument(document); } } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //遍历各种视频 public void query() { try { IndexReader reader=IndexReader.open(directory); System.out.println("numdocs:"+reader.numDocs());//文档总数 System.out.println("maxDocs:"+reader.maxDoc());//可存储文章做大数目 System.out.println("detelemaxDocs:"+reader.numDeletedDocs()); reader.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //删除索引,但并没有完全删除,可以恢复的 public void delete() { try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36))); writer.deleteDocuments(new Term("id","1")); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //使用reader进行恢复 @SuppressWarnings("deprecation") public void undelete() { IndexReader reader = null; try { reader = IndexReader.open(directory,false); reader.undeleteAll(); reader.close(); } catch (StaleReaderException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //清空回收站 public void forceDelete() { try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36))); writer.forceMergeDeletes(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //已经停用 public void forceMerge() { try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36))); writer.forceMerge(3); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //更新索引 public void update() { Document document=null; try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36))); /* * lucene没有提供更新,只能先删除再添加 * */ for(int i=0;i<ids.length;i++) { document=new Document(); document.add(new Field("id", "11", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS )); document.add(new Field("email",emails[0], Field.Store.YES,Field.Index.ANALYZED)); document.add(new Field("content", contents[0], Field.Store.NO, Field.Index.NOT_ANALYZED)); document.add(new Field("name",names[0], Field.Store.YES,Field.Index.NOT_ANALYZED)); writer.updateDocument(new Term("id","1"), document); } } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //搜索 public void search() { IndexReader reader=null; try { reader = IndexReader.open(directory); IndexSearcher searcher=new IndexSearcher(reader); TermQuery query=new TermQuery(new Term("content","like")); TopDocs tds =searcher.search(query, 10); for(ScoreDoc sdc:tds.scoreDocs) { Document document=searcher.doc(sdc.doc); System.out.println("("+sdc.doc+")"+document.get("name")+"["+document.get("email")+ "]-->"+document.get("id")); } reader.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } // 测试类 package Java.se.lucene; import org.junit.Test; public class Test_Index { @Test public void test_index() //测试索引 { index ind=new index(); ind.Index(); } @Test public void test_query() //遍历 { index ind=new index(); ind.query(); } @Test public void test_delete() //删除索引 { index ind=new index(); ind.delete(); } @Test public void test_undelete() //恢复删除 { index ind=new index(); ind.undelete(); } @Test public void test_forceDelete() //清空回收站站 { index ind=new index(); ind.forceDelete(); } @Test public void test_forceMerge() //清空回收站站 { index ind=new index(); ind.forceMerge(); } @Test public void test_update() //更新索引 { index ind=new index(); ind.update(); } @Test public void test_search() //更新索引 { index ind=new index(); ind.search(); } /* public void check() throws IOException{ //检查索引是否被正确建立(打印索引) Directory directory = FSDirectory.open(new File("f:/lucene/Index04/"));//创建directory,其储存方式为在 IndexReader reader = IndexReader.open(directory); for(int i = 0;i<reader.numDocs();i++){ System.out.println(reader.document(i)); } reader.close(); } public static void main(String[] args) throws IOException { new index().check(); }*/ }