最近再看Lucene,在网上也看了不少的例子,现在把我做的拿出来。
1、首先去http://mirrors.cnnic.cn/apache/lucene/java/4.5.0/下载lucene4.5的jar包。解压出来
2、在MyEclipse里面新建一个JAVA项目,将以下jar包拷入项目,其中junit-4.11.jar是测试单元的jar包
3、在I盘创建文件夹lucene,在里面创建连个文件夹,一个index(放要创建的文件),一个indexed(放保存索引),在index文件夹里面放待创建索引的文件
4、新建一个创建索引的Index类,如下
- packagewww.lucene.com;
- importjava.io.BufferedReader;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.IOException;
- importjava.io.InputStreamReader;
- importorg.apache.lucene.analysis.standard.StandardAnalyzer;
- importorg.apache.lucene.document.Document;
- importorg.apache.lucene.document.Field;
- importorg.apache.lucene.document.StringField;
- importorg.apache.lucene.document.TextField;
- importorg.apache.lucene.index.IndexWriter;
- importorg.apache.lucene.index.IndexWriterConfig;
- importorg.apache.lucene.store.Directory;
- importorg.apache.lucene.store.FSDirectory;
- importorg.apache.lucene.util.Version;
- publicclassIndex{
- /**
- *@创建索引
- */
- publicvoidcreateIndex(){
- IndexWriterwriter=null;
- try{
- //1、创建Directory对象
- Directorydir=FSDirectory.open(newFile("I:/lucene/indexed"));
- //2、创建indexWrite
- writer=newIndexWriter(dir,newIndexWriterConfig(Version.LUCENE_45,newStandardAnalyzer(Version.LUCENE_45)));
- //3、创建document对象
- Documentdocument=null;
- //4、将要索引的文件已Field形式添加到document
- Filefiles=newFile("I:/lucene/index");
- for(Filefile:files.listFiles()){
- document=newDocument();
- document.add(newStringField("fileName",file.getName(),Field.Store.YES));//为文件名创建索引,存储
- document.add(newStringField("filePath",file.getAbsolutePath(),Field.Store.YES));//为文件路径创建索引,存储
- document.add(newTextField("content",newBufferedReader(
- newInputStreamReader(
- newFileInputStream(file),"UTF-8"))));//为内容创建索引,但不存储
- writer.addDocument(document);
- }
- }catch(IOExceptione){
- e.printStackTrace();
- }finally{
- if(writer!=null){
- try{
- writer.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- }
- }
- }
5、创建查询索引的类,如下:
- packagewww.lucene.com;
- importjava.io.File;
- importjava.io.IOException;
- importorg.apache.lucene.analysis.standard.StandardAnalyzer;
- importorg.apache.lucene.document.Document;
- importorg.apache.lucene.index.DirectoryReader;
- importorg.apache.lucene.index.IndexReader;
- importorg.apache.lucene.queryparser.classic.ParseException;
- importorg.apache.lucene.queryparser.classic.QueryParser;
- importorg.apache.lucene.search.IndexSearcher;
- importorg.apache.lucene.search.Query;
- importorg.apache.lucene.search.ScoreDoc;
- importorg.apache.lucene.search.TopDocs;
- importorg.apache.lucene.store.Directory;
- importorg.apache.lucene.store.FSDirectory;
- importorg.apache.lucene.util.Version;
- publicclassSearcher{
- /**
- *@搜索
- */
- publicvoidsearchIndex(){
- try{
- //1.创建Directory
- Directorydir=FSDirectory.open(newFile("i:/lucene/indexed"));
- //2.创建IndexReader
- IndexReaderreader=DirectoryReader.open(dir);
- //3.根据IndexReader创建IndexSearcher
- IndexSearchersearcher=newIndexSearcher(reader);
- //4.创建搜索的Query
- //创建parser来确定搜索的内容,第二个参数表示搜索的域
- QueryParserparser=newQueryParser(Version.LUCENE_45,"content",newStandardAnalyzer(Version.LUCENE_45));
- //创建query,表示搜索域中包含'Directory'的文档
- Queryquery=parser.parse("Directory");
- //5.根据search搜索返回TopDocs,要设置返回条数
- TopDocsdocs=searcher.search(query,10);
- //6.根据TopDocs获取ScoreDoc
- for(ScoreDocdoc:docs.scoreDocs){
- //7.根据searcher和scoredoc获取具体的Document对象
- Documentdocument=searcher.doc(doc.doc);
- //8.根据Document对象获取需要的内容
- System.out.println(document.get("fileName")+"["+document.get("filePath")+"]");
- }
- //9.关闭reader
- reader.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }catch(ParseExceptione){
- e.printStackTrace();
- }
- }
- }
6、新建一个测试类,来测试这两个,不过先要执行创建索引的,然后查询,如下:
- packagewww.lucene.com;
- publicclassTestLucene{
- publicstaticvoidmain(String[]args){
- Indexindex=newIndex();
- index.createIndex();
- Searchersearcher=newSearcher();
- searcher.searchIndex();
- }
- }
7、执行结果:
(本文转自http://blog.csdn.net/yu_han_23/article/details/12348285)