14、实时搜索(lucene笔记)

一、是否进行实时搜索

  • 实时搜索:只要数据库一变动,马上要更新索引,要使用writer.commit来操作,但是这种情况太消耗资源。
  • 近实时搜索:当用户修改了信息之后,先把索引保存到内存中,然后在一个统一的时间内对内存中的索引进行提交操作。基于这个原因我们一般进行的是近实时搜索。

二、入门程序(工程lucene-nearRealTime

IndexUtil.java

package cn.itcast.util;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.StaleReaderException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.SearcherWarmer;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class IndexUtil {
    private String[] ids = {"1", "2", "3", "4", "5", "6"};
    //下面是邮件
    private String[] emails = {"aa@qq.com", "bb@sina.edu", "cc@yahu.org", "ss@sina.com", "dd@gmail.com", "ee@163.com"};
    //下面是邮件内容
    private String[] content = {
            "welcom to visited the space,I like football", 
            "hello boy, i like someone", 
            "come on baby", 
            "first blood", 
            "I like football,I like football", 
            "my girlfriend is so beatiful, every body like game"
    };
    private int[] attaches = {2,5,6,5,8,4};//附件数量
    //发件人名字
    private String[] names = {"Tom", "Jack", "goudan", "alibaba", "jerry", "kitty"};
    //邮件的日期
    private Date[] dates = null;
    
    private Directory directory = null;
    private Map<String, Float> scores = new HashMap<String, Float>();//新建一个Map,用来存储权值
    /*private static IndexReader reader = null;//声明一个IndexReader的属性*/
    private SearcherManager mgr = null;
    
    public IndexUtil() {
        try {
            setDates();//设置日期
            scores.put("qq.com", 2.0f);//如果是"qq.com"结尾的索引则让其权值为2.0,注意:默认是1.0
            scores.put("sina.edu", 1.5f);
            directory = FSDirectory.open(new File("E:/myeclipse/Lucene/index"));
            mgr = new SearcherManager(directory, new SearcherWarmer() {
                //在重新打开的时候可能需要做一个控制,这些控制可以在这个方法中做
                @Override
                public void warm(IndexSearcher searcher) throws IOException {
                    System.out.println("has changed");
                }
            }, Executors.newCachedThreadPool());
        } catch (IOException e) {
            e.printStackTrace(); 
        }
    }

    //创建索引
    public void index(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            //此方法可将索引全部清空
            writer.deleteAll();
            Document document = null;
            for(int i = 0; i < ids.length; i++){
                document = new Document();
                //id需要存储,不需要加权、分词,email也需要存储,但不需要分词,有时候也需要加权
                //对于内容,我们不需要存储和加权,但需要分词。而名字需要存储,不需要分词和加权
                //这里我们先不对整型数据进行索引,后面再说
                document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("content", content[i], Field.Store.NO, Field.Index.ANALYZED));
                document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                
                //为数字添加索引,第三个参数设置为true表示默认索引
                document.add(new NumericField("attach", Field.Store.YES, true).setIntValue(attaches[i]));
                //为日期添加索引
                document.add(new NumericField("date", Field.Store.YES, true).setLongValue(dates[i].getTime()));
                
                
                String et = emails[i].substring(emails[i].lastIndexOf("@") + 1);
                System.out.println(et);
                //加入权值
                if(scores.containsKey(et)){
                    document.setBoost(scores.get(et));
                }else{
                    document.setBoost(0.5f);
                }
                writer.addDocument(document);
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //查询
    public void query(){
        try {
            IndexReader reader = IndexReader.open(directory);
            //maxDoc 和 numDocs()方法的区别:maxDoc()返回索引中删除和未被删除的文档总数,
            //后者返回索引中未被删除的文档总数,通过reader可以有效获取文档的数量
            System.out.println("numDocs: " + reader.numDocs());
            System.out.println("maxDocs: " + reader.maxDoc());
            //查看被删除的索引
            System.out.println("deleteDocs : " + reader.numDeletedDocs());
            //记得用完之后关闭
            reader.close();
            
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //删除索引
    public void delete(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            
            //参数可以是一个选项,可以是一个query,也可以是一个term,term是一个精确查找的值
            //这里我们测试此方法之后再次执行搜索方法,发现文档数numDocs还有5个,比之前少了一个,但是maxDoc还是6个
            //在我们的索引目录中发现出现了一个delete的文件。这里的删除就像一个回收站一样,是可以恢复的
            writer.deleteDocuments(new Term("id", "1"));//这里表示删除索引为1的id
            writer.commit();
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //更新索引
    public void update(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));

            /* lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集
             * 先删除之后再添加,所以不是在之前的位置更新
             * 测试之后我们会发现回收站中有一个索引
             * */
            Document document = new Document();
            document.add(new Field("id", "11", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            document.add(new Field("email", emails[0], Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.add(new Field("content", content[0], Field.Store.NO, Field.Index.ANALYZED));
            document.add(new Field("name", names[0], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            writer.updateDocument(new Term("id", "1"), document);
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public void search01(){
        IndexReader reader;
        try {
            reader = IndexReader.open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);
            TermQuery query = new TermQuery(new Term("content", "like"));//搜索内容中含有like的
            TopDocs tds = searcher.search(query, 10);
            for(ScoreDoc sd : tds.scoreDocs){
                Document doc = searcher.doc(sd.doc);
                //这里我们获取权值getBoost()的时候发现都是1.0,这是因为这里是获取的一个document,和原来的没有关系。
                //要想看其权值信息,可以使用luke工具
                //而这里的日期需要我们转换成日期格式
                System.out.println("(" + sd.doc + "权值:"+ doc.getBoost() + ")" + doc.get("name") + "[" + doc.get("email") + "]-->" 
                            + doc.get("id") + "-->" + doc.get("attach") + "-->" + doc.get("date"));
                reader.close();
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //设置日期
    private void setDates(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dates = new Date[ids.length];
            dates[0] = sdf.parse("2015-02-15");
            dates[1] = sdf.parse("2015-03-01");
            dates[2] = sdf.parse("2015-05-18");
            dates[3] = sdf.parse("2015-09-05");
            dates[4] = sdf.parse("2015-12-15");
            dates[5] = sdf.parse("2015-08-29");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public void search02(){
        IndexSearcher searcher = null;
        try {
            mgr.maybeReopen();//判断是否需要重新打开一个IndexSearcher
            searcher = mgr.acquire();
            TermQuery query = new TermQuery(new Term("content", "like"));//搜索内容中含有like的
            TopDocs tds = searcher.search(query, 10);
            for(ScoreDoc sd : tds.scoreDocs){
                Document doc = searcher.doc(sd.doc);
                System.out.println("(" + sd.doc + "权值:"+ doc.getBoost() + ")" + 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();
            }
        }
    }
}

说明:这是我们之前写过的一个工具类,和之前不同的是我们这里不再直接获取IndexReader,而是定义一个SearcherManager对象:

private SearcherManager mgr = null;

然后对IndexUtil方法进行改造,我们通过SearcherManager来管理IndexReader,同时在我们更新索引的过程中我们还可以对资源做一些操作。再对方法search02方法进行改造,这里我们是通过SearcherManager来获得IndexReader对象的,同时是否打开一个新的IndexReaderSearcherManager的方法maybeReopen来决定。而使用完之后我们也不需要关闭IndexReader,只需要将其释放即可,使用方法release

下面进行测试:

package cn.lucene.test;
import org.junit.Test;
import cn.itcast.util.IndexUtil;

public class TestIndex {
    @Test
    public void testIndex(){
        IndexUtil util = new IndexUtil();
        util.index();
    }
    
    @Test
    public void testQuery(){
        IndexUtil util = new IndexUtil();
        util.query();
    }
    
    @Test
    public void testDelete(){
        IndexUtil util = new IndexUtil();
        util.delete();
    }

    @Test
    public void testUpdate(){
        IndexUtil util = new IndexUtil();
        util.update();
    }
    @Test
    public void testSearch01(){
        IndexUtil util = new IndexUtil();
        util.search01();
    }
    @Test
    public void testSearch02(){
        IndexUtil util = new IndexUtil();
        for(int i = 0; i < 5; i++){
            util.search02();//这里我们让其执行5次
            System.out.println("************************");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

说明:在测试方法testSearch02之前我们先使用方法testIndex重建索引,然后我们使用方法testQuery可以看到记录数:

1

而在testSearch02方法还没有运行完时我们使用testDelete方法进行删除,此时我们发现testSearch02方法在后面的输出少了一条记录。将记录0给删除了。虽然如此,当我们再次使用testQuery方法查询的时候发现记录没有被删掉,这是因为并没有提交,而使用SearcherManager需要进行writer.commit才会检测到,于是这就需要用到NRTManager这个类了。

注意:我们在reopen时可能需要对某些资源做一些操作,可以在方法SearcherWarmer. Warm中进行,我们在testSearch02方法的输出中也可以看到打印出了has changed

三、近实时搜索(工程lucene-nearRealTime01

只是使用SearcherManager是不能实现实时搜索的,因为只是更新的内存,而并没有真正的提交。
在类IndexUtil.java中我们添加两个属性:

private NRTManager nrtMgr = null;
private IndexWriter writer;

对相关方法进行改造:

package cn.itcast.util;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.StaleReaderException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NRTManager;
import org.apache.lucene.search.NRTManagerReopenThread;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.SearcherWarmer;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class IndexUtil {
    private String[] ids = {"1", "2", "3", "4", "5", "6"};
    //下面是邮件
    private String[] emails = {"aa@qq.com", "bb@sina.edu", "cc@yahu.org", "ss@sina.com", "dd@gmail.com", "ee@163.com"};
    //下面是邮件内容
    private String[] content = {
            "welcom to visited the space,I like football", 
            "hello boy, i like someone", 
            "come on baby like", 
            "first blood like", 
            "I like football,I like football", 
            "my girlfriend is so beatiful, every body like game"
    };
    private int[] attaches = {2,5,6,5,8,4};//附件数量
    //发件人名字
    private String[] names = {"Tom", "Jack", "goudan", "alibaba", "jerry", "kitty"};
    //邮件的日期
    private Date[] dates = null;
    
    private Directory directory = null;
    private Map<String, Float> scores = new HashMap<String, Float>();//新建一个Map,用来存储权值
    /*private static IndexReader reader = null;//声明一个IndexReader的属性*/
    private SearcherManager mgr = null;
    private NRTManager nrtMgr = null;
    private IndexWriter writer;
    
    public IndexUtil() {
        try {
            setDates();//设置日期
            scores.put("qq.com", 2.0f);//如果是"qq.com"结尾的索引则让其权值为2.0,注意:默认是1.0
            scores.put("sina.edu", 1.5f);
            directory = FSDirectory.open(new File("E:/myeclipse/Lucene/index"));
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            nrtMgr = new NRTManager(writer, new SearcherWarmer() {
                
                @Override
                public void warm(IndexSearcher arg0) throws IOException {
                    System.out.println("reopen");
                    
                }
            });
            //重新打开的时候间隔最大为5.0,最小为0.025,0.025为25秒
            NRTManagerReopenThread reopen = new NRTManagerReopenThread(nrtMgr, 5.0, 0.025);
            reopen.setDaemon(true);
            reopen.setName("NrtManger Reopen Thread");
            reopen.start();//启动线程NRTManager的reopen线程
            mgr = nrtMgr.getSearcherManager(true);//允许所有更新
        } catch (IOException e) {
            e.printStackTrace(); 
        }
    }

    //创建索引
    public void index(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            //此方法可将索引全部清空
            writer.deleteAll();
            Document document = null;
            for(int i = 0; i < ids.length; i++){
                document = new Document();
                //id需要存储,不需要加权、分词,email也需要存储,但不需要分词,有时候也需要加权
                //对于内容,我们不需要存储和加权,但需要分词。而名字需要存储,不需要分词和加权
                //这里我们先不对整型数据进行索引,后面再说
                document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("content", content[i], Field.Store.NO, Field.Index.ANALYZED));
                document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                
                //为数字添加索引,第三个参数设置为true表示默认索引
                document.add(new NumericField("attach", Field.Store.YES, true).setIntValue(attaches[i]));
                //为日期添加索引
                document.add(new NumericField("date", Field.Store.YES, true).setLongValue(dates[i].getTime()));
                
                
                String et = emails[i].substring(emails[i].lastIndexOf("@") + 1);
                System.out.println(et);
                //加入权值
                if(scores.containsKey(et)){
                    document.setBoost(scores.get(et));
                }else{
                    document.setBoost(0.5f);
                }
                writer.addDocument(document);
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //查询
    public void query(){
        try {
            IndexReader reader = IndexReader.open(directory);
            //maxDoc 和 numDocs()方法的区别:maxDoc()返回索引中删除和未被删除的文档总数,
            //后者返回索引中未被删除的文档总数,通过reader可以有效获取文档的数量
            System.out.println("numDocs: " + reader.numDocs());
            System.out.println("maxDocs: " + reader.maxDoc());
            //查看被删除的索引
            System.out.println("deleteDocs : " + reader.numDeletedDocs());
            //记得用完之后关闭
            reader.close();
            
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //删除索引
    public void delete(){
        IndexWriter writer = null;
        try {
            nrtMgr.deleteDocuments(new Term("id", "1"));//这里表示删除索引为1的id
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //更新索引
    public void update(){
        try {
            /* lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集
             * 先删除之后再添加,所以不是在之前的位置更新
             * 测试之后我们会发现回收站中有一个索引
             * */
            Document document = new Document();
            document.add(new Field("id", "11", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            document.add(new Field("email", emails[0], Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.add(new Field("content", content[0], Field.Store.NO, Field.Index.ANALYZED));
            document.add(new Field("name", names[0], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            nrtMgr.updateDocument(new Term("id", "1"), document);
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void search01(){
        IndexReader reader;
        try {
            reader = IndexReader.open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);
            TermQuery query = new TermQuery(new Term("content", "like"));//搜索内容中含有like的
            TopDocs tds = searcher.search(query, 10);
            for(ScoreDoc sd : tds.scoreDocs){
                Document doc = searcher.doc(sd.doc);
                //这里我们获取权值getBoost()的时候发现都是1.0,这是因为这里是获取的一个document,和原来的没有关系。
                //要想看其权值信息,可以使用luke工具
                //而这里的日期需要我们转换成日期格式
                System.out.println("(" + sd.doc + "权值:"+ doc.getBoost() + ")" + doc.get("name") + "[" + doc.get("email") + "]-->" 
                            + doc.get("id") + "-->" + doc.get("attach") + "-->" + doc.get("date"));
                reader.close();
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //设置日期
    private void setDates(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dates = new Date[ids.length];
            dates[0] = sdf.parse("2015-02-15");
            dates[1] = sdf.parse("2015-03-01");
            dates[2] = sdf.parse("2015-05-18");
            dates[3] = sdf.parse("2015-09-05");
            dates[4] = sdf.parse("2015-12-15");
            dates[5] = sdf.parse("2015-08-29");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public void search02(){
        IndexSearcher searcher = null;
        try {
            mgr.maybeReopen();//判断是否需要重新打开一个IndexSearcher
            searcher = mgr.acquire();
            TermQuery query = new TermQuery(new Term("content", "like"));//搜索内容中含有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("attach") + "-->" + doc.get("date"));
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                mgr.release(searcher);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public void commit(){
        try {
            writer.commit();
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

说明:在方法IndexUtil中我们实例化了IndexWriter对象,同时使用NRTManager对象来获得SearcherManager对象,进而管理IndexSearcher。在deleteupdate方法中,直接使用NRTManager来进行相关的操作,因为此对象实现了IndexSearcher的所有方法,所以我们不直接使用IndexSearcher了。在方法search02中我们使用方法maybeReopen判断是否需要重新打开IndexSearcher,而这个打开的时间我们在方法IndexUtil中设置的为25-5000秒之间。

测试:
测试时对testSearch02方法进行修改:

    @Test
    public void testSearch02(){
        IndexUtil util = new IndexUtil();
        for(int i = 0; i < 5; i++){
            util.search02();//这里我们让其执行5次
            System.out.println("************************");
            util.delete();
            if(i == 2){
                util.update();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

说明:测试结果:

1-->Tom[aa@qq.com]-->2-->1423929600000
2-->Jack[bb@sina.edu]-->5-->1425139200000
5-->jerry[dd@gmail.com]-->8-->1450108800000
6-->kitty[ee@163.com]-->4-->1440777600000
************************
reopen
2-->Jack[bb@sina.edu]-->5-->1425139200000
5-->jerry[dd@gmail.com]-->8-->1450108800000
6-->kitty[ee@163.com]-->4-->1440777600000
************************
2-->Jack[bb@sina.edu]-->5-->1425139200000
5-->jerry[dd@gmail.com]-->8-->1450108800000
6-->kitty[ee@163.com]-->4-->1440777600000
************************
reopen
2-->Jack[bb@sina.edu]-->5-->1425139200000
11-->Tom[aa@qq.com]-->null-->null
5-->jerry[dd@gmail.com]-->8-->1450108800000
6-->kitty[ee@163.com]-->4-->1440777600000
************************
2-->Jack[bb@sina.edu]-->5-->1425139200000
11-->Tom[aa@qq.com]-->null-->null
5-->jerry[dd@gmail.com]-->8-->1450108800000
6-->kitty[ee@163.com]-->4-->1440777600000
************************

这里我们是先进行查询,之后进行删除,同时在第三次查询的时候进行更新,更新其实就是将之前id为1(删除的那条记录)更新为11,可以看到相应的结果。而此时我们再将删除和更新的部分注释掉,即:

util.delete();
if(i == 2){
    util.update();
}

再次查询,发现并没有删除,这是因为删除只是在内存中进行,而并没有提交。这里要注意的是:这里之所以叫近实时搜索,就是这个更新的时间是由线程控制,这里我们可以将休眠时间分别该为1秒和2秒(同时将mgr.maybeReopen();注释掉)来查看效果。当然在这个测试方法的最后加上

uti.commit();

才会将相关的更新持久化。而相比SearcherManagerNRTManager不需要提交就能被搜索到,这样我们就可以在一个统一的时间对索引进行更新了。

未完待续。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容