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

Proper structuring of Lucene.Net usage in an ASP.NET MVC site(在 ASP.NET MVC 站点中正确构建 Lucene.Net 使用)

本文介绍了在 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.

  • 关于 Global.asax 中的 Application_Start 事件:我检查文件系统上是否存在索引 - 如果不存在,我创建它并用从数据库.
  • 提交新内容时:我创建一个IndexWriter,填写一个文档,写入索引,最后处置IndexWriter.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 是否有很大的代码味道?
  • 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?

推荐答案

你所有三个问题的答案都是一样的:重用你的读者(也可能是你的作者).您可以使用 singleton 模式来执行此操作(即将您的阅读器/作者声明为公共静态).Lucene 的 FAQ 告诉你同样的事情:分享你的读者,因为第一个查询真的很慢.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 模型)从中获得读者.如果您很少写入索引,或者您对速度的需求不大,那么每次都打开您的 writer 可能是可以的.我就是这么做的.

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.

添加代码示例:

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 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 ASP.NET MVC 站点中正确构建 Lucene.Net 使用

基础教程推荐