http://sourceforge.net/projects/regain/files/
http://sourceforge.jp/projects/sfnet_regain/releases/?file_id=2419546
http://code.google.com/p/paoding/downloads/list
lucene3.0入门实例2009-12-07 21:33lucene3.0已于2009-11-25发布啦,但网上的入门实例都是针对lucene3.0以前的,相对于以前的版本,貌似改动不小。
本人从零开始学习《lucene in action中文版》,并结合lucene3.0文档写了个入门实例,可供像我一样直接从lucene3.0开始学习的初学者参考!(变化大的地方用蓝字标出来了)
入门实例:
1.预处理:先把网上下载的一个《三国演义》电子书“三国演义.txt”(可用其他代替,呵呵)切割成多个小文件。
/**
* @author ht
* 预处理
*
*/
public class FilePreprocess {
public static void main(String[] arg){
String outputpath = "D:\\test\\small\\";//小文件存放路径
String filename = "D:\\test\\三国演义.txt";//原文件存放路径
if(!new File(outputpath).exists()){
new File(outputpath).mkdirs();
}
splitToSmallFiles(new File(filename), outputpath);
}
/**大文件切割为小的
* @param file
* @param outputpath
*/
public static void splitToSmallFiles(File file ,String outputpath){
int filePointer = 0;
int MAX_SIZE = 10240;//小文件大小
String filename = "output";//小文件的文件名前缀
BufferedWriter writer = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String line = reader.readLine();
while(line != null){
buffer.append(line).append("\r\n");
if(buffer.toString().getBytes().length>=MAX_SIZE){
writer = new BufferedWriter(new FileWriter(outputpath+filename+filePointer+".txt"));
writer.write(buffer.toString());
writer.close();
filePointer++;
buffer=new StringBuffer();
}
line = reader.readLine();
}
writer = new BufferedWriter(new FileWriter(outputpath+filename+filePointer+".txt"));
writer.write(buffer.toString());
writer.close();
System.out.println("The file hava splited to small files !");
} catch (FileNotFoundException e) {
System.out.println("file not found !");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.用lucene3.0生成索引类:用lencene3.0对生成的多个小文件进行索引,中文分词用的是lucene3.0自带的StandardAnalyzer.
/**
* @author ht
* 索引生成
*
*/
public class Indexer {
private static String INDEX_DIR = "D:\\test\\index";//索引存放目录
private static String DATA_DIR = "D:\\test\\small\\";//小文件存放的目录
public static void main(String[] args) throws Exception {
long start = new Date().getTime();
int numIndexed = index(new File(INDEX_DIR), new File(DATA_DIR));//调用index方法
long end = new Date().getTime();
System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");
}
/**索引dataDir下的.txt文件,并储存在indexDir下,返回索引的文件数量
* @param indexDir
* @param dataDir
* @return int
* @throws IOException
*/
public static int index(File indexDir, File dataDir) throws IOException {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir + " does not exist or is not a directory");
}
IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true,
IndexWriter.MaxFieldLength.LIMITED);
indexDirectory(writer, dataDir);//调用indexDirectory方法
int numIndexed = writer.numDocs();
writer.optimize();
writer.close();
return numIndexed;
}
/**循环遍历目录下的所有.txt文件并进行索引
* @param writer
* @param dir
* @throws IOException
*/
private static void indexDirectory(IndexWriter writer, File dir)
throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(writer, f); // recurse
} else if (f.getName().endsWith(".txt")) {
indexFile(writer, f);
}
}
}
/**对单个txt文件进行索引
* @param writer
* @param f
* @throws IOException
*/
private static void indexFile(IndexWriter writer, File f)
throws IOException {
if (f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
System.out.println("Indexing " + f.getCanonicalPath());
Document doc = new Document();
doc.add(new Field("contents",new FileReader(f)));
doc.add(new Field("filename",f.getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
}
3.查询类:查询“玄德”!
/**
* @author ht
* 查询
*
*/
public class Searcher {
private static String INDEX_DIR = "D:\\test\\index\\";//索引所在的路径
private static String KEYWORD = "玄德";//关键词
private static int TOP_NUM = 100;//显示前100条结果
public static void main(String[] args) throws Exception {
File indexDir = new File(INDEX_DIR);
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir +
" does not exist or is not a directory.");
}
search(indexDir, KEYWORD);//调用search方法进行查询
}
/**查询
* @param indexDir
* @param q
* @throws Exception
*/
public static void search(File indexDir, String q) throws Exception {
IndexSearcher is = new IndexSearcher(FSDirectory.open(indexDir),true);//read-only
String field = "contents";
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, new StandardAnalyzer(Version.LUCENE_CURRENT));
Query query = parser.parse(q);
TopScoreDocCollector collector = TopScoreDocCollector.create(TOP_NUM , false);
long start = new Date().getTime();// start time
is.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
System.out.println(hits.length);
for (int i = 0; i < hits.length; i++) {
Document doc = is.doc(hits[i].doc);//new method is.doc()
System.out.println(doc.getField("filename")+" "+hits[i].toString()+" ");
}
long end = new Date().getTime();//end time
System.out.println("Found " + collector.getTotalHits() +
" document(s) (in " + (end - start) +
" milliseconds) that matched query '" +
q + "':");
}
}
分享到:
相关推荐
这个“lucene入门小例子”很可能是为了帮助初学者理解并掌握Lucene的基本用法而设计的一系列示例代码。 Lucene的核心概念包括索引、文档、字段和查询。首先,你需要理解索引的概念,它类似于传统数据库中的索引,但...
Lucene入门与使用,非常简单,适合入门
lucenetest.rar,lucene,全文检索,lucene例子 lucenetest.rar,lucene,全文检索,lucene例子lucenetest.rar,lucene,全文检索,lucene例子
lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子
**Lucene 2.4 入门例子** Lucene 是一个高性能、全文本搜索库,由Apache软件基金会开发。它提供了强大的搜索功能,被广泛应用于各种应用中的信息检索。在这个入门例子中,我们将探讨Lucene 2.4版本的一些关键特性和...
**Lucene 入门** Lucene 是一个高性能、全文本搜索库,由Apache软件基金会开发。它是Java平台上的一个开源项目,广泛应用于各种搜索引擎的构建,包括网站内部搜索、数据库索引等。Lucene 提供了强大的文本分析、...
在这个经典Lucene入门模块中,我们将深入理解如何使用Lucene进行索引创建和搜索操作。 首先,我们来看Lucene如何建立数据的索引。这通常涉及以下几个步骤: 1. **索引创建**:使用 `IndexWriter` 对象来创建或更新...
lucene3.0 例子lucene3.0 例子 lucene3.0 例子 ,很好的学习,只有原代原,jar 包自己加上去就OK了
本篇将通过一个简单的入门例子,带你了解如何使用Lucene进行搜索。 首先,我们要知道Lucene的核心组件包括文档(Document)、字段(Field)、索引(Index)和查询(Query)。在Lucene中,信息是以文档的形式存储,...
Lucene5.2.1 入门学习例子. 这是别人的例子源码。可以参考。内有使用说明。
lucene文档例子
**Lucene 3.6 入门案例** Lucene 是一个高性能、全文本搜索库,由 Apache 软件基金会开发。它提供了完整的搜索功能,包括索引、查询、评分等,广泛应用于各种项目和产品中。在这个入门案例中,我们将深入理解如何...
【标题】:“Lucene3 实例解析” 在深入探讨Lucene3这个主题之前,我们先来了解一下Lucene是什么。Lucene是一个开源的全文检索库,由Apache软件基金会开发,广泛应用于各种信息检索系统中,包括搜索引擎、内容管理...
**Lucene 入门资料详解** Lucene 是一个开源全文搜索引擎库,由Apache软件基金会开发。它是Java编写的,但提供了与其他编程语言的接口。这个入门资料包将帮助你了解并掌握Lucene的核心概念和使用方法。 ### 一、...
lucene入门例子,有创建索引和检索。详情:http://blog.csdn.net/authorzhh/article/details/7869806
在这个项目例子中,我们将深入探讨如何利用Lucene进行索引构建、查询处理以及结果排序,从而创建一个功能完善的搜索引擎。 首先,我们需要了解Lucene的基本概念。Lucene的核心功能包括文本分析、索引构建和搜索。...
**Lucene入门学习文档** **一、什么是Lucene** Lucene是Apache软件基金会下的一个开源全文检索库,它提供了一个高性能、可扩展的信息检索服务。Lucene最初由Doug Cutting开发,现在已经成为Java社区中事实上的标准...
**全文搜索引擎Lucene入门** 全文搜索引擎Lucene是Apache软件基金会的一个开放源代码项目,它为Java开发者提供了一个高性能、可扩展的信息检索库。Lucene以其强大的文本搜索功能和高效的索引能力,在各种需要全文...