欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lucene学习笔记1:创建索引并查询

程序员文章站 2022-07-09 13:45:57
...
package testLucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

public class TestIndexHello {

public void testIndex() throws IOException {
Date date1 = new Date();

IndexWriter writer = new IndexWriter("E:\\index\\index",
new StandardAnalyzer(), true);
File file = new File("E:\\test");

if (file.isDirectory()) {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
Document doc = new Document();
BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(fileList[i]),
"UTF-8"));
// Reader reader = new BufferedReader(new
// FileReader(fileList[i]));
Field field1 = new Field("content", reader);
Field field2 = new Field("path", fileList[i].getAbsolutePath(),
Field.Store.YES, Field.Index.TOKENIZED);
doc.add(field1);
doc.add(field2);

TokenStream ts = new StandardAnalyzer().tokenStream("content",
reader);
Token tk = ts.next();
while (tk != null) {
System.out.print(tk.term() + " ");
tk = ts.next();
}
writer.addDocument(doc);
}
}
writer.close();
Date date2 = new Date();
System.out.println("");
System.out.println("建立索引用了" + (date2.getTime() - date1.getTime())
+ "毫秒");
}

public void helloSearch() throws Exception {
IndexSearcher seach = new IndexSearcher("E:\\index\\index");
// System.out.println("请输入查询条件:");
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
// BufferedReader br = new BufferedReader(new
// InputStreamReader(System.in));
// Query query = parser.parse(br.readLine());
String str = new String("world");
Query query = parser.parse(str);
Hits hits = seach.search(query);
System.out.println("'" + str + "'" + "正在搜索...");
if (hits.length() == 0) {
System.out.println("没有找到符合条件的结果");
} else {
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println("搜索结果是:" + doc.get("content"));
System.out.println("文件路径是" + doc.get("path"));
}
}

}

public static void main(String[] args) {
TestIndexHello tindex = new TestIndexHello();
try {
tindex.testIndex();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tindex.helloSearch();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}