Lucenc.net Full-Text Search create indexes, queries, pagination

Source: Internet
Author: User

 #region Create, follow the new Thesaurus///<summary>///Create, follow the new Thesaurus///</summary> private void CreateIndex Data () {//Index library folder fsdirectory dir = Fsdirectory.open (new DirectoryInfo (path), new Nativefsloc            Kfactory ());            Whether there is an index library bool has = indexreader.indexexists (dir);                    if (has) {//unlocks the locked index library if (indexwriter.islocked (dir)) {                Indexwriter.unlock (dir); }}//Index library Write object IndexWriter IW = new IndexWriter (dir, New Panguanalyzer (),!has, IndexWriter .            maxfieldlength.unlimited);            list<lucenemodel> list = LuceneBll.Instance.Get ();                foreach (var o in list) {document d = new Document (); Field.Store.YES: Store field value (field value before word breaker)//field.store.no: no storage, Storage is not related to indexes//field.store.cOmpress: Compressed storage for long text or binary, but with impaired performance//field.index ANALYZED;           Word Segmentation build index//field.index analyzed_no_norms;                Segmentation Compression Build index//field.index NO;                Field.index not_analyzed;       Non-participle index//field.index not_analyzed_no_norms;                                      Non-segmentation compression build index//field.termvector no;           Field.termvector with_offsets;         Storage offset//field.termvector with_positions; Storage location//field.termvector with_positions_offsets;                    Storage location and offset//field.termvector YES; Store the field's Termvector d.add ("Id", o.id.tostring (), Field.Store.YES, Field.Index.NOT_ANA for each document).                lyzed));                D.add (New Field ("Title", O.title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); D.add (New Field ("Message", O.message, Field.Store.YES, Field.index.                ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); Iw.            Adddocument (d); } IW.            Optimize (); Release the file IW.            Close (); Dir.        Close (); } #endregion
#region Enquiry///<summary>///</summary>//<param name= "str" > keywords to query </param>//<param name= "index" > page index (starting from 1) </param>//<param name= "Count" > number of Data bars per page & lt;/param>//<returns></returns> private list<lucenemodel> Search (string str, int ind            ex = 1, int count = 3) {list<lucenemodel> List = null;            Index Library folder Fsdirectory dir = Fsdirectory.open (new DirectoryInfo (path), new Nativefslockfactory ());            Whether there is an index library bool has = indexreader.indexexists (dir);            Does not exist then create if (!has) {//CREATE INDEX library createindexdata ();            }//Index library Read object Indexreader ir = Indexreader.open (dir, true);            Index Library Query Object Indexsearcher searcher = new Indexsearcher (IR);            Search criteria Phrasequery query = new Phrasequery (); //participle {Segment Segment = new Segment (); icollection<wordinfo> words = segment.                Dosegment (str); foreach (Var o in words) {query.                ADD (New term ("Message", O.tostring ())); }}//Specify the maximum distance between keywords and query.            Setslop (100);            Container TopScoreDocCollector collector = topscoredoccollector.create (Index * count, false) for the results of the query; Query searcher according to query criteria.            Search (query, NULL, collector); scoredoc[] Docs = Collector.            Topdocs (Count * (index-1), Count * (index-1) + count). Scoredocs; if (Docs.                Length > 0) {list = new list<lucenemodel> (); foreach (Var o in docs) {Document document = Searcher.                    Doc (O.doc);                    Lucenemodel m = new Lucenemodel (); M.id = Convert.ToInt32 (document.             Get ("Id"));       M.title = document.                    Get ("Title"); M.message = document.                    Get ("Message"); List.                ADD (m); }}//Release file IR.            Close (); Searcher.            Close (); Dir.            Close ();        return list; } #endregion


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.