1. The lifecycle of IndexReader and IndexWriter
1) The open and close operations for reader/writer are at high cost.
2) Especially for IndexReader, time consumption of these operations are high.
3) So by convention, IndexReader is set as singleton in application.
package edu.xmu.lucene.Lucene_ModuleOne; import java.io.File; import java.io.IOException; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; /** * Hello world! * */ public class App { private Directory dir = null; private static IndexReader reader = null; public App() { try { dir = FSDirectory.open(new File("E:/LuceneIndex")); reader = IndexReader.open(dir); } catch (IOException e) { e.printStackTrace(); } } public IndexSearcher getSearcher() { IndexSearcher searcher = new IndexSearcher(reader); return searcher; } /** * Search * */ public void search() { IndexSearcher searcher = getSearcher(); TermQuery query = new TermQuery(new Term("name", "Davy")); TopDocs topDocs; try { topDocs = searcher.search(query, 10); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); String score = doc.get("score"); String date = doc.get("date"); float boost = doc.getBoost(); System.out.println("Score = " + score + ", Date = " + date + ", Boost = " + boost); } } catch (IOException e) { e.printStackTrace(); } finally { try { // We only have to close searcher and don't have to close // reader. searcher.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2. Other quesions:
1) As there is only one reader that is created through the whole application.
2) Will the changes be detected and reflected to reader whenever an IndexWriter change the index file?
--> Any update/delete operations will not affect the reader that created before such operation executed.
package edu.xmu.lucene.Lucene_ModuleOne; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { @Test public void testSearch() { App app = new App(); for (int i = 0; i < 5; i++) { app.search(); } } }
Comments: If we execute test case as above. There will be no affect whatever the count of search operation be excuted. Because during the whole process, there is only one reader created. There will not be some other index files created.
3) How can we enable reader to detect the change of index during the whole lifecycle of this reader?
public IndexSearcher getSearcher() { try { if (reader == null) { reader = IndexReader.open(dir); } else { if (null != IndexReader.openIfChanged(reader)) { reader = IndexReader.openIfChanged(reader); } } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } IndexSearcher searcher = new IndexSearcher(reader); return searcher; }
Comments:
1) In class IndexReader: static IndexReader openIfChanged(IndexReader oldReader)
-> If the index has chaged since the provided reader was opend, open and return a new reader, else, return null.
2) By using this method, we can update reader in real time.
3) By convention, during the whole lifecycle of the application, there will be only one IndexReader.
4) But some other application, writer is required to be singleton.
-> So how can we commit the change we made to the index file as we cannot close the writer?
-> Use writer.commit();
-> If we don't commit, then the index file will not change, the modification we did in IndexWriter is invalid.
5) As we can see, during the procession of IndexReader.openIfChanged, there will be new IndexReader created if there is some change in index.
-> So what about the old reader? As the old reader hasn't been closed yet.
6) We can find out that during the procession of delete.
-> We can not only use writer.deleteDocument(new Term(key, value)); but also use reader.deleteDocument(new Term(key, value));
-> Remember to the easiest way for reader to commit is reader.close();
-> So what's the difference of the two approaches?
-> By default, reader is read only. Use IndexReader.open(dir, false) to make it writable.
-> If we use reader to modify the index, actually it will create a writer and then use the writer to execute the modification.
-> The benefit is that the modification information will reflect to reader in real time and we don't have to use IndexReader.openIfChanged(IndexReader reader);
-> But it would be a little difficult to submit changes using reader.
-> So by convention, we don't use this approach.
相关推荐
《Nutch入门经典翻译1:Introduction to Nutch, Part 1: Crawling》一文深入介绍了Nutch这一开源网络爬虫框架的基本概念、体系结构及其关键组件,为初学者提供了全面的理解视角。以下是对该文章核心知识点的详细解读...
Lucene是一个基于Java的全文索引工具包。 1. 基于Java的全文索引引擎Lucene简介:关于作者和Lucene的...5. Hacking Lucene:简化的查询分析器,删除的实现,定制的排序,应用接口的 扩展 6. 从Lucene我们还可以学到什么
Maven坐标:org.apache.lucene:lucene-core:7.7.0; 标签:apache、lucene、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档...
由于林良益先生在2012之后未对IKAnalyzer进行更新,后续lucene分词接口发生变化,导致不可使用,所以此jar包支持lucene6.0以上版本
指南-Lucene:ES篇.md
【Lucene:基于Java的全文检索引擎简介】 Lucene是一个由Java编写的开源全文检索引擎工具包,由Doug Cutting创建并贡献给Apache基金会,成为Jakarta项目的一部分。它不是一个独立的全文检索应用,而是提供了一个可...
**Lucene:基于Java的全文检索引擎简介** Lucene是一个高度可扩展的、高性能的全文检索库,由Apache软件基金会开发并维护。它是Java开发者在构建搜索引擎应用时的首选工具,因为它提供了完整的索引和搜索功能,同时...
### 基于Java的全文检索引擎Lucene简介 #### 1. Lucene概述与历史背景 Lucene是一个开源的全文检索引擎库,完全用Java编写。它为开发者提供了构建高性能搜索应用程序的基础组件。尽管Lucene本身不是一个现成的应用...
**Lucene:基于Java的全文检索引擎** Lucene是一个由Apache软件基金会的Jakarta项目维护的开源全文检索引擎。它不是一个完整的全文检索应用,而是一个用Java编写的库,允许开发人员轻松地在他们的应用程序中集成...
快速上手 1. 运行环境 IDE:IntelliJ IDEA 项目构建工具:Maven 数据库:MySQL Tomcat:Tomcat 8.0.47 2. 初始化项目 创建一个名为bookshop的数据库,将bookshop.sql导入 打开IntelliJ IDEA,将项目导入 ...
【Lucene:基于Java的全文检索引擎简介】 Lucene是一个由Java编写的全文索引工具包,它不是一个完整的全文检索应用,而是作为一个可嵌入的引擎,为各种应用程序提供全文检索功能。Lucene的设计目标是简化全文检索的...
《Lucene分词技术与IKAnalyzer详解》 在信息技术领域,搜索引擎是不可或缺的一部分,而Lucene作为Apache软件基金会的一个开放源代码项目,是Java语言开发的全文检索引擎库,为构建高效、可扩展的信息检索应用提供了...
Maven坐标:org.apache.lucene:lucene-sandbox:6.6.0; 标签:apache、lucene、sandbox、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译...