Reprint Please specify source: http://blog.csdn.net/xiaojimanman/article/details/44280311
Http://www.llwjy.com/blogdetail/e42fa5c3097f4964fca0fdfe7cd7a9a2.html
Personal Blog Station has been online, the URL www.llwjy.com, welcome everyone to spit Groove ~
The previous blog has introduced the real-time index retrieval function, this is equivalent to the data query function, we all know that the database of add and remove is the most commonly used four functions, real-time index is the same, he also has to increase the deletion of the operation, here on the real-time index of the deletion and modification operations, in the real-time index management We have already mentioned that the operation of IndexWriter in the real-time index is delegated to Trackingindexwriter, and the method in Trackingindexwriter is similar to the method in IndexWriter, which implements the basic operation of the index. Here is a description of the next.
The index name will allow me to instantiate
The nrtindex here, like the previous Nrtsearch class, are instantiated according to the index name, in the Nrtindex class, we define two properties, one is the Trackingindexwriter object, and the other is the index name. So the Nrtindex is constructed as follows:
Public Nrtindex (String indexname) {this.indexname = Indexname;indexwriter = Indexmanager.getindexmanager (indexName). Gettrackingindexwriter ();}
The operation of a statement is sufficient
Trackingindexwriter in the operation and IndexWriter as simple, almost all is a statement enough, the following we separately see and delete the operation of the statement:
Add a record indexwriter.adddocument (DOC),//delete the qualifying record indexwriter.deletedocuments (query);// All the records are not Indexwriter.deleteall ();//Update records indexwriter.updatedocument (term, doc) according to conditions;
The implementation of the code is so simple that a single statement of the implementation of the index operation, of course, all thanks to Lucene's powerful features.
I'm lazy.
In order to facilitate the subsequent operation, we will be the four methods for further encapsulation, so that the subsequent operation is more simple, the source code of the Nrtindex class is as follows:
/** * @Description: Index management Operation class, adding and removing three operations */package com.lulei.lucene.index.operation; Import Java.io.ioexception;import Org.apache.lucene.document.document;import Org.apache.lucene.index.term;import Org.apache.lucene.search.nrtmanager.trackingindexwriter;import Org.apache.lucene.search.query;import Com.lulei.lucene.index.manager.IndexManager; public class Nrtindex {private Trackingindexwriter indexwriter;private String indexname;// Directly using IndexWriter in Indexmanager, delegate the modification of the index to Trackingindexwriter implementation public Nrtindex (String indexname) {this.indexname = Indexname;indexwriter = Indexmanager.getindexmanager (indexname). Gettrackingindexwriter (); /** * @param doc * @return Boolean * @Author: Lulei * @Description: Add document to Index */public boolean adddocument (document Doc) {try {indexwriter.adddocument (DOC); return true;} catch (IOException e) {e.printstacktrace (); return false;}} /** * @param Query * @return Boolean * @Author: Lulei * @Description: Remove document */public Boolean Deletedoc from the index by query condition Ument (Query query) {try {indexwriter.deletedocuments (query); return true;} catch (IOException e) {e.printstacktrace (); return false;}} /** * @return * @Author: Lulei * @Description: Empty index */public boolean deleteAll () {try {indexwriter.deleteall (); return TR UE;} catch (IOException e) {e.printstacktrace (); return false;}} /** * @param term * @param doc * @return * @Author: Lulei * @Description: Modify the document */public Boolean UpdateD in the index according to terms Ocument (term, Document doc) {try {indexwriter.updatedocument (term, doc); return true;} catch (IOException e) { E.printstacktrace (); return false;}} /** * @throws IOException * @Author: Lulei * @Description: Merge index */public void commit () throws IOException {INDEXMANAGER.G Etindexmanager (IndexName). Getindexwriter (). commit ();}}
The introduction to the Nrtindex class here is over, and later blogs will create application subclasses for the Nrtsearch and Nrtindex classes, making it easier to manipulate the index.
PS: Recently found other sites may be reproduced on the blog, there is no source link, if you want to see more about Lucene-based case development please click here. Or visit the URL http://blog.csdn.net/xiaojimanman/article/category/2841877 or http://www.llwjy.com/
Lucene-based case development: changes to real-time indexing