In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to obtain Reader by Lucene". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
As the current web search hopes to achieve the effect of real-time search, users want to be visible in the search results immediately after uploading articles, which requires us to use the quasi-real-time search function of Lucene, so that we can achieve the effect of near-real-time search without affecting performance. However, quasi-real-time search API is completely different from 3.x in version 4.x.
First, let's take a look at how to obtain Reader instances for quasi-real-time search. As we all know, due to performance and other reasons, Lucene-based applications generally adopt the scheme of sharing Writer, Reader and Searcher of Lucene, and we are no exception here:
IndexPathname = "D:/aproject/xincaigu/work/index"; analyzer = new MMSegAnalyzer (); IndexWriterConfig iwc = new IndexWriterConfig (Version.LUCENE_41, analyzer); iwc.setOpenMode (OpenMode.CREATE_OR_APPEND); try {indexDir = FSDirectory.open (new File (indexPathname)); writer = new IndexWriter (indexDir, iwc); / / writer and reader share reader = DirectoryReader.open (writer, true) / / reader = writer.getReader ();} catch (CorruptIndexException e) {} catch (LockObtainFailedException e) {} catch (IOException e) {}
Friends who are familiar with Lucene 3.x must have noticed that the Reader used to obtain quasi-real-time searches has switched to the DirectoryReader.open method instead of the writer.getReader () method in 3.x.
Similarly, in 3.x, in order to see the new article just added, Reader needs to reopen, which is a resource-saving way to get the newly indexed article without saving the changes to disk and then reopening the index. But reopne was also replaced by the new API in 4.x, as follows:
Try {IndexReader newReader = DirectoryReader.openIfChanged ((DirectoryReader) reader, writer, false); / / reader.reopen (); / / read the newly added incremental index content to meet the real-time index requirements if (newReader! = null) {reader.close (); reader = newReader;} searcher = new IndexSearcher (reader) } catch (CorruptIndexException e) {} catch (IOException e) {
Here, we first use the new APIDirctoryReader.openIfChanged to get the Reader. If there is new content, the new Reader is returned. In this case, we need to close the old Reader.
This is the end of the content of "how Lucene gets Reader". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.