问题描述
我正在构建一个 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_Startevent 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 theIndexWriter.IndexWritersare 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.Cacheto 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 anIndexReader, build and run a query, put the results inHttpRuntime.Cache, return them to the user, and finally dispose of theIndexReader. Once again,IndexReadersaren'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 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)