To build an array:
Private string[] ids = {"1", "2", "3", "4", "5", "6"};p rivate string[] emails = {"[email protected]", "[email protected]", "[ Email protected] "," [email protected] "," [email protected] "," [Email protected] "};p rivate string[] contents = {" Welcome to Visited the space ', ' hello,my name is SCC ', ' The day is first ', ' tommorw ', ' Hello World ', ' I like football '};p rivate int [] att Achs = {2,3,1,4,5,6};p rivate string[] names = {"Zhang San", "John Doe", "Harry", "Jetty", "Mick", "divID"};
To create a directory:
Private Directory directory;public Indexutil () {try {//create directorydirectory = Fsdirectory.open (New File ("d:/workspace/ Lucene/index02 "). Topath ());} catch (IOException e) {e.printstacktrace ();}}
To create a document, add a field to the document:
public void Index () {IndexWriter IndexWriter = null;try {indexwriter = new IndexWriter (directory, new Indexwriterconfig (NE W StandardAnalyzer ()));D ocument doc = null;for (int i = 0; i < ids.length; i++) {//Create document (document equivalent to each record in database table) Doc = new DOCU ment ();//Add a field to the document (the field is equivalent to each field in the table) Doc.add (new TextField ("id", Ids[i],field.store.yes));d Oc.add (New TextField ("email", Emails[i],field.store.yes)); TextField content = new TextField ("content", Contents[i],field.store.yes); Content.tokenstream (new StandardAnalyzer () , null);d Oc.add (content);d Oc.add (New TextField ("name", Names[i],field.store.yes));// Writes the document to the index indexwriter.adddocument (DOC);}} catch (IOException e) {e.printstacktrace ();} Finally{if (indexwriter!=null) {try {indexwriter.close ();} catch (IOException e) {e.printstacktrace ();}}}}
Query index:
/** * Query index */public void query () {Indexreader Indexreader = null;try {indexreader = directoryreader.open (directory);//Through RE Ader can get the number of documents SYSTEM.OUT.PRINTLN ("Numdosc:" +indexreader.numdocs ()); System.out.println ("Maxdocs:" +indexreader.maxdoc ());} catch (IOException e) {e.printstacktrace ();} Finally{if (Indexreader!=null) try {indexreader.close ();} catch (IOException e) {e.printstacktrace ();}}}
JUnit Test Run:
Deletion of the index:
/** * Delete index */public void Deleteindex () {IndexWriter IndexWriter = null;try {indexwriter = new IndexWriter (directory, New in Dexwriterconfig (New StandardAnalyzer ()));//The parameter is an option, either a query or a term, Term is an exact lookup of the value indexwriter.deletedocuments (new term ("id", "1")); catch (Exception e) {e.printstacktrace ();} Finally{if (Indexwriter!=null) try {indexwriter.close ();} catch (IOException e) {e.printstacktrace ();}}}
JUnit Test:
When the Deleteindex method is executed, the query index is executed again
Lucene Learning Note II (array-based Lucene retrieval, index deletion)