简体   繁体   English

将IndexWriter与SearchManager结合使用

[英]Using IndexWriter with SearchManager

I have a few basic questions regarding the usage of SearcherManager with IndexWriter. 关于将SearcherManager与IndexWriter结合使用,我有一些基本问题。

I need to periodically re-build the Lucene index in the application and currently it happens on a different thread other than the one that serves the search requests. 我需要定期在应用程序中重建Lucene索引,当前它发生在与服务搜索请求不同的线程上。

  1. Can I use the same IndexWriter instance through the lifetime of the application to rebuild the index periodically? 是否可以在应用程序的整个生命周期中使用相同的IndexWriter实例来定期重建索引? Currently, I create / open it once during the startup and just call IndexWriter#commit whenever a new index is built. 当前,我在启动过程中创建/打开一次它,只要构建新索引就调用IndexWriter#commit
  2. I'm using SearcherManager to acquire and release IndexSearcher instances for each search request. 我正在使用SearcherManager来获取和释放每个搜索请求的IndexSearcher实例。 After the index is periodically built, I'm planning to use SearcherManager#maybeRefresh method to get refreshed IndexSearcher instances.SearcherManager instance is also created once during the startup and I intend to maintain it through out. 定期建立索引后,我打算使用SearcherManager#maybeRefresh方法来获取刷新的IndexSearcher实例SearcherManager#maybeRefresh实例在启动过程中也会创建一次,并且打算一直进行维护。
  3. I do not close the IndexWriter or SearcherManager throughout the app's lifetime. 我不会在应用程序的整个生命周期中关闭IndexWriterSearcherManager

Now for the questions, 现在对于问题,

  1. If I create a new IndexWriter every time I need to rebuild the index, will SearcherManager#maybeRefresh be able to detect that it's a new IndexWriter Instance? 如果我每次需要重建索引时都创建一个新的IndexWriter, SearcherManager#maybeRefresh能否检测到它是新的IndexWriter实例? Or do I need to create a new SearcherManager using the newly created IndexWriter ? 还是我需要使用新创建的IndexWriter创建新的SearcherManager?
  2. What's the difference between creating a SearcherManager instance using an IndexWriter , creating it using a DirectoryReader or creating it using a Directory ? 什么是创造之间的区别SearcherManager使用实例IndexWriter ,使用创造它DirectoryReader或使用它创建Directory

The answers depend on how you construct your SearcherManager : 答案取决于您如何构造SearcherManager

If you construct it with a DirectoryReader , all future IndexSearchers acquired from the SearcherManager will be based on that reader, ie all searches will provide results from the point in time you instantiated the SearcherManager. 如果使用DirectoryReader构造它,则将来从SearcherManager获取的所有IndexSearcher都将基于该阅读器,即,所有搜索都将提供实例化SearcherManager时的结果。 If you write data to the index/directory and run SearcherManager.maybeRefresh() afterwards, the reader will not be updated and your search results will be outdated. 如果将数据写入索引/目录,然后再运行SearcherManager.maybeRefresh() ,则不会更新阅读器,并且搜索结果将过时。

If you construct the SearcherManager with an IndexWriter , SearcherManager.maybeRefresh() will update the SearcherManager's reader if data has been written and commited by the writer. 如果使用IndexWriter构造SearcherManager,则如果写入器已写入并提交了数据, SearcherManager.maybeRefresh()将更新SearcherManager的读取器。 All newly acquired IndexSearchers will then reflect the new state of the underlying index. 然后,所有新收购的IndexSearchers将反映基础索引的新状态。

Despite having limited experience, I recommend using the latter approach. 尽管经验有限,但我建议使用后一种方法。 It provides a very simple way to implement near-real-time searching : At application start you create an IndexWriter and construct a SearcherManager with it. 它提供了一种非常简单的方法来实现近实时搜索 :在应用程序启动时,您将创建一个IndexWriter并使用它构建一个SearcherManager。 Afterwards you start a background thread that periodically commits all changes in the IndexWriter and refreshes the SearcherManager. 然后,您启动一​​个后台线程,该线程会定期提交IndexWriter中的所有更改并刷新SearcherManager。 For the lifetime of your application you can keep using the initial IndexWriter and SearcherManager without having to close/reopen them. 在应用程序的生命周期内,您可以继续使用初始的IndexWriter和SearcherManager,而不必关闭/重新打开它们。


PS : I have only started working with Lucene a few days ago, so don't take everything I wrote here as 100% certain. PS :几天前我才刚开始与Lucene合作,所以请不要以我在这里写的一切为100%把握。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM