Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper structuring of Lucene.Net usage in an ASP.NET MVC site

I'm building an ASP.NET MVC site where I plan to use Lucene.Net. I've envisioned a way to structure the usage of Lucene, but not sure whether my planned architecture is OK and efficient.


My Plan:

  • On Application_Start event in Global.asax: I check for the existence of the index on the file system - if it doesn't exist, I create it and fill it with documents extracted it from the database.
  • When new content is submitted: I create an IndexWriter, fill up a document, write to the index, and finally dispose of the IndexWriter. IndexWriters are not reused, as I can't imagine a good way to do that in an ASP.NET MVC application.
  • When content is edited: I repeat the same process as when new content is submitted, except that I first delete the old content and then add the edits.
  • When a user searches for content: I check HttpRuntime.Cache to see if a user has already searched for this term in the last 5 minutes - if they have, I return those results; otherwise, I create an IndexReader, build and run a query, put the results in HttpRuntime.Cache, return them to the user, and finally dispose of the IndexReader. Once again, IndexReaders aren't reused.

My Questions:

  • Is that a good structure - how can I improve it?
  • Are there any performance/efficiency problems I should be aware of?
  • Also, is not reusing the IndexReaders and IndexWriters a huge code smell?
like image 766
Maxim Zaslavsky Avatar asked Aug 13 '10 22:08

Maxim Zaslavsky


People also ask

Which of these state management techniques helps to maintain session in an ASP.NET MVC application?

Server-Side of State Management in ASP NET An important technique to maintain state. It is used to store identity and information; information is stored in the server using Sessionid.

Is MVC stateless or stateful?

MVC is stateless because HTTP is. There is nothing in HTTP that indicates when a session starts or ends.

Which of the following is used for state management in ASP NET MVC?

In ASP.NET web applications, for state management, we use ViewState, HiddenFileds, Session, etc. Now, ASP.NET MVC doesn't support ViewState or Server controllers. To achieve state management in ASP.NET MVC, there are different ways which are given below. Let's have a detailed discussion on each of them.

Can we use view state in MVC?

ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method.


2 Answers

The answer to all three of your questions is the same: reuse your readers (and possibly your writers). You can use a singleton pattern to do this (i.e. declare your reader/writer as public static). Lucene's FAQ tells you the same thing: share your readers, because the first query is reaaalllyyyy slow. Lucene handles all the locking for you, so there is really no reason why you shouldn't have a shared reader.

It's probably easiest to just keep your writer around and (using the NRT model) get the readers from that. If it's rare that you are writing to the index, or if you don't have a huge need for speed, then it's probably OK to open your writer each time instead. That is what I do.

Edit: added a code sample:

public static IndexWriter writer = new IndexWriter(myDir);  public JsonResult SearchForStuff(string query) {     IndexReader reader = writer.GetReader();     IndexSearcher search = new IndexSearcher(reader);     // do the search } 
like image 52
Xodarap Avatar answered Sep 18 '22 22:09

Xodarap


I would probably skip the caching -- Lucene is very, very efficent. Perhaps so efficent that it is faster to search again than cache.

The OnApplication_Start full index feels a bit off to me -- should probably be run in it's own thread so as not to block other expensive startup activities.

like image 27
Wyatt Barnett Avatar answered Sep 20 '22 22:09

Wyatt Barnett