我正在构建一个 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
事件:我检查文件系统上是否存在索引 - 如果不存在,我创建它并用从数据库.IndexWriter
,填写一个文档,写入索引,最后处置IndexWriter
.IndexWriters
没有被重用,因为我无法想象在 ASP.NET MVC 应用程序中这样做的好方法.HttpRuntime.Cache
以查看用户在过去 5 分钟内是否已经搜索过这个词 - 如果有,我会返回这些结果;否则,我创建一个 IndexReader
,构建并运行查询,将结果放入 HttpRuntime.Cache
,返回给用户,最后处置 IndexReader代码>.IndexReaders
再次没有被重用.
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.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.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.你所有三个问题的答案都是一样的:重用你的读者(也可能是你的作者).您可以使用 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 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!