Lucene index Delete, restore, update

Source: Internet
Author: User
Package org.se.lucene;
Main class import Java.io.File;
Import java.io.IOException;
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.Term;
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 Lucene_index {private string[] ids={"1", "2", "3", "4", "5", "6"};
		Private string[] emails={"aa@aa.org", "cc@cc.org", "dd@ @dd. org", "bb@bb.org", "ee@ee.org", "ff@ff.org"};
		Private string[] contents={"Welcometotyu", "Hellowboy", "Higirl", "howareyou", "Googluck", "Badgosh"};
		Private int[] attachs={1,2,3,4,5,6}; Private string[] names={"Liwu", "Zhangsan","Xiaoqinag", "Laona", "Dabao", "Lisi"};
		
		Private Directory Directory=null;
			Public Lucene_index () {try {directory=fsdirectory.open (new File ("f:/lucene/index02"));
			catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
				} public void Quary () {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 (); @SuppressWarnings ("deprecation") public void undelete () {try {//Reply) must set Reader's read-only to False Ind
				Exreader Reader=indexreader.open (Directory,false);
				Reader.undeleteall ();
			Reader.close (); catch (CorruptindexexcePtion e) {//TODO auto-generated catch block E.printstacktrace ();
			catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
			}//Empty the Recycle Bin, forcing the optimization of the public void Forcedelete () {IndexWriter writer=null; try {writer=new IndexWriter (directory, new Indexwriterconfig (version.lucene_36, New StandardAnalyzer (version.lu
				cene_36)));
			Parameter 11 options, can be a query, can also be a term term is an exact lookup value//The deleted document is not completely deleted, but is stored in the Recycle Bin, can be restored writer.forcemergedeletes ();
			catch (Corruptindexexception e) {e.printstacktrace ();
			catch (Lockobtainfailedexception e) {e.printstacktrace ();
			catch (IOException e) {e.printstacktrace ();
					} finally{if (writer!=null) {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 Merge () {IndexWriter writer=null; try {writer=new IndexWriter (directory, new Indexwriterconfig (version.lucene_36, New StandardAnalyzer (version.lu
				
				cene_36)));
			Writer.forcemerge (2);
			catch (Corruptindexexception e) {e.printstacktrace ();
			catch (Lockobtainfailedexception e) {e.printstacktrace ();
			catch (IOException e) {e.printstacktrace ();
					} finally{if (writer!=null) {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 Delete () {IndexWriter writer=null; try {writer=new IndexWriter (directory, new Indexwriterconfig (version.lucene_36, New StandardAnalyzer (version.lu
				cene_36))); Parameter 11 options, can be a query, can also be a term term is an exact lookup value//The deleted document is not completely deleted, but is stored in the RecycleStation, can restore the writer.deletedocuments (new Term ("id", "1"));
			catch (Corruptindexexception e) {e.printstacktrace ();
			catch (Lockobtainfailedexception e) {e.printstacktrace ();
			catch (IOException e) {e.printstacktrace ();
					} finally{if (writer!=null) {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 Index () {IndexWriter writer=null;
			   Document Doc=null; try {writer =new indexwriter (directory,new indexwriterconfig (version.lucene_36, New StandardAnalyzer (Versio
				n.lucene_36)));
				Writer.deleteall ();
			    	for (int i=0;i<ids.length;i++) {doc=new Document ();
			    	Doc.add (New Field ("id", ids[i],field.store.yes,field.index.not_analyzed_no_norms)); Doc.add (New Field ("emails", Emails[i],field.store.yes,field.index.not_analyzed));
			    	Doc.add (New Field ("Contents", contents[i],field.store.yes,field.index.analyzed));
			    	Doc.add (New Field ("name", names[i],field.store.yes,field.index.not_analyzed_no_norms)); 
				Writer.adddocument (DOC);
			The 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{if (writer!=null) {try {writer.close ();
					catch (Corruptindexexception e) {//TODO auto-generated catch block E.printstacktrace ();
					catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); "}}//update index public void Update () {/*lucene itself does not support update * * * Update by deleting index and then indexing
			  IndexWriter Writer=null;
			   Document Doc=null; try {writer =new indexwriter (directory,new indexwriterconfig (version.lucene_36, New StandardAnalyzer (Versio
				n.lucene_36)));
				Writer.deleteall ();
			    	for (int i=0;i<ids.length;i++) {doc=new Document ();
			    	Doc.add (New Field ("id", ids[i],field.store.yes,field.index.not_analyzed_no_norms));
			    	Doc.add (New Field ("emails", emails[i],field.store.yes,field.index.not_analyzed));
			    	Doc.add (New Field ("Contents", contents[i],field.store.yes,field.index.analyzed));
			    	Doc.add (New Field ("name", names[i],field.store.yes,field.index.not_analyzed_no_norms)); 
				Writer.updatedocument (New Term ("id", "1"), doc);
			The 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{if (writer!=null) {try {writer.close ();
					catch (Corruptindexexception e) {//TODO auto-generated catch block E.printstacktrace ();
					catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();

}}}//test class package org.se.lucene;

Import Org.junit.Test;
		public class Test {@Test public void Testindex () {lucene_index l_index=new lucene_index ();
    L_index.index ();
		@Test public void Testquary () {lucene_index l_index=new lucene_index ();
	L_index.quary ();
		@Test public void Testdelete () {lucene_index l_index=new lucene_index ();
	L_index.delete ();
		@Test public void Testundelete () {lucene_index l_index=new lucene_index ();
	L_index.undelete ();
		@Test public void Testforcedelete () {lucene_index l_index=new lucene_index ();
	L_index.forcedelete (); @Test public void Testmerge () {lucene_inDex l_index=new Lucene_index ();
	L_index.merge ();
		@Test public void UpDate () {lucene_index l_index=new lucene_index ();
	L_index.update ();
 }
	
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.