这里主要介绍一下如何快速的学习开发自己的第一个Lucene程序。
Lucene的开发主要分为两个步骤:
1、对需要检索的数据创建索引
2、根据关键字在索引查询到你需要的内容
首先我们来看一下如何利用Lucene创建索引:
public void createIndex() throws Exception {
// 你需要检索的文件路径
String filePath = "E:/work_cvs/itcastLucene/luceneDatasource/javadoc .txt";
// 你的索引库存放的位置
String indexPath = "E:/work_cvs/itcastLucene/luceneIndex/";
// 索引库的目录
Directory dir = FSDirectory.getDirectory(indexPath);
// 将电脑上的文档转换为Lucene中的文档类型
Document doc = File2DocumentUtils.file2Document(filePath);
// Lucene索引流的输入对象
IndexWriter indexWriter = new IndexWriter(dir, new StandardAnalyzer(), MaxFieldLength.LIMITED);
// 将文档对象加入到索引的输入流中
indexWriter.addDocument(doc);
// 合并优化索引
indexWriter.optimize();
// 关闭索引输入流
indexWriter.close();
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
public class File2DocumentUtils {
/**
* 磁盘中的文件对象转化为Lucene的文档对象
* @param filePath
* @return
*/
public static Document file2Document(String filePath){
File file = new File(filePath);
Document doc = new Document();
doc.add(new Field("name", file.getName(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("content", readFileContent(file), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("size", file.length()+"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
return doc;
}
/**
* <pre>
* 读取文件内容
* </pre>
*
* @param file 要读取的文件
* @return 文件内容
*/
public static String readFileContent(File file){
StringBuffer content = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
for(String line = null; (line = reader.readLine()) != null; ){
content.append(line).append("\n");
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return content.toString();
}
/**
* <pre>
* 获取 name 属性的值的两种方法:
* 1,Field f = doc.getField("name");
* f.stringValue();
* 2,doc.get("name");
* </pre>
*
* @param doc
*/
public static void printDocumentInfo(Document doc) {
// Field f = doc.getField("name");
// f.stringValue();
System.out.println("------------------------------");
System.out.println("name = " + doc.get("name"));
System.out.println("content = " + doc.get("content"));
System.out.println("size = " + doc.get("size"));
System.out.println("path = " + doc.get("path"));
}
}
下面是利用Lucene创建的索引库查询
public void search() throws Exception {
// 待查询的关键字
String queryString = "adddocument";
// 构建一个查询解析器
QueryParser queryParser = new MultiFieldQueryParser(new String[]{"name","content"}, new StandardAnalyzer());
// 利用查询解析器构建查询对象
Query query = queryParser.parse(queryString);
// 构建索引查询对象
IndexSearcher indexSearcher = new IndexSearcher(indexPath);
Filter filter = null;
// 查询出匹配的文档
TopDocs topDocs = indexSearcher.search(query, filter, 10000);
System.out.println("共搜索到【"+topDocs.totalHits+"】条匹配记录");
// 展示查询结果
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docSn = scoreDoc.doc;
Document doc = indexSearcher.doc(docSn);
File2DocumentUtils.printDocumentInfo(doc);
}
}
本文介绍如何使用Lucene进行文档索引及查询。通过实例演示了创建索引的过程,包括文件路径设置、文档转换、索引优化等关键步骤,并展示了如何进行高效检索。
671

被折叠的 条评论
为什么被折叠?



