Real-time search (near real-time search)
Full real-time search: As soon as the database changes, to update the index, writer.commit to operate
Near real-time search: When the user modifies the information, the index is saved in memory and then committed at a uniform time to all the indexes in memory.
Reopen,nrtmanager (Near-real-time)
Lucene uses the Nrtmanager class to achieve near real-time search, the so-called near real-time search in the index changes, the The call to the user program is reflected in a relatively short time by thread tracking.
Nrtmanager manages IndexWriter objects and will indexwriter some of the methods (deletions) such as:
Adddocument,deletedocument and other methods exposed to the client call, its operations are all in memory, so if you do not call IndexWriter's Commit method, through the above operation,
The index library inside the user's hard drive will not change, as you update the index library every time, remember to commit it so that you can write the changed index to the hard disk and synchronize the index update. with
Each time the user gets the latest index (indexsearcher), there are two ways to:
The first is through Call change, each change is called maybereopen method, keep the latest generation index,
open a new The Indexsearcher object, and the user wants the indexsearcher object is Nrtmanager Call Getsearchermanager method to get Searchermanager to
the Searchermanager object is then used to get the Indexsearcher object back to customer use, and the user finishes of after calling Searchermanager release releases the Indexsearcher object, the most
After remember to close the Nrtmanagerreopenthread;
The second way is not to pass the Nrtmanagerreopenthread object, but to call Nrtmanager's Maybereopen method directly to get the latest Indexsearcher object to get the most
The new index.
The following is the relevant code:
Private Searchermanager mgr = null; Private Nrtmanager nrtmgr = null; Private IndexWriter writer = null; writer = new IndexWriter (directory,new indexwriterconfig (version.lucene_35,new standardanalyzer (Version.LUCENE_35)) ); Nrtmgr = new Nrtmanager (writer,new Searcherwarmer () {@Override public void warm (indexsearc Her s) throws IOException {System.out.println ("reopen"); } }); Start Nrtmanager reopen thread nrtmanagerreopenthread reopen = new Nrtmanagerreopenthread (nrtmgr, 5.0,0.025); Reopen.setdaemon (TRUE); Reopen.setname ("Nrtmanager reopen Thread"); Reopen.start (); MGR = Nrtmgr.getsearchermanager (true); public void Delete () {try {nrtmgr.deletedocuments (The new term ("id", "2")); } catch (Corruptindexexception e) {e.printstacktrace (); } catch (LockobtainfailedexceptIon e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }} public void Update () {try {* * * * Lucene does not provide an update, the update operation here is actually the following two operations of the collection * First delete and add */Document DOC = new document (); Doc.add (New Field ("id", "one", field.store.yes,field.index.not_analyzed_no_norms)); Doc.add (New Field ("email", emails[0],field.store.yes,field.index.not_analyzed)); Doc.add (New Field ("content", contents[0],field.store.no,field.index.analyzed)); Doc.add (New Field ("name", names[0],field.store.yes,field.index.not_analyzed_no_norms)); Nrtmgr.updatedocument (New term ("id", "1"), doc); } catch (Corruptindexexception e) {e.printstacktrace (); } catch (Lockobtainfailedexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }} public void Search02 () {Indexsearcher searcher = Mgr.acquire (); try {//Mgr.maybereopen ();//Determine if you need to reopen a searcher termquery query = new Termquery (New term ("content", "like")); Topdocs TDS = searcher.search (query, 10); for (Scoredoc sd:tds.scoreDocs) {Document doc = Searcher.doc (sd.doc); System.out.println (Doc.get ("id") + "---->" + doc.get ("name") + "[" +doc.get ("email") + "]-->" +doc.get ("id") + "," + Doc.get ("attach") + "," +doc.get ("date"); }} catch (Corruptindexexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } finally {try {mgr.release (searcher); } catch (IOException e) {e.printstacktrace (); } } }
lucene3.5 near real-time search through Nrtmanager and Searchmanager