1 Interface 1.1 screenshot:
Principles: simplification, visualization, explaining problems, useful
1.2 Description:
1 Creating an index on a specified folder
2 input keywords, display query results
2 Implementation 2.1 Creation
Button1_Click method:
//indexing a set up folder Private voidButton1_Click (Objectsender, EventArgs E1) {System.IO.FileInfo Docdir=NewSystem.IO.FileInfo (TextBox1.Text); System.DateTime start=System.DateTime.Now; Try { //Index builder IndexWriter, standard analyzer StandardAnalyzerIndexWriter writer =NewIndexWriter (Fsdirectory.open (index_dir),NewStandardAnalyzer (Version.lucene_current),true, IndexWriter.MaxFieldLength.LIMITED); Indexdocs (writer, docdir); Writer. Optimize (); Writer. Close (); System.DateTime End=System.DateTime.Now; intTotalMilliseconds = End.millisecond-Start.millisecond; MessageBox.Show ("Index Complete! Total"+ TotalMilliseconds +"milliseconds"); } Catch(System.IO.IOException e) {MessageBox.Show ("caught a"+ e.gettype () +"\ n with message:"+e.message); } }
View Code
Indexdocs Method:
//Traverse the directory to index each documentInternalStaticvoidIndexdocs (indexwriter writer, System.IO.FileInfo file) {//Do not try to index files, cannot be read//if (File.canread ())//{{Aroush}} what's CanRead () in C #?{if (System.IO.Directory.Exists (file. FullName))//Directory recursion{system.string[] files =System.IO.Directory.GetFileSystemEntries (file. FullName);//An IO error could occurif (Files! =Null) {for (int i =0; I < files. Length; i++) {Indexdocs (writer,NewSystem.IO.FileInfo (Files[i])); } } }Else// index the file "adding "+ file); try {writer. Adddocument (filedocument.document (file)); // //// checking if the file can be read doesn ' t help catch
View Code
2.2 Queries
Button2_Click method
//Enquiry Private voidButton2_Click (Objectsender, EventArgs e) {dataGridView1.Rows.Clear (); System.String KeyWord=TextBox2.Text; System.String Index="Index"; System.String Field="Contents"; System.String Normsfield=NULL; //1 Initializing reader, searcherIndexreader reader = Indexreader.open (Fsdirectory.open (NewSystem.IO.FileInfo (index)),true);//Only searching, so read-only=true if(Normsfield! =NULL)//Normative StandardsReader =NewOnenormsreader (reader, Normsfield); Searcher Searcher=NewIndexsearcher (reader);//Search DeviceAnalyzer Analyzer =NewStandardAnalyzer (version.lucene_current); Queryparser Parser=NewQueryparser (field, analyzer);//keyword parser: Specify Type field, Word breaker analyzer//2 parsing keywords if(KeyWord = =NULL|| Keyword.length = =-1) return; KeyWord=Keyword.trim (); if(Keyword.length = =0) return; Query Query=parser. Parse (KeyWord); System.Console.Out.WriteLine ("Searching for:"+query. ToString (field)); //3 Get query results hitsHits Hits = searcher. Search (query);//Hits for(inti =0; I < hits. Length (); i++) {Document doc=hits. Doc (i); stringFullName = doc. Get ("Path");//# # #key stringName = Fullname.substring (Fullname.lastindexof ("\\") +1, Fullname.length-1-Fullname.lastindexof ("\\")); DataGridViewRow Dr=NewDataGridViewRow (); intIndex1 =DataGridView1.Rows.Add (DR); DATAGRIDVIEW1.ROWS[INDEX1]. cells[0]. Value =name; DATAGRIDVIEW1.ROWS[INDEX1]. cells[1]. Value =FullName; } searcher. Close (); Reader. Close (); }
View Code
3 Summary 3.1 methods:
1 Progressive: Logic--Level 1--Level 2
2 Seizing the core://### #key
3 Source code Comment segmentation, expected guessing
3.2 Points of Knowledge:
1 Build Index: indexwriter.adddocument (filedocument.document (file));
2 Parse Keyword: query query = parser. Parse (KeyWord);
3 Get query results: Hits Hits = searcher. Search (query);
3.3 Mentality:
Natural flow, not the pursuit of perfection.
Cycle exercises, gradually approaching, form a template.
VS Remove items does not cause deletion
4 Next
Document Summary
Chinese support
(a) Simple search