Properly structured use of Lucene.Net on ASP.NET MVC

I am creating an ASP.NET MVC site where I plan to use Lucene.Net. I have foreseen a way to structure the use of Lucene, but I'm not sure that my planned architecture is in order and efficient.




My plan:

  • In the Application_Start event in Global.asax: I check for an index on the file system β€” if it does not exist, I create it and populate it with documents extracted from the database.
  • When new content is posted: I create an IndexWriter , fill out the document, write to the index, and finally delete the IndexWriter . IndexWriters not reused since I cannot imagine a good way to do this in an ASP.NET MVC application.
  • When the content is edited: I repeat the same process as when sending new content, except that I delete the old content first and then add the changes.
  • When a user searches for content: I check HttpRuntime.Cache to see if the user has actually searched for this term in the last 5 minutes - if there are any, I will return these results; otherwise, I create an IndexReader , create and run a query, put the results in HttpRuntime.Cache , return them to the user, and finally delete IndexReader . Once again, IndexReaders not reused.



My questions:

  • This is a good structure - how can I improve it?
  • Are there any performance / efficiency issues. . I need to know.
  • Also, don't reuse IndexReaders and IndexWriters huge code smell?
+26
c # asp.net-mvc lucene
Aug 13 2018-10-10T00:
source share
2 answers

The answer to all three of your questions is the same: repeat the use of your readers (and, possibly, your authors). You can use the singleton pattern to do this (i.e. declare the reader / writer public). The Lucene FAQ tells you the same thing: share it with your readers, because the first reaaalllyyyy request is slow. Lucene handles all locks for you, so there is no reason why you should not have a common reader.

Probably the easiest way is to just keep your writer and (using the NRT model ). If you rarely have to write to the index, or if you do not need a huge need for speed, then probably OK to open your writer every time. This is what I do.

Edit: added sample code:

 public static IndexWriter writer = new IndexWriter(myDir); public JsonResult SearchForStuff(string query) { IndexReader reader = writer.GetReader(); IndexSearcher search = new IndexSearcher(reader); // do the search } 
+15
Aug 17 '10 at 19:39
source share

I would probably skip caching - Lucene is very, very efficient. Perhaps so effective that it’s faster to search again than cache.

The full OnApplication_Start index feels a bit off of me - it should probably run its own thread in it so as not to block other expensive launching activities.

+13
Jan 28 '11 at 18:47
source share



All Articles