MVC + MQ + WinServices + Lucene. Net Demo,

Source: Internet
Author: User

MVC + MQ + WinServices + Lucene. Net Demo,

Preface:

I have never been familiar with Lucene. Net before. Recently, many great gods have been reading this article in the garden, which is very enlightening. Adhering to the spirit of "practice with real knowledge", combined with the actual situation of the company's projects, we have the idea of writing a Demo, which is a test of our own capabilities.

 

Function Description:

1. After the front-end website serializes the newly added index item object (title, content), it sends it to MQ

2. After receiving the message, MQ persists and then pushes it to the consumer of the message.

3. After receiving the message, the message consumer (WinServices) deserializes the message into an index object and calls the index creation method of the SearchEngine class library.

4. The front-end website calls the query method of the SearchEngine class library, passes in the keywords entered by the user, and displays the matching results on The View.

Note:

1. to simulate the simultaneous addition of index item objects by multiple users, the Internet itself is a multi-threaded environment. The ActiveMQ queue mode is used here (In addition, the topic mode is mainly used for message broadcast scenarios), because it maintains an FIFO queue internally, it can ensure that only one message can be received at a time, and all other messages to be received must be queued for waiting.

2. This introduces the idea of distributed projects. The foreground website only copies new index items and queries, MQ receives and pushes messages, and WinServices generates index files.

3. Because it is only a Demo, many functions are not perfect, and there is still a big gap between the real enterprise-level applications. The purpose is to train your hands and familiarize yourself with relevant knowledge points.

 

Flowchart:

 

Architecture diagram:

 

Hierarchy chart:

 

Project Structure:

Deleetest. Entity: class library that defines index items and query results

Receivetest. MQ: class library that encapsulates the send and receive functions of the Message Queue (ActiveMQ ).

LuceneTest. Web: MVC project used to manage index items and query

LuceneTest. WinService. Test: WinForm project used for WinService Testing

LuceneTest. SearchEngine: stores indexes created by Lucene. Net and class libraries queried Based on keywords.

 

Key code snippets:

1 /// <summary> 2 /// create index 3 /// </summary> 4 /// <param name = "model"> </param> 5 public void createIndex (IndexSet model) 6 {7 // open the index file storage location 8 var directory = FSDirectory. open (new DirectoryInfo (this. _ indexPath), new NativeFSLockFactory (); 9 // IndexReader: 10 var isExist = IndexReader. indexExists (directory); 11 12 if (isExist) 13 {14 // if the INDEX directory is locked (for example, the program unexpectedly exits during the indexing process or another process is operating on the index database ), unlock 15 if (IndexWrite R. isLocked (directory) 16 // manually unlock 17 IndexWriter. unlock (directory); 18} 19 20 // create the write operation object to the index database, IndexWriter (INDEX directory, specifying the use of pangu word segmentation for word segmentation, maximum write length limit) 21 // supplement: When you use IndexWriter to open directory, the index library file will be automatically locked 22 var writer = new IndexWriter (directory, new PanGuAnalyzer (),! IsExist, IndexWriter. maxFieldLength. UNLIMITED); 23 // create a document object. A record corresponds to a Document in the index database 24 var document = new Document (); 25 26 // Add Field 27 to the document // The values of all fields will be saved as strings, because the index Library only stores string data 28 29 // Field. store: whether to Store the original article: 30 // Field. store. YES: store the original value (if the original content must be YES), you can use document. get to retrieve the original value 31 // Field. store. NO: Do not store the original value 32 // Field. store. COMPRESS: Compressed Storage 33 34 // Field. index: whether to create an Index: 35 // Field. index. NOT_ANALYZED: do not create an index 36 // Field. index. ANALYZED: index creation (for retrieval) 37 38 // WITH_POSITIONS_OFFSETS: indicates not only saving the separated words, but also saving the distance between words 39 documents. add (new Field ("title", model. title, Field. store. YES, Field. index. ANALYZED, Field. termVector. WITH_POSITIONS_OFFSETS); 40 document. add (new Field ("content", model. content, Field. store. YES, Field. index. ANALYZED, Field. termVector. WITH_POSITIONS_OFFSETS); 41 42 // write the document to the index database 43 writer. addDocument (document); 44 45 // 46 writer is automatically unlocked. close (); 47 // do not forget to Close, otherwise the index results cannot find 48 directory. close (); 49}
/// <Summary> /// query /// </summary> /// <param name = "keyWord"> </param> /// <returns> </ returns> public List <SearchResult> Search (string keyWord) {var searchResultList = new List <SearchResult> (); // open the index file storage location var directory = FSDirectory. open (new DirectoryInfo (this. _ indexPath), new NoLockFactory (); // IndexReader: Class var reader = IndexReader for reading the index library. open (directory, true); // keyword segmentation var words = this. splitWords (keyWord); // Search Condition var query = new PhraseQuery (); foreach (var item in words) {query. add (new Term ("content", item);} // specify the maximum distance between keywords and query. setSlop (100); // TopScoreDocCollector: the container that stores the query results var collector = TopScoreDocCollector. create (1000, true); // IndexReader: Class var searcher = new IndexSearcher (reader) for querying the index database; // query based on the query conditions, put the query result into the collector container searcher. search (query, null, collector); // TopDocs: Specify 0 to GetTotalHits (), that is, all documents in the query results. If TopDocs (20, 10) this means to get the document content between 20-30 to achieve the paging effect var docs = collector. topDocs (0, collector. getTotalHits ()). scoreDocs; foreach (var item in docs) {var searchResult = new SearchResult (); // the id of the query result document (the id allocated within Lucene) var docId = item.doc; // obtain the Document Object Document var doc = searcher based on the Document id. doc (docId); searchResult. id = docId; searchResult. title = doc. get ("title"); // The searchResult is highlighted. content = this. hightLight (keyWord, doc. get ("content"); searchResultList. add (searchResult);} return searchResultList ;}

View

@ {ViewBag. title = "Search" ;}< h2> Search List 

 

Note:

1. If you use the pangu word segmentation algorithm, select "Copy to output directory" for the following files"

2. The index file of this Demo is saved under the executable directory (bin \ Debug \ IndexData) of WinServices. Therefore, you must configure the index file path for the front-end website to query.

 

Run:

1. Add an index

2. Query

 

References:

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

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

 

Related Article

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.