Analyzer analyzer = new StandardAnalyzer();
/**
* 创建索引
*
* IndexWriter 是用来操作(增、删、改)索引库的
*/
@Test
public void createIndex() throws Exception {
// file --> doc
Document doc = File2DocumentUtils.file2Document(filePath);
// 建立索引
IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true,
MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}
/**
* 搜索
*
* IndexSearcher 是用来在索引库中进行查询的
*/
@Test
public void search() throws Exception {
// String queryString = "document";
String queryString = "adddocument";
// 1,把要搜索的文本解析为 Query
String[] fields = { "name", "content" };
QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = queryParser.parse(queryString);
// 2,进行查询
IndexSearcher indexSearcher = new IndexSearcher(indexPath);
Filter filter = null;
TopDocs topDocs = indexSearcher.search(query, filter, 10000);
System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
// 3,打印结果
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docSn = scoreDoc.doc; // 文档内部编号
Document doc = indexSearcher.doc(docSn); // 根据编号取出相应的文档
File2DocumentUtils.printDocumentInfo(doc); // 打印出文档信息
}
}
public static Document file2Document(String path) {
File file = new File(path);
Document doc = new Document();
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
doc.add(new Field("content", readFileContent(file), Store.YES, Index.ANALYZED));
doc.add(new Field("size", NumberTools.longToString(file.length()), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED));
return doc;
}
public static String readFileContent(File file) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer content = new StringBuffer();
for (String line = null; (line = reader.readLine()) != null;) {
content.append(line).append("\n");
}
return content.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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 = " + NumberTools.stringToLong(doc.get("size")));
System.out.println("path = " + doc.get("path"));
public class DirectoryTest {
public void test1()throws Exception {
// Directory dir = FSDirectory.getDirectory(indexPath);
Directory dir = new RAMDirectory();
Document doc = File2DocumentUtils.file2Document(filePath);
IndexWriter indexWriter = new IndexWriter(dir, analyzer, MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}
@Test
public void test2() throws Exception{
Directory fsDir = FSDirectory.getDirectory(indexPath);
// 1,启动时读取
Directory ramDir = new RAMDirectory(fsDir);
// 运行程序时操作 ramDir
IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
// 添加 Document
Document doc = File2DocumentUtils.file2Document(filePath);
ramIndexWriter.addDocument(doc);
ramIndexWriter.close();
// 2,退出时保存
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer,true, MaxFieldLength.LIMITED);
fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDir});
// fsIndexWriter.flush();
// fsIndexWriter.optimize();
fsIndexWriter.close();
}
@Test
public void test3() throws Exception{
Directory fsDir = FSDirectory.getDirectory(indexPath);
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, MaxFieldLength.LIMITED);
fsIndexWriter.optimize();
fsIndexWriter.close();
}
}
/**
* 创建索引
*
* IndexWriter 是用来操作(增、删、改)索引库的
*/
@Test
public void createIndex() throws Exception {
// file --> doc
Document doc = File2DocumentUtils.file2Document(filePath);
// 建立索引
IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true,
MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}
/**
* 搜索
*
* IndexSearcher 是用来在索引库中进行查询的
*/
@Test
public void search() throws Exception {
// String queryString = "document";
String queryString = "adddocument";
// 1,把要搜索的文本解析为 Query
String[] fields = { "name", "content" };
QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = queryParser.parse(queryString);
// 2,进行查询
IndexSearcher indexSearcher = new IndexSearcher(indexPath);
Filter filter = null;
TopDocs topDocs = indexSearcher.search(query, filter, 10000);
System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
// 3,打印结果
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docSn = scoreDoc.doc; // 文档内部编号
Document doc = indexSearcher.doc(docSn); // 根据编号取出相应的文档
File2DocumentUtils.printDocumentInfo(doc); // 打印出文档信息
}
}
public static Document file2Document(String path) {
File file = new File(path);
Document doc = new Document();
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
doc.add(new Field("content", readFileContent(file), Store.YES, Index.ANALYZED));
doc.add(new Field("size", NumberTools.longToString(file.length()), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED));
return doc;
}
public static String readFileContent(File file) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer content = new StringBuffer();
for (String line = null; (line = reader.readLine()) != null;) {
content.append(line).append("\n");
}
return content.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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 = " + NumberTools.stringToLong(doc.get("size")));
System.out.println("path = " + doc.get("path"));
public class DirectoryTest {
public void test1()throws Exception {
// Directory dir = FSDirectory.getDirectory(indexPath);
Directory dir = new RAMDirectory();
Document doc = File2DocumentUtils.file2Document(filePath);
IndexWriter indexWriter = new IndexWriter(dir, analyzer, MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}
@Test
public void test2() throws Exception{
Directory fsDir = FSDirectory.getDirectory(indexPath);
// 1,启动时读取
Directory ramDir = new RAMDirectory(fsDir);
// 运行程序时操作 ramDir
IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
// 添加 Document
Document doc = File2DocumentUtils.file2Document(filePath);
ramIndexWriter.addDocument(doc);
ramIndexWriter.close();
// 2,退出时保存
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer,true, MaxFieldLength.LIMITED);
fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDir});
// fsIndexWriter.flush();
// fsIndexWriter.optimize();
fsIndexWriter.close();
}
@Test
public void test3() throws Exception{
Directory fsDir = FSDirectory.getDirectory(indexPath);
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, MaxFieldLength.LIMITED);
fsIndexWriter.optimize();
fsIndexWriter.close();
}
}
本文介绍如何使用Lucene进行文档的索引创建及搜索功能实现。通过具体实例展示了如何将文件转换为索引文档,并利用QueryParser进行查询解析,最终通过IndexSearcher执行搜索并展示结果。
222

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



