为常见格式的文件建立索引:
package jim.Lucene35;
import java.io.File;
import java.io.IOException;
import jim.Figurer.Figurer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import tool.FileList;
import tool.FileText;
public class Lucene35 {
/**
* @param args
*/
public static void main(String[] args) {
new creatIndex("index");
}
}
class creatIndex{
Directory directory = null; //用来决定引索目录的储存方式
IndexWriter writer = null; //引索器
Document document = null; //引索文件
Field field = null; //字段信息
IndexWriterConfig iwc = null; //用来选择lucene的版本以及分词器的版本
String title = "title"; //文件的标题
String content = "content"; //文件的内容
String [] files = null; //用来记录文件夹里所有文件的地址
@SuppressWarnings("static-access")
public creatIndex(String indexPath){ //构造器indexPath引索目录的储存地址
try {
directory = FSDirectory.open(new File(indexPath)); //创建directory,其储存方式为在硬盘上储存
} catch (IOException e) {
System.out.println("创建Directory时发生错误!");
e.printStackTrace();
}
iwc = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer()); //选择lucene的版本以及分词器的版本
try {
writer = new IndexWriter(directory,iwc); //创建引索器
} catch (CorruptIndexException e) {
System.out.println("创建IndexWriter时发生错误!");
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
files = FileList.getFiles("testFiles"); //记录文件夹里所有文件的地址
for(int i = 0;i<files.length;i++){
Figurer figurer = new Figurer(); //建立Figure用来分析不同类型的文件
File file = new File(files[i]);
try {
figurer.toTextFile(files[i], "jim\\"+file.getName()+".txt"); //将不同类型的文件转换为txt格式
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
System.out.println("无法打开存放要搜索的文件的文件夹");
e.printStackTrace();
}
try {
files = FileList.getFiles("jim"); //存放要搜索的文件的文件夹
} catch (IOException e1) {
e1.printStackTrace();
} //记录文件夹里所有文件的地址
int num = files.length; //记录文件夹里所有文件的总数
for( int i = 0; i < num ; i++ ){
document = new Document(); //创建索引文件
File file = new File(files[i]);
title = file.getName(); //取得文件的名字
field = new Field("title",title,Field.Store.YES,Index.NOT_ANALYZED); //创建索引字段
document.add(field);
content = FileText.getText(file);
field = new Field("content",content,Field.Store.NO,Index.ANALYZED);
document.add(field);
String Path = file.getPath(); //获取文件的路径
field = new Field("path",Path,Field.Store.YES,Index.NOT_ANALYZED);
document.add(field);
field = new Field("id",String.valueOf(i),Field.Store.YES,Index.NOT_ANALYZED);
document.add(field);
System.out.println("File: "+title+" Indexed");
try {
writer.addDocument(document);
} catch (CorruptIndexException e) {
System.out.println("将Document写入IndexWriter时错误!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
writer.close(); //关闭索引器
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("IndexWriter关闭时错误!");
e.printStackTrace();
}
System.out.println("Index is Created!");
}
}
建立搜索:
package jim.Lucene35;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
class Search {
Directory directory = null; //存储方式
IndexReader reader = null; //读入引索
IndexSearcher searcher = null; //确定搜索对象
QueryParser parser = null; //用于确定搜索时的引索的版本以及分词器
Query query = null; //记录要搜索的词语
TopDocs tds = null; //记录搜索后返回的结果
Document document = null; //存放搜索结果以便于提取结果
ScoreDoc[] sds = null; //存放TopDocs传来的内容(搜索结果)
public Search(String indexPath,int num){ //indexPath引索存放的目录,num为显示搜索结果的条数
try {
directory = FSDirectory.open(new File(indexPath)); //创建directory,其储存方式为在硬盘上储存
} catch (IOException e) {
System.out.println("创建Directory时发生错误!");
e.printStackTrace();
}
try {
reader = IndexReader.open(directory); //读入索引
} catch (CorruptIndexException e) {
System.out.println("创建IndexReader时发生错误!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("创建IndexReader时发生错误!");
e.printStackTrace();
}
searcher = new IndexSearcher(reader);
parser = new QueryParser(Version.LUCENE_35,"content",new IKAnalyzer());
try {
query = parser.parse("Pierre"); //设置搜索的关键词
} catch (ParseException e) {
System.out.println("query = parser.parse(\"keyword\")时发生错误");
e.printStackTrace();
}
try {
tds = searcher.search(query,num); //设置显示的行数
} catch (IOException e) {
System.out.println("std = searcher.search(query,5);时发生错误");
e.printStackTrace();
}
sds = tds.scoreDocs;
System.out.println("一共搜索到: "+sds.length+" 条");
if(sds.length != 0){
for( ScoreDoc sd:sds){
try {
document = searcher.doc(sd.doc);
} catch (CorruptIndexException e) {
System.out.println("document = searcher.doc(sd.doc);时发生错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("document = searcher.doc(sd.doc);时发生错误");
e.printStackTrace();
}
System.out.println(document.get("title")+"["+document.get("path")+"]");
}
}
else
System.out.println("The word you enter can't be found!");
try {
reader.close();
} catch (IOException e) {
System.out.println("关闭reader时发生错误!");
e.printStackTrace();
}
System.out.println("Finished");
}
public void check() throws IOException{ //检查索引是否被正确建立(打印索引)
directory = FSDirectory.open(new File("index"));
IndexReader reader = IndexReader.open(directory);
for(int i = 0;i<reader.numDocs();i++){
System.out.println(reader.document(i));
}
}
}
public class Searcher {
public static void main(String [] args){
new Search("index",10);
}
}
今天的收获:
终于把对常见文件建立索引这个功能弄出来了,而且能够检索一个大的XML文件.
今天的不足:
今天偷了点小懒,看了一晚上的动漫.