1. Creation of Lucene Index
1 /// <Contents>2 ///speak list data incoming CREATE INDEX3 /// </Contents>4 /// <param name= "DataList" ></param>5 /// <returns></returns>6 Public BOOLCreateIndex (list<luceneindexarticle>DataList)7 {8Fsdirectory directory = Fsdirectory.open (NewDirectoryInfo (Indexdic),Newnativefslockfactory ());9IndexWriter writer =NULL;Ten BOOLIsupdate =indexreader.indexexists (directory); One if(isupdate) A { - if(indexwriter.islocked (directory)) - { the indexwriter.unlock (directory); - } - } -writer =NewIndexWriter (Indexdic, Panguanalyzer,!isupdate, IndexWriter.MaxFieldLength.LIMITED);//false to append (true to delete previous write-back) + - foreach(Luceneindexarticle datainchDataList) + { A CreateIndex (writer, data); at } - writer. Optimize (); - writer. Close (); - return true; - } - Public BOOLCreateIndex (indexwriter writer, luceneindexarticle data) in { - Try to { + if(Data = =NULL)return false; -Document doc =NewDocument (); theType type =data. GetType (); *propertyinfo[] Propertys =type. GetProperties (); $ for(inti =0; i < propertys.length; i++)Panax Notoginseng { -PropertyInfo pi =Propertys[i]; the stringName =Pi. Name; + ObjectObjval = Pi. GetValue (data,NULL); A stringValue = Objval = =NULL?"": Objval. ToString ();//value the if(Name = ="ID")//ID in the write index must be non-participle, or fuzzy search and delete, there will be confusion + { -Doc. ADD (NewField (name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));//ID not participle $ } $ Else - { -Doc. ADD (NewField (name, value, Field.Store.YES, Field.Index.ANALYZED)); the } - }Wuyi writer. Adddocument (DOC); the } - Catch(System.IO.FileNotFoundException fnfe) Wu { - ThrowFnfe; About } $ return true; -}
Index storage directory and Pangu configuration file storage
1 /// <Contents>2 ///Index Store Directory3 /// </Contents>4 /// <Contents>5 ///Pangu participle configuration file6 /// </Contents>7 protected stringPanguxmlpath8 {9 GetTen { One return @"F:\DCIM\PanGu.xml"; A } -}
1 protected string Indexdic 2 {3 get4 {5 return @" F:\DCIM " ; 6 }7 }
Click Create Index
The index is created, and these are the generated disk files.
The next section is how to retrieve data from the index
Lucene Full Text Search-Starting from zero (2)