在 ASP.NET MVC 站点中正确构建 Lucene.Net 使用结构

发布于 2024-09-14 10:40:23 字数 838 浏览 6 评论 0原文

我正在构建一个 ASP.NET MVC 站点,我计划在其中使用 Lucene.Net。我设想了一种构建 Lucene 使用的方法,但不确定我计划的架构是否正确且高效。


我的计划:

  • 在 Global.asax 中的 Application_Start 事件上:我检查文件系统上是否存在索引 - 如果不存在,我创建它并用从文件系统中提取的文档填充它数据库。
  • 提交新内容时:我创建一个 IndexWriter,填充文档,写入索引,最后处理 IndexWriterIndexWriters 没有被重用,因为我无法想象在 ASP.NET MVC 应用程序中执行此操作的好方法。
  • 编辑内容时:我重复与提交新内容时相同的过程,只不过我首先删除旧内容,然后添加编辑。
  • 当用户搜索内容时:我检查 HttpRuntime.Cache 以查看用户是否在过去 5 分钟内搜索过该术语 - 如果有,我将返回这些结果;否则,我创建一个 IndexReader,构建并运行查询,将结果放入 HttpRuntime.Cache,将它们返回给用户,最后处理 IndexReader 。再次强调,IndexReaders 不会被重用。

我的问题:

  • 这是一个好的结构吗?我该如何改进它?
  • 是否存在我应该注意的任何性能/效率问题
  • 另外,不重用 IndexReaders 和 IndexWriters 是否会产生巨大的代码味道?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-09-21 10:40:23

所有三个问题的答案都是相同的:重用你的读者(可能还有你的作者)。您可以使用 singleton 模式来执行此操作(即将您的阅读器/编写器声明为公共静态)。 Lucene 的 FAQ 告诉您同样的事情:分享您的读者,因为第一个查询非常慢。 Lucene 会为您处理所有锁定,因此您没有理由不拥有共享读取器。

最简单的方法可能是让你的作者留在身边并(使用 NRT 模型)吸引读者从那。如果您很少写入索引,或者您对速度没有很大的需求,那么每次打开您的写入器可能就可以了。这就是我所做的。

编辑:添加了代码示例:

public static IndexWriter writer = new IndexWriter(myDir);

public JsonResult SearchForStuff(string query)
{
    IndexReader reader = writer.GetReader();
    IndexSearcher search = new IndexSearcher(reader);
    // do the search
}

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
}
暮年 2024-09-21 10:40:23

我可能会跳过缓存——Lucene 非常非常高效。也许效率如此之高,以至于再次搜索比缓存更快。

OnApplication_Start 完整索引对我来说有点不对劲——可能应该在它自己的线程中运行,以免阻塞其他昂贵的启动活动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文