Lucene4.5索引的创建与搜索

最近再看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类,如下

  1. packagewww.lucene.com;
  2. importjava.io.BufferedReader;
  3. importjava.io.File;
  4. importjava.io.FileInputStream;
  5. importjava.io.IOException;
  6. importjava.io.InputStreamReader;
  7. importorg.apache.lucene.analysis.standard.StandardAnalyzer;
  8. importorg.apache.lucene.document.Document;
  9. importorg.apache.lucene.document.Field;
  10. importorg.apache.lucene.document.StringField;
  11. importorg.apache.lucene.document.TextField;
  12. importorg.apache.lucene.index.IndexWriter;
  13. importorg.apache.lucene.index.IndexWriterConfig;
  14. importorg.apache.lucene.store.Directory;
  15. importorg.apache.lucene.store.FSDirectory;
  16. importorg.apache.lucene.util.Version;
  17. publicclassIndex{
  18. /**
  19. *@创建索引
  20. */
  21. publicvoidcreateIndex(){
  22. IndexWriterwriter=null;
  23. try{
  24. //1、创建Directory对象
  25. Directorydir=FSDirectory.open(newFile("I:/lucene/indexed"));
  26. //2、创建indexWrite
  27. writer=newIndexWriter(dir,newIndexWriterConfig(Version.LUCENE_45,newStandardAnalyzer(Version.LUCENE_45)));
  28. //3、创建document对象
  29. Documentdocument=null;
  30. //4、将要索引的文件已Field形式添加到document
  31. Filefiles=newFile("I:/lucene/index");
  32. for(Filefile:files.listFiles()){
  33. document=newDocument();
  34. document.add(newStringField("fileName",file.getName(),Field.Store.YES));//为文件名创建索引,存储
  35. document.add(newStringField("filePath",file.getAbsolutePath(),Field.Store.YES));//为文件路径创建索引,存储
  36. document.add(newTextField("content",newBufferedReader(
  37. newInputStreamReader(
  38. newFileInputStream(file),"UTF-8"))));//为内容创建索引,但不存储
  39. writer.addDocument(document);
  40. }
  41. }catch(IOExceptione){
  42. e.printStackTrace();
  43. }finally{
  44. if(writer!=null){
  45. try{
  46. writer.close();
  47. }catch(IOExceptione){
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }
  53. }

5、创建查询索引的类,如下:

  1. packagewww.lucene.com;
  2. importjava.io.File;
  3. importjava.io.IOException;
  4. importorg.apache.lucene.analysis.standard.StandardAnalyzer;
  5. importorg.apache.lucene.document.Document;
  6. importorg.apache.lucene.index.DirectoryReader;
  7. importorg.apache.lucene.index.IndexReader;
  8. importorg.apache.lucene.queryparser.classic.ParseException;
  9. importorg.apache.lucene.queryparser.classic.QueryParser;
  10. importorg.apache.lucene.search.IndexSearcher;
  11. importorg.apache.lucene.search.Query;
  12. importorg.apache.lucene.search.ScoreDoc;
  13. importorg.apache.lucene.search.TopDocs;
  14. importorg.apache.lucene.store.Directory;
  15. importorg.apache.lucene.store.FSDirectory;
  16. importorg.apache.lucene.util.Version;
  17. publicclassSearcher{
  18. /**
  19. *@搜索
  20. */
  21. publicvoidsearchIndex(){
  22. try{
  23. //1.创建Directory
  24. Directorydir=FSDirectory.open(newFile("i:/lucene/indexed"));
  25. //2.创建IndexReader
  26. IndexReaderreader=DirectoryReader.open(dir);
  27. //3.根据IndexReader创建IndexSearcher
  28. IndexSearchersearcher=newIndexSearcher(reader);
  29. //4.创建搜索的Query
  30. //创建parser来确定搜索的内容,第二个参数表示搜索的域
  31. QueryParserparser=newQueryParser(Version.LUCENE_45,"content",newStandardAnalyzer(Version.LUCENE_45));
  32. //创建query,表示搜索域中包含'Directory'的文档
  33. Queryquery=parser.parse("Directory");
  34. //5.根据search搜索返回TopDocs,要设置返回条数
  35. TopDocsdocs=searcher.search(query,10);
  36. //6.根据TopDocs获取ScoreDoc
  37. for(ScoreDocdoc:docs.scoreDocs){
  38. //7.根据searcher和scoredoc获取具体的Document对象
  39. Documentdocument=searcher.doc(doc.doc);
  40. //8.根据Document对象获取需要的内容
  41. System.out.println(document.get("fileName")+"["+document.get("filePath")+"]");
  42. }
  43. //9.关闭reader
  44. reader.close();
  45. }catch(IOExceptione){
  46. e.printStackTrace();
  47. }catch(ParseExceptione){
  48. e.printStackTrace();
  49. }
  50. }
  51. }

6、新建一个测试类,来测试这两个,不过先要执行创建索引的,然后查询,如下:

  1. packagewww.lucene.com;
  2. publicclassTestLucene{
  3. publicstaticvoidmain(String[]args){
  4. Indexindex=newIndex();
  5. index.createIndex();
  6. Searchersearcher=newSearcher();
  7. searcher.searchIndex();
  8. }
  9. }

7、执行结果:

(本文转自http://blog.csdn.net/yu_han_23/article/details/12348285)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值