辅助 lucene 的文件输入类:
FileList 用于读取一个文件夹里所有的文件路径
package tool;
import java.io.*;
public class FileList {
private static final String SEP = "/";
private static StringBuffer sb = new StringBuffer("");
public static String [] getFiles(File f) throws IOException{
if(f.isDirectory()){
File[] fs = f.listFiles();
for(int i = 0;i<fs.length;i++){
getFiles(fs[i]);
}
}
else
{
sb.append(f.getPath()+SEP);
}
String s = sb.toString();
String [] list = s.split(SEP);
return list;
}
public static String [] getFiles(String t)throws IOException{
File f = new File(t);
if(f.isDirectory()){
File[] fs = f.listFiles();
for(int i=0;i<fs.length;i++){
getFiles(fs[i]);
}
}
else
{
sb.append(f.getPath()+SEP);
}
String s = sb.toString();
String [] list = s.split(SEP);
return list;
}
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
String s[] = FileList.getFiles("F:\\test\\lucene-3.5.0\\lib");
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
}
}
FileText用于读取文件里的内容。
package tool;
import java.io.*;
public class FileText {
public static String getText(File f){
StringBuffer sb = new StringBuffer("");
try{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while (s != null){
sb.append(s);
s = br.readLine();
}
br.close();
}
catch(Exception e){
sb.append("");
}
return sb.toString();
}
public static String getText(String s){
String t = "";
try{
File f = new File(s);
t = getText(f);
}
catch(Exception e){
t = "";
}
return t;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String s = FileText.getText("text.htm");
System.out.println(s);
}
}
将一个文件夹里所有的文件都建立引索的引索器:
package jim.java.iSearch2;
import java.io.File;
import java.io.IOException;
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 tool.FileList;
import tool.FileText;
public class Index {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String indexPath = "index";
IndexWriter writer = null;
String [] files = null;
try {
writer = new IndexWriter(indexPath,new StandardAnalyzer());//创建索引器
} catch (IOException e) {
System.out.println("索引建立失败!");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
files = FileList.getFiles("Files");//建立信息字段对象
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("检索资源文件打开失败");
e.printStackTrace();
}
int num = files.length;
for( int i = 0 ; i < num ; i++ ){
Document doc = new Document();//建立文档对象
File file = new File(files[i]);
String name = file.getName();
Field field = new Field("name",name,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
String content = FileText.getText(file);
field = new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
String path = file.getPath();
field = new Field("Path",path,Field.Store.YES,Field.Index.NO);
doc.add(field);
System.out.println("File: "+name+" Indexed");
try {
writer.addDocument(doc);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("将Document写入IndexWriter时错误!");
e.printStackTrace();
}
}
try {
writer.close();//关闭引索器
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IndexWriter关闭时错误!");
e.printStackTrace();
}
System.out.println("Index is Created!");
}
}
搜索器的实现:
package jim.java.testlucene;
import java.io.IOException;
import org.apache.lucene.index.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.search.*;
import org.apache.lucene.document.*;
public class testSearch {
/**
* @param args
*/
@SuppressWarnings("null")
public static void main(String[] args) {
// TODO Auto-generated method stub
String indexPath = "index";//索引目录的地址
String searchField = "content";//需要索引的字段
String searchPhrase = "love";//需要搜索的关键词
IndexSearcher searcher = null;
Hits hs = null;
Document doc = null;
StringBuffer sb = new StringBuffer();//建立StringBuffer对象
try {
searcher = new IndexSearcher(indexPath);//建立IndexSearcher对象
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("打开索引目录时错误!");
e.printStackTrace();
}
///
Term t = new Term(searchField,searchPhrase); //封装搜
Query q = new TermQuery(t); //索条件
//
try {
hs = searcher.search(q);//反馈搜索结果
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("执行搜索时发生错误!");
e.printStackTrace();
}
int num = hs.length();
//以下代码的作用是 : 从Hits对象中获得搜索结果并显示出来
for(int i = 0;i < num ; i++){
try {
doc = hs.doc(i);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("循环搜索时发生错误!");
e.printStackTrace();
}
Field fname = doc.getField("title");
sb.append("title: "+"\n");
sb.append(fname.stringValue()+"\n");
Field fcontent = doc.getField("content");
sb.append("content: "+"\n");
sb.append(fcontent.stringValue()+"\n");
sb.append("------------"+"\n");
}
try {
searcher.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("关闭搜索时发生错误!");
e.printStackTrace();
}
System.out.print(sb);
}
}
今天的收获:
对lucene的了解更进了一步,了解了索引器和搜索器的建立步骤。
今天的不足:
没能好好地看看关于线程方面的知识。