在ASP.NET MVC网站Lucene.Net使用的正确结构 [英] Proper structuring of Lucene.Net usage in an ASP.NET MVC site

查看:198
本文介绍了在ASP.NET MVC网站Lucene.Net使用的正确结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个ASP.NET MVC网站,我打算使用Lucene.Net。我设想了一个方法来组织的Lucene的使用,但不知道我的计划架构是否正常,高效。

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.


  • 的Application_Start 事件在Global.asax中:我检查文件系统上的索引的存在 - 如果不存在的话,我创建并填写与文件从数据库中提取它。

  • 在当前新的提交内容:我创建一个的IndexWriter ,填写了一份文件,写索引,最后处置的IndexWriter <的/ code>。 IndexWriters 不重用,因为我无法想象一个很好的方法做,在ASP.NET MVC应用程序。

  • 当内容被编辑:我重复同样的过程,当有新的内容提交的,除了我先删除旧的内容,然后添加修改

  • 当用户搜索的内容:我检查 HttpRuntime.Cache 来看看,如果用户已经搜索这个词在过去的5分钟 - 如果他们有,我返回那些结果;否则,我创建一个的IndexReader ,建立并运行一个查询,结果放 HttpRuntime.Cache ,他们返回用户和最终处置的的IndexReader 。再次, IndexReaders 都不要再用。

  • 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.

  • 是一个良好的结构 - 我怎么能改善它

  • 是否有任何性能/效率问题我应该知道的?

  • 此外,为不重用IndexReaders和IndexWriters 一个巨大的code味道?

  • 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?

推荐答案

回答所有三个你的问题是一样的:重用你的读者(也可能是您的刻录机)。您可以使用模式来做到这一点(即宣告你的读/写器作为公共静态)。 Lucene的常见问题解答告诉你同样的事情:分享你的读者,因为第一个查询reaaalllyyyy缓慢。 Lucene的处理所有的锁定你,所以实在没有理由你不应该有一个共同的读者。

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.

这是最简单的可能只是保持你周围的作家和(使用 NRT 模型)得到了读者。如果它是罕见的,你正在写到索引,或者如果你没有对速度的巨大需求,那么它可能确定每次打开你的作家来代替。这是我做的。

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.

编辑:增加了code样品:<​​/ P>

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
}

这篇关于在ASP.NET MVC网站Lucene.Net使用的正确结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆