The general index structure establishes a ing relationship between documents and words, while the inverted index creates a ing relationship between words and documents. In daily searches, the search is usually based on keywords. Therefore, inverted indexes can be better suited to the needs of such search mechanisms. This is why inverted indexes are widely used today.
Buildindex
ImportJava. Io. file;ImportJava. Io. ioexception;ImportJava. util. date;ImportOrg. Apache. Lucene. analysis. simpleanalyzer;ImportOrg. Apache. Lucene. Demo. filedocument;ImportOrg. Apache. Lucene. Index. indexwriter;Public ClassBuildindex {Public Static VoidMain (string [] ARGs ){// Timing
Date start =NewDate ();Try{// Create an index directoryIndexwriter writer =NewIndexwriter ("C: \ indexdir",NewSimpleanalyzer (),True);// Indexed Text DocumentFile file =NewFile ("C: \ indexdata.txt");// Add the document to the index type and optimize it
System. Out. println ("Adding"+ File); writer. adddocument (filedocument. Document (File); writer. Optimize (); writer. Close ();}Catch(Ioexception e) {e. printstacktrace ();}// End Time
Date end =NewDate ();//
System. Out. Print (end. gettime ()-start. gettime (); system. Out. println ("Total milliseconds");}}
Dosearch
ImportOrg. Apache. Lucene. Store .*;ImportOrg.apache.e.doc ument .*;ImportOrg. Apache. Lucene. analysis .*;ImportOrg. Apache. Lucene. Index .*;ImportOrg. Apache. Lucene. Search .*;ImportOrg. Apache. Lucene. queryparser .*;ClassDosearch {Public Static VoidMain (string [] ARGs ){Try{// Create an index
Directory directory =NewRamdirectory ();//Generates analyzer objects for Word Segmentation and so on.
Analyzer analyzer =NewSimpleanalyzer ();//Index writer
Indexwriter writer =NewIndexwriter (directory, analyzer,True);//Create an index
String [] docs = {"A B C D E","A B c d e","A B c d e f g h I j","A C E","E C","A C E","A C E A B C"};For(IntJ = 0; j <docs. length; j ++) {document D =NewDocument (); D. Add (field. Text ("Contents", Docs [J]); writer. adddocument (d);} writer. Close ();//Generate search object
Searcher searcher =NewIndexsearcher (directory );//Generate a result set object and initialize it to a null value.
String [] queries = {"\ "A c e \"",};// |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%? ? Limit 1 ?? ¥ ??? ? 3? ?? ¥? A ??? ZookeeperHits hits =Null;//Generate queryparser object, Word Segmentation
Queryparser parser =NewQueryparser ("Contents", Analyzer );// Use the query string in sequence to generate the query object query
For(IntJ = 0; j <queries. length; j ++) {query = parser. parse (queries [J]); system. Out. println ("Query:"+ Query. tostring ("Contents"));//Result set
Hits = searcher. Search (query );//Output the total number of searched documents: system. Out. println (hits. Length () +"Total results");// Output the searched documents in sequence
For(IntI = 0; I "+ Hits. Score (I) +" "+ D. Get ("Contents") ;}} Searcher. Close ();}Catch(Exception e) {system. Out. println ("Caught"+ E. getclass () +"\ N with message:"+ E. getmessage ());}}}
Select <conquer Ajax and Lucene framework Search>.
Please refer to the following link for more information when necessary. You can download the complete PDF file from Baidu Library:
Http://wenku.baidu.com/search? WORD = % D5 % F7 % B7 % feajax % 20 Lucene & lm = 0 & OD = 0