Lucene-based case development: changes to real-time indexing

Source: Internet
Author: User

Reprint Please specify source:


Personal Blog Station has been online, the URL, 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;import Org.apache.lucene.document.document;import Org.apache.lucene.index.term;import;import;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 or

Lucene-based case development: changes to real-time 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: 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.