Mvc+mq+winservices+lucene.net Demo

Source: Internet
Author: User

Objective:

I have not been in touch with lucene.net knowledge before, recently in the garden to see many great gods sharing this piece of content, deeply inspired. Faith "Practice out of the truth" spirit, and then combined with the actual situation of the project, with the idea of writing a demo, is a test of their ability.

Function Description:

1. The foreground website serializes the new Index item object (title, content) and sends it to MQ

2. When MQ receives the message, it persists and then pushes the message to the consumer

3. The message's consumer (winservices) receives the message, deserializes it into an indexed item object, and invokes the CREATE index method of the SearchEngine class library

4. The foreground website calls the query method of the SearchEngine class library, and passes in the user input keyword, and displays the results of the matching query on the view.

Note:

1. In order to simulate multiple users at the same time Add Index Item object, the Internet itself is a multi-threaded environment. This uses the ACTIVEMQ queue pattern (in addition to the theme mode, which is mainly used for message broadcast scenarios), because it maintains a first-in, out-of-the-line queue, guaranteeing that only one message can be received at a time, and all other waiting queues need to be queued.

2. The idea of distributed projects is introduced here, the foreground site only duplicates new index entries and queries, MQ is responsible for receiving and pushing messages, and winservices is responsible for generating index files.

3. Because still only demo, so many functions are not perfect, from the real enterprise-level applications there is a big gap, the purpose is to practice practiced hand, familiar with the relevant knowledge points.

Flow chart:

Architecture diagram:

Hierarchy Chart:

Project structure:

Lucenetest.entity: Class libraries that define index entries and query result classes

LUCENETEST.MQ: Class library for encapsulating Message Queuing (ActiveMQ) send and Receive functions

Lucenetest.web: MVC Project for managing index entries and queries

LuceneTest.WinService.Test: WinForm Engineering for WinService testing

Lucenetest.searchengine: Encapsulates Lucene.Net's creation index and class library based on keyword queries

Key Code Snippet:

1         /// <summary>2         ///Create an index3         /// </summary>4         /// <param name= "model" ></param>5          Public voidCreateIndex (Indexset model)6         {7             //Open Index document save location8             varDirectory = Fsdirectory.open (NewDirectoryInfo ( This. _indexpath),Newnativefslockfactory ());9             //Indexreader: Class for reading the index libraryTen             varIsexist =indexreader.indexexists (directory); One  A             if(isexist) -             { -                 //unlocked if the index directory is locked (such as when the program exits abnormally during indexing or another process is operating the index library) the                 if(indexwriter.islocked (directory)) -                     //Manual Unlocking - indexwriter.unlock (directory); -             } +  -             //Create a write operation object to the index library, indexwriter (index directory, specifying the cut word using pangu participle, maximum write length limit) +             //Add: The index library file is automatically locked when you open directory with IndexWriter A             varwriter =NewIndexWriter (Directory,NewPanguanalyzer (),!isexist, IndexWriter.MaxFieldLength.UNLIMITED); at             //Create a new Document object, a record corresponding to a document in the index library -             varDocument =NewDocument (); -  -             //Add a field to a document -             //the values for all fields are saved as String types because the index library stores only String type data -  in             //Field.store: Whether to store the original text: -             //Field.Store.YES: Store original value (if display original content must be YES), you can use Document.get to remove the original value to             //Field.Store.NO: Does not store the original value +             //Field.Store.COMPRESS: Compressed storage -  the             //Field.index: Whether to create an index: *             //Field.Index.NOT_ANALYZED: Do not create an index $             //Field.Index.ANALYZED: Creating an index (facilitates retrieval)Panax Notoginseng  -             //with_positions_offsets: Indicates not only to save the segmented words, but also to save the distance between the words theDocument. ADD (NewField ("title", model. Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); +Document. ADD (NewField ("content", model. Content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); A  the             //document write to index library + writer. Adddocument (document); -  $             //will automatically unlock $ writer. Close (); -             //do not forget close, otherwise the index results cannot be searched - directory. Close (); the}
        /// <summary>        ///Enquiry/// </summary>        /// <param name= "KeyWord" ></param>        /// <returns></returns>         PublicList<searchresult> Search (stringKeyWord) {            varSearchresultlist =NewList<searchresult>(); //Open Index document save location            varDirectory = Fsdirectory.open (NewDirectoryInfo ( This. _indexpath),Newnolockfactory ()); //Indexreader: Class for reading the index library            varReader = Indexreader.open (directory,true); //keyword participle            varWords = This.            Splitwords (KeyWord); //Search Criteria            varquery =NewPhrasequery (); foreach(varIteminchwords) {Query. ADD (NewTerm ("content", item)); }            //specify the maximum distance between keywordsQuery. Setslop ( -); //TopScoreDocCollector: Container to store query results            varCollector = Topscoredoccollector.create ( +,true); //Indexreader: Class for querying the index library            varSearcher =NewIndexsearcher (reader); //Query according to query criteria, query results into the collector containerSearcher. Search (Query,NULL, collector); //Topdocs: Specify 0 to Gettotalhits (), which is the document in all query results, if Topdocs (20,10) means to get the contents of the document between 第20-30, to achieve the effect of paging            varDocs = Collector. Topdocs (0, collector.            Gettotalhits ()). Scoredocs; foreach(varIteminchdocs) {                varSearchResult =NewSearchResult (); //gets the ID of the query result document (Lucene internally assigned ID)                varDocId =Item.doc; //obtaining Document Object documents based on document ID                varDoc =Searcher.                Doc (DOCID); Searchresult.id=docId; Searchresult.title= Doc. Get ("title"); //Highlight DisplaySearchresult.content = This. Hightlight (KeyWord, Doc. Get ("content"));            Searchresultlist.add (SearchResult); }            returnsearchresultlist; }

Query Page view

@{Viewbag.title="Search";}@using (Html.BeginForm ("Search","Indexmgr")){    <div>Key Words:</div> <div>@Html. TextBox ("KeyWord")    </div> <input type="Submit"Value="Save"/>}@{    varList = This.    Viewbag.searchresultlist; if(List! =NULL)    {        foreach(varIteminchlist) {@Html. Raw ("Title:"+item. Title)<br/>@Html. Raw ("content:"+item. Content)        }    }}

Precautions:

1. If you use the Pangu segmentation algorithm, the "Copy to output directory" of the following files needs to select "Copy if newer"

2. The index file of this demo is saved under the Winservices executable directory (bin\debug\indexdata), so the foreground website needs to be queried and the path of the index file need to be configured.

Run:

1. New index entries

2. Enquiry

Reference documents:

Http://www.cnblogs.com/jiekzou/p/4364780.html

Http://www.cnblogs.com/piziyimao/archive/2013/01/31/2887072.html

Mvc+mq+winservices+lucene.net Demo

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.