Package COM. jadyer. lucene; import Java. io. file; import Java. io. ioexception; import Org. apache. lucene. analysis. standard. standardanalyzer; import org.apache.e.doc ument. document; import org.apache.e.doc ument. field; import Org. apache. lucene. index. indexreader; import Org. apache. lucene. index. indexwriter; import Org. apache. lucene. index. indexwriterconfig; import Org. apache. lucene. index. term; import Org. apa Che. lucene. search. indexsearcher; import Org. apache. lucene. search. nrtmanager; import Org. apache. lucene. search. nrtmanagerreopenthread; import Org. apache. lucene. search. query; import Org. apache. lucene. search. scoredoc; import Org. apache. lucene. search. termquery; import Org. apache. lucene. search. topdocs; import Org. apache. lucene. search. nrtmanager. trackingindexwriter; import Org. apache. lucene. store. directory; impor T Org. apache. lucene. store. fsdirectory; import Org. apache. lucene. util. version;/*** [lucene3.6.2 getting started series] section 12th _ near real-time search * @ see real-time search (near-real-time) ----> as long as the data changes, then update the index immediately (indexwriter. commit () * @ see near real-time search ----------------> when data changes, first save the index to the memory, then execute the commit action on all indexes in the memory at a unified time * @ see in order to achieve near real-time search, the method provided by javase3.0 is called reopen, later versions provided two thread-safe classes: nrtmanager and searchermanager * @ see. However, the usage of these two thread-safe classes in javase3.5 and 3.6 is a bit different. Note that * @ Create Aug 7, 2013 4:19:58 * @ author Xuan Yu
Below is a small test written with junit4.x
Package COM. jadyer. test; import Org. JUnit. after; import Org. JUnit. before; import Org. JUnit. test; import COM. jadyer. lucene. hellonrtsearch; public class hellonrtsearchtest {@ beforepublic void Init () {hellonrtsearch. createindex () ;}@ afterpublic void destroy () {hellonrtsearch. getdocscount () ;}@ testpublic void searchfile () {hellonrtsearch Hello = new hellonrtsearch (); For (INT I = 0; I <5; I ++) {hello. searchfile (); system. out. println ("-----------------------------------------------------------"); Hello. deleteindex (); if (I = 2) {hello. updateindex ();} Try {system. out. println ("......... start sleep for 5 S (simulating near real-time search scenarios) "); thread. sleep (1, 5000); system. out. println ("......... sleep ends ");} catch (interruptedexception e) {e. printstacktrace () ;}}// new hellonrtsearch cannot be used independently. Make sure they are the same object. Otherwise, the delete and update operations will not be commithello. commitindex ();}}