Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get Reader by Lucene

Shulou Source: shulou.com Published: 2022-06-01 09:14:17 10月01日 Update

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!

Tags: Search real-time article index content performance situation effect way method more version knowledge different practical successful next function reason dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB Shulou Technology macOS Docker