lucene练习

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();
}
}

你可能感兴趣的:(F#,Lucene)