Lucene Full Text Search-Starting from zero (2)

Source: Internet
Author: User
Tags createindex

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)

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.