Lucene 4.x Practice 1

Source: Internet
Author: User

In the Lucene 3. x era, Lucene In Action is a very good reference book, which details various advanced usage technologies of Lucene, which is very practical for developers. However, Lucene has recently been upgraded to version 4.x, which greatly improves performance and is worth using in new projects. However, Lucene 4. the API in x is 3. x has changed a lot. A lot of content In Lucene In Action is outdated, and because 4. x has not been released for a long time, and there are no good documents to describe the usage. This series of articles is intended to record their own use of Lucene 4. x experience for your reference.

Because network searches all want to achieve real-time search results, users want to see the search results immediately after uploading the article, which requires that we must use Lucene's quasi-real-time search function, this allows us to achieve near real-time search without affecting performance. However, the quasi-real-time search API version 4. x is completely different from version 3. x.

First, let's take a look at how to obtain a quasi-real-time search Reader instance. We all know that Lucene-based applications generally use the Lucene-based Writer, Reader, and Searcher sharing solutions for performance and other reasons, 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 ){}

Those familiar with Lucene 3.x must have noticed that the Reader used for obtaining quasi-real-time search has switched to the DirectoryReader. open method instead of the writer. getReader () method in the 3.x method.

Similarly, in 3. in column x, in order to see the newly added article, Reader needs to perform reopen operations. This is a resource saving method and can obtain the Newly Added Index article, you do not need to save the changes to the disk, and then re-open the index. However, reopne is also replaced by the new API in 4.x. The specific usage is 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 requirement. 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 obtain the reader. If there is new content, the new reader is returned. In this case, we need to disable the old reader.

With the above code, we can use the quasi-real-time search function of Lucene 4.x. But Lucene 4. the API changes in X are far more than that. When indexing, the original field definition method is outdated and replaced by a more flexible fieldtype mechanism, in the next article, we will discuss in detail how to use this new mechanism in text indexing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.