您的位置:首页 > 其它

lucene笔记(搜索的小例子)

2007-03-04 13:59 309 查看
今天上午本来再看火箭VS马刺的比赛,但是火箭打得太烂了,第一节看完我就关了,没事可做我就开始看我买的新书Ajax+lucene,看了看lucene部分的前几章,发现我下载的lucene2.0和书中用到的lucene1.4差别挺大的,在这里把我在网上查到的修改的方法写出来,以后查找就方便多了。

首先在你的c盘建立一个test文件加,里面放入四个.txt文件 其中a.txt输入“中华人民共和国” b.txt输入“人民共和国” c.txt输入“人民” d.txt输入“共和国”。c盘下在建立一个index文件,只是作为索引存放目录的。

然后在eclipse中新建一个java项目,输入项目名称LuceneHelloWord,然后点击下一步,找到“库”然后“添加外部jar ”,把你的lucene包添加进去就可以了,我的是lucene-core-2.0.0.jar。

在项目中建三个类,Constants.java是用来存储一些常量的类,如索引文件的路径和索引文件的存放位置

代码


package testlucene;






public class Constants ...{


// 要索引的文件的存放路径


public final static String INDEX_FILE_PATH = "c:/test";




// 索引的存放位置


public final static String INDEX_STORE_PATH = "c:/index";




}

LuceneIndex.java是用来对文件建立索引的类。代码


package testlucene;




import java.io.BufferedReader;


import java.io.File;


import java.io.FileInputStream;


import java.io.InputStreamReader;


import java.io.Reader;


import java.util.Date;


import org.apache.lucene.index.IndexWriter;


import org.apache.lucene.analysis.standard.StandardAnalyzer;


import org.apache.lucene.document.Document;


import org.apache.lucene.document.Field;






public class LuceneIndex ...{






public static void main(String[] args) throws Exception ...{


// 声明一个对象


LuceneIndex indexer = new LuceneIndex();


// 建立索引


Date start = new Date();


indexer.writeToIndex();


Date end = new Date();


System.out


.println("建立索引时用:" + (end.getTime() - start.getTime()) + "毫秒");


// 关闭索引器


indexer.close();


}






public LuceneIndex() ...{




try ...{


writer = new IndexWriter(Constants.INDEX_STORE_PATH,


new StandardAnalyzer(), true);




} catch (Exception e) ...{


e.printStackTrace();


}


}




// 索引器


private IndexWriter writer = null;




// 将要建立索引的文件构造成一个Document对象,并添加一个域"Content"




private Document getDocument(File f) throws Exception ...{


// 生成文档对象


Document doc = new Document();


// 获取文件流


FileInputStream is = new FileInputStream(f);


Reader reader = new BufferedReader(new InputStreamReader(is));


// 添加索引内容


doc.add(new Field("contents", reader));


doc.add(new Field("path", f.getCanonicalPath(), Field.Store.YES,


Field.Index.NO));


return doc;


}






public void writeToIndex() throws Exception ...{


File folder = new File(Constants.INDEX_FILE_PATH);




if (folder.isDirectory()) ...{


String[] files = folder.list();




for (int i = 0; i < files.length; i++) ...{


File file = new File(folder, files[i]);


Document doc = getDocument(file);


System.out.println("正在建立索引" + file + "");


writer.addDocument(doc);


}


}


}






public void close() throws Exception ...{


writer.close();


}




}



luceenSearch.java适用于检索索引的类。代码:


package testlucene;




import java.util.Date;




import org.apache.lucene.analysis.standard.StandardAnalyzer;


import org.apache.lucene.document.Document;


import org.apache.lucene.index.IndexReader;


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 LuceneSearch ...{




public static void main(String args[])throws Exception...{


LuceneSearch test = new LuceneSearch();


//检索“人民”这个关键词


Hits h = null;


h = test.search("华人");


test.printResult(h);


h = test.search("共和");


test.printResult(h);


h = test.search("人");


test.printResult(h);


}




public LuceneSearch()...{




try...{


searcher = new IndexSearcher(IndexReader.open(Constants.INDEX_STORE_PATH));




}catch(Exception e)...{


e.printStackTrace();


}


}


//声明一个IndexSearcher对象


private IndexSearcher searcher = null;


//声明一个Query对象


private Query query = null;






public final Hits search(String keyword)...{


System.out.println("正在检索关键字:"+keyword);




try...{


//将关键字包装成Query对象


query = new QueryParser("contents",new StandardAnalyzer()).parse(keyword);




Date start = new Date();


Hits hits = searcher.search(query);


Date end = new Date();


System.out.println("检索完成,用时:"+(end.getTime()-start.getTime())+"毫秒");


return hits;




}catch (Exception e)...{


e.printStackTrace();


return null;


}


}




public void printResult(Hits h)...{




if(h.length()==0)...{


System.out.println("对不起,没有找到您要的结果。");


}


else




...{




for(int i=0;i<h.length();i++)...{




try...{


Document doc = h.doc(i);


System.out.print("这是第"+ i + "个检索到的结果,文件名为:");


System.out.println(doc.get("path"));




}catch(Exception e)...{


e.printStackTrace();


}


}


}


System.out.println("-------------------------------");


}


}

在LuceneIndex.java中,


doc.add(new Field("contents", reader));


doc.add(new Field("path", f.getCanonicalPath(), Field.Store.YES,


Field.Index.NO));

这段代码在lucene1.4中是


doc.add(Field.Text("contents",reader));


doc.add(Field.keyword("path", f.getSbsolutePath()));

是因为lucene2.0相对于1.4版来说,Field类变化较大,在lucene1.4中分为四种:Keyword、Noindexed、Nostoreed和Text,而在lucene2.0中分为了Index、Store和TermVector三大类,每个大类下还分几个小类。

我对于Field的几个分类没有理解清楚(API都是英文的很难看懂)。

在luceenSearch.java中的代码


query = new QueryParser("contents",new StandardAnalyzer()).parse(keyword);

在lucene1.4中是


query = QueryParser.parse(keyword,"contents",new StandardAnalyzer());

运行LuceneIndex.java(点击运行方式,运行java应用程序)可看到如下结果:

正在建立索引c:/test/a.txt
正在建立索引c:/test/b.txt
正在建立索引c:/test/c.txt
正在建立索引c:/test/d.txt
建立索引时用:63毫秒

运行luceenSearch.java(点击运行方式,运行java应用程序)可看到如下结果:

正在检索关键字:华人
检索完成,用时:31毫秒
这是第0个检索到的结果,文件名为:C:/test/a.txt
-------------------------------
正在检索关键字:共和
检索完成,用时:0毫秒
这是第0个检索到的结果,文件名为:C:/test/d.txt
这是第1个检索到的结果,文件名为:C:/test/b.txt
这是第2个检索到的结果,文件名为:C:/test/a.txt
-------------------------------
正在检索关键字:人
检索完成,用时:0毫秒
这是第0个检索到的结果,文件名为:C:/test/c.txt
这是第1个检索到的结果,文件名为:C:/test/b.txt
这是第2个检索到的结果,文件名为:C:/test/a.txt
-------------------------------

附lucene下载地址:

lucene下载地址http://www.apache.org/dyn/closer.cgi/jakarta/lucene

或者直接点这个ftp ftp://apache.mirrors.pair.com/jakarta/lucene 都可以

lucene-core-2.0.0.jar是需要用到的文件,lucene-2.0.0-src.zip或者lucene-2.0.0.zip压缩包都可以,还可以直接下载jar文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: