Lucene.Net the problem of solving the lock

Source: Internet
Author: User
Tags createindex

1    Public Sealed classSearchindexmanager2     {3         Private Static ReadOnlySearchindexmanager searchmanager=NewSearchindexmanager ();4         PrivateSearchindexmanager ()5         {6         7         }8          Public StaticSearchindexmanager Getsinglesearchmanager ()9         {Ten             returnSearchmanager; One         } A         PrivateQueue<searchcontent> Indexqueue =NewQueue<searchcontent>(); -         /// <summary> -         ///Add index the         /// </summary> -          Public voidAddindex (searchcontent indexcontent) -         { -Indexcontent. Indextype =Createindexenum. ADD; + Indexqueue.enqueue (indexcontent); -         } +         /// <summary> A         ///Delete Index at         /// </summary> -         /// <param name= "indexcontent" ></param> -          Public voidDeleteindex (searchcontent indexcontent) -         { -Indexcontent. Indextype =Createindexenum. Delete; - Indexqueue. Enqueue (indexcontent); in         } -         //Add the appropriate index without entering a single piece of data, in order to solve the problem of the lock, using the queue to add data to the queue, the next step is to extract the data from the queue, added to the Lucen Index library to          Public voidStartindexthread () +         { -Thread thread =NewThread (NewThreadStart (CreateIndex)); theThread. IsBackground =true; * thread. Start (); $         }Panax Notoginseng          Public voidCreateIndex () -         { the              while(true) +             { A                 if(Indexqueue.count >0) the                 { +                     //Create an index -                     //Place the created word breaker in the directory $                     stringIndexpath =@"D:\LuceneDir"; $                     //Specify an index file for lucent.net (Open index directory) FS means filesystem. -Fsdirectory directory = Fsdirectory.open (NewDirectoryInfo (Indexpath),Newnativefslockfactory ()); -                     //Indexreader: The class that reads the index. The purpose of this statement is to determine whether the index library folder exists and whether the index signature file exists.  the                     BOOLIsupdate =indexreader.indexexists (directory); -                     if(isupdate)Wuyi                     { the                         //only one piece of code can write to the index library at the same time. When you open Directory with IndexWriter, the index library files are automatically locked.  -                         //if the index directory is locked (for example, the program exits unexpectedly during indexing), unlock it first (hint: If I'm writing now that it's locked, but I haven't finished it yet, then another request, then not unlocked?) This problem will be resolved later) Wu                         if(indexwriter.islocked (directory)) -                         { About indexwriter.unlock (directory); $                         } -                     } -                     //writes an index to the index library. This is where the lock is added. At the same time specify the word segmentation algorithm is Pangu participle -                     //The index will be written to the folder directory we set up through writer. AIndexWriter writer =NewIndexWriter (Directory,NewPanguanalyzer (),!isupdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED); +  the                     //fetching data from the queue -Searchcontent search=indexqueue.dequeue (); $                         //we need to add a file to the Lucene.Net index library theDocument document =NewDocument ();//represents a document.  the                         //Field.Store.YES: Indicates whether the original value is stored. The value can only be fetched with Doc.get ("number") when the Field.Store.YES is in the back. Field.index. not_analyzed: Do not save the word breaker the                         //because there is no overwrite, you need to make the previous delete flag, or there will be multiple duplicate theWriter. Deletedocuments (NewTerm ("ID", Search.id. ToString ())); -  in                         if(Search. Indextype = =createindexenum.delete) the                         { the                             Continue; About                         } theDocument. ADD (NewField ("ID", Search.id. ToString (), Field.Store.YES, Field.Index.NOT_ANALYZED)); the  the                         //field.index. ANALYZED: Word breaker: That is, to make a full-text field to set the word breaker (because you want to make a fuzzy query) +  -                         //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS: Not only save the word, but also save the distance.  the BayiDocument. ADD (NewField ("title", search. Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS)); theDocument. ADD (NewField ("content", search. Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS)); the writer. Adddocument (document); -  -Writer. Close ();//is automatically unlocked.  theDirectory. Close ();//do not forget close, otherwise the index results cannot be searched the                 } the                 Else the                 { -Thread.Sleep ( the); the                 } the             } the}

Lucene.Net the problem of solving the lock

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.