Lucene_demo05_内存索引和文件索引
内存索引库:数据是临时的、访问速度比文件索引库要快、索引库中的数据不能存放太多、内存索引库和文件索引库能结合在一起
两个或者两个以上的索引库的合并:
如果是内存索引库,直接调用构造函数进行合并就可以了 内存索引库中。也可以调用addIndexesNoOptimize进行合并
如果是文件索引库,调用addIndexesNoOptimize进行合并,该方法可以接受多个索引库
public class RamIndex {
/**
* 内存索引库
*/
@Test
public void testRAM() throws Exception {
// 创建了内存索引库
Directory directory = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
Article article = new Article();
article.setId(26L);
article.setTitle("NBA总冠军");
article.setContent("LBJ和韦德能带领热火在2013赛季拿到NBA总冠军吗");
Document document = DocumentUtils.article2Document(article);
indexWriter.addDocument(document);
indexWriter.close();
this.showData(directory);
}
/**
* 内存索引库和文件索引库的结合
*/
@Test
public void testRamIndexAndFileIndex() throws Exception {
Directory fileDirectory = FSDirectory.open(new File("./indexDir"));// 创建两个索引库
Directory ramDirectory = new RAMDirectory(fileDirectory);
// 内存索引
IndexWriter ramIndexWriter = new IndexWriter(ramDirectory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);// 这个ramIndexWriter是针对内存索引库
Article article = new Article();
article.setId(26L);
article.setTitle("热火赢了总冠军");
article.setContent("哈哈,热火终于赢了,果然不负众望!!");
Document document = DocumentUtils.article2Document(article);
ramIndexWriter.addDocument(document);
ramIndexWriter.close();
// 文件索引
IndexWriter fileIndexWriter = new IndexWriter(fileDirectory, LuceneUtils.analyzer, true, MaxFieldLength.LIMITED);// fileIndexWriter对应的索引库是fileDirectory
fileIndexWriter.addIndexesNoOptimize(ramDirectory);// 把内存索引库的信息写入到文件索引库addIndexesNoOptimize
fileIndexWriter.close();
// 经过两个索引操作后,输出最后的数据
this.showData(fileDirectory);
}
/**
* 当应用程序启动的时候,直接和内存索引库交互,当程序快结束的时候,把内存索引库中的内容追加到文件索引库中
*/
@Test
public void testRamIndexAndFileIndex2() throws Exception {
// 内存索引
Directory directory = new RAMDirectory();
IndexWriter ramIndexWriter = new IndexWriter(directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
Article article = new Article();
article.setId(27L);
article.setTitle("热火赢了总冠军");
article.setContent("哈哈,热火终于赢了,果然不负众望!!");
Document document = DocumentUtils.article2Document(article);
ramIndexWriter.addDocument(document);
ramIndexWriter.close();
// 文件索引
Directory fileDirectory = FSDirectory.open(new File("./indexDir"));
IndexWriter fileIndexWriter = new IndexWriter(fileDirectory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
fileIndexWriter.addIndexesNoOptimize(directory);
fileIndexWriter.close();
// 经过两个索引操作后,输出最后的数据
this.showData(fileDirectory);
}
// 公共输出类
private void showData(Directory directory) throws Exception {
IndexSearcher indexSearcher = new IndexSearcher(directory);
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[] { "title", "content" }, LuceneUtils.analyzer);
Query query = queryParser.parse("总冠军");
TopDocs topDocs = indexSearcher.search(query, 50);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
Document document = indexSearcher.doc(scoreDocs[i].doc);
Article article = DocumentUtils.document2Article(document);
articleList.add(article);
}
// 输出查询出来的索引对象
for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
}
分享到:
相关推荐
Lucene5写的全文搜索的demo,包括创建索引和搜索
全文索引工具 开源工具 java编写 lucene的简单demo
lucene+zoie近实时索引demo lucene+zoie近实时索引demo
lucene 4.7.2支持java 6 ,之后的版本需要java 7以上,创建、删除、修改索引,搜索支持通用对象(可以根据对象类型搜索),可以范围搜索、排序、高亮,希望有所帮助
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
Lucene结合Sql建立索引Demo源码 Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的...
这是我通过对Lucene3.3.0源码进行了简单解读,依据应用详细的做了很多Demo,大家可以一道学习。
Lucene.NET2.9.2 的应用Demo,从提取自淘车网的搜索项目,主要分两部分,创建索引和查询索引. 效果展示:http://www.taoche.com
//src要创建索引的文件,destDir索引存放的目录 public static void createIndex(File src, File destDir){ Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); //创建一个语法分析器 ...
创建索引 一、创建词法分析器 二、创建索引存储目录 三、创建索引写入器 四、将内容存储到索引 关键字查询 一、创建索引存储目录读取器 二、创建索引搜索器 三、解析查询 四、获取结果
里面编写了法创建,搜索的基本方法 LuceneDemo 单个文件索引,创建查找 CopyFile 为多文件复制多些文件做准备 CreateIndexe 多文件创建索引;Searcher多文件搜索对应CreateIndexe
总结了一些实用的demo 包括: 1.建立索引 2.通过IKAnalyzer搜索中文关键词 3.复杂的多字段搜索 4.多线程并发搜索,通过contiperf测试,详见:...lucene支持多线程并发搜索和建索引,只要IndexWriter是单例模式即可
Lucene使用的一个Demo,包括索引的创建、增加、更新、删除等
Lucene3.0 Demo 索引的创建,更新(未实现),删除
利用Lucene.net做的建立索引,检索分页
lucene4.0创建索引,删除索引,搜索的一些小demo,分享出来供大家学习。
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
这是Lucene.NET v3.0.3 DEMO范例程序(含PanGu分词),用C#语言编写的,同时对PanGu分词进行了整合,可以直接下载运行。 项目中还整理了一个后台任务线程监听范例,可以用作增量索引创建,但这个需要你自行加入相关...
一个简单的lucene demo,使用Lucene5.5+springmvc+mysql开发,包括索引的增删改查,查询中可以高亮显示+分页+自定义排序,有兴趣的朋友可以看看