`
endual
  • 浏览: 3506882 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Lucene自带示例精简

 
阅读更多

Lucene自带示例精简,只留下了主要代码。以备查看
对文件夹生成索引

Java代码  收藏代码
  1. package  zhch.illq.lucene;  
  2.   
  3. import  java.io.File;  
  4. import  java.io.FileReader;  
  5. import  java.io.IOException;  
  6.   
  7. import  net.paoding.analysis.analyzer.PaodingAnalyzer;  
  8.   
  9. import  org.apache.lucene.analysis.standard.StandardAnalyzer;  
  10. import  org.apache.lucene.document.DateTools;  
  11. import  org.apache.lucene.document.Document;  
  12. import  org.apache.lucene.document.Field;  
  13. import  org.apache.lucene.index.IndexWriter;  
  14. import  org.apache.lucene.store.FSDirectory;  
  15. import  org.apache.lucene.util.Version;  
  16.   
  17. public   class  LuceneIndex {  
  18.   
  19.     static   final  File INDEX_DIR =  new  File( "d:\\temp\\index" );  
  20.   
  21.     // 主要代码 索引docDir文件夹下文档,索引文件在INDEX_DIR文件夹中   
  22.     public   static   void  main(String[] args) {  
  23.         File docDir = new  File( "d:\\temp\\neirong" );  
  24.         try  {  
  25.             IndexWriter standardWriter = new  IndexWriter(FSDirectory.open(INDEX_DIR),  new  StandardAnalyzer(  
  26.                     Version.LUCENE_CURRENT), true , IndexWriter.MaxFieldLength.LIMITED);  
  27.             // 如果是索引中文内容,可以使用Paoding中文分词器   
  28.             IndexWriter writer = new  IndexWriter(FSDirectory.open(INDEX_DIR),  new  PaodingAnalyzer(),  true ,  
  29.                     IndexWriter.MaxFieldLength.LIMITED);  
  30.             String[] files = docDir.list();  
  31.             for  (String fileStr : files) {  
  32.                 File file = new  File(docDir, fileStr);  
  33.                 if  (!file.isDirectory()) {  
  34.                     writer.addDocument(document(file));  
  35.                 }  
  36.             }  
  37.             writer.optimize();  
  38.             writer.close();  
  39.   
  40.         } catch  (IOException e) {  
  41.             System.out.println(" caught a "  + e.getClass() +  "\n with message: "  + e.getMessage());  
  42.         }  
  43.     }  
  44.   
  45.     public   static  Document document(File f)  throws  java.io.FileNotFoundException {  
  46.   
  47.         Document doc = new  Document();  
  48.   
  49.         // 添加path,索引(可查询)但不切词   
  50.         doc.add(new  Field( "path" , f.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));  
  51.   
  52.         // 添加最后修改日期   
  53.         doc.add(new  Field( "modified" , DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE),  
  54.                 Field.Store.YES, Field.Index.NOT_ANALYZED));  
  55.   
  56.         // 添加内容,指定一个Reader,文件内容解析但不存储,这里的Reader使用系统默认的编码读入   
  57.         doc.add(new  Field( "contents" new  FileReader(f)));  
  58.         return  doc;  
  59.     }  
  60. }  


对索引进行查询

Java代码  收藏代码
  1. package  zhch.illq.lucene;  
  2.   
  3. import  java.io.BufferedReader;  
  4. import  java.io.File;  
  5. import  java.io.IOException;  
  6. import  java.io.InputStreamReader;  
  7.   
  8. import  net.paoding.analysis.analyzer.PaodingAnalyzer;  
  9.   
  10. import  org.apache.lucene.analysis.Analyzer;  
  11. import  org.apache.lucene.analysis.standard.StandardAnalyzer;  
  12. import  org.apache.lucene.document.Document;  
  13. import  org.apache.lucene.index.IndexReader;  
  14. import  org.apache.lucene.queryParser.QueryParser;  
  15. import  org.apache.lucene.search.IndexSearcher;  
  16. import  org.apache.lucene.search.Query;  
  17. import  org.apache.lucene.search.ScoreDoc;  
  18. import  org.apache.lucene.search.Searcher;  
  19. import  org.apache.lucene.search.TopScoreDocCollector;  
  20. import  org.apache.lucene.store.FSDirectory;  
  21. import  org.apache.lucene.util.Version;  
  22.   
  23. public   class  LuceneSearch {  
  24.   
  25.     /** Simple command-line based search demo. */   
  26.     public   static   void  main(String[] args)  throws  Exception {  
  27.   
  28.         String index = "d:\\temp\\index" ;  
  29.         String field = "contents" ;  
  30.         String queries = null ;  
  31.         boolean  raw =  false ;  
  32.         // 要显示条数   
  33.         int  hitsPerPage =  10 ;  
  34.   
  35.         // searching, so read-only=true   
  36.         IndexReader reader = IndexReader.open(FSDirectory.open(new  File(index)),  true );  // only   
  37.   
  38.         Searcher searcher = new  IndexSearcher(reader);  
  39.         Analyzer standardAnalyzer = new  StandardAnalyzer(Version.LUCENE_CURRENT);  
  40.         // 如果是索引中文内容,可以使用Paoding中文分词器   
  41.         Analyzer analyzer = new  PaodingAnalyzer();  
  42.   
  43.         BufferedReader in = new  BufferedReader( new  InputStreamReader(System.in));  
  44.         QueryParser parser = new  QueryParser(field, analyzer);  
  45.         while  ( true ) {  
  46.             if  (queries ==  null // prompt the user   
  47.                 System.out.println("Enter query: " );  
  48.   
  49.             String line = in.readLine();  
  50.   
  51.             if  (line ==  null  || line.length() == - 1 )  
  52.                 break ;  
  53.   
  54.             line = line.trim();  
  55.             if  (line.length() ==  0 )  
  56.                 break ;  
  57.   
  58.             Query query = parser.parse(line);  
  59.             System.out.println("Searching for: "  + query.toString(field));  
  60.   
  61.             doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null );  
  62.         }  
  63.         reader.close();  
  64.     }  
  65.   
  66.     public   static   void  doPagingSearch(BufferedReader in, Searcher searcher, Query query,  int  hitsPerPage,  boolean  raw,  
  67.             boolean  interactive)  throws  IOException {  
  68.   
  69.         TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, false );  
  70.         searcher.search(query, collector);  
  71.         ScoreDoc[] hits = collector.topDocs().scoreDocs;  
  72.   
  73.         int  end, numTotalHits = collector.getTotalHits();  
  74.         System.out.println(numTotalHits + " total matching documents" );  
  75.   
  76.         int  start =  0 ;  
  77.   
  78.         end = Math.min(hits.length, start + hitsPerPage);  
  79.   
  80.         for  ( int  i = start; i < end; i++) {  
  81.             Document doc = searcher.doc(hits[i].doc);  
  82.             String path = doc.get("path" );  
  83.             if  (path !=  null ) {  
  84.                 System.out.println((i + 1 ) +  ". "  + path);  
  85.                 System.out.println("   modified: "  + doc.get( "modified" ));  
  86.   
  87.             } else  {  
  88.                 System.out.println((i + 1 ) +  ". "  +  "No path for this document" );  
  89.             }  
  90.   
  91.         }  
  92.   
  93.     }  
  94.   

分享到:
评论

相关推荐

    lucene示例 demo+jar包

    lucene全文检索示例 demo+jar包

    lucene3.0.3搜索的使用示例

    lucene3.0.3搜索的使用示例lucene3.0.3搜索的使用示例lucene3.0.3搜索的使用示例

    Lucene7.4官方示例

    Lucene7.4官方示例,内含若干官方实例,可用于学习Lucene

    Lucene与DB结合示例

    今天发一个简单的和数据库交互的Lucene示例,只是初步的靠Lucene自带的分词实现中文分词,效果肯定没有网上琳琅的分词器相媲美,只为了示例,用了自带的高亮。页面不好看,将就一下哦。 主要是由 Spring + Struts1 +...

    Lucene6.6.2API示例

    Lucene6.6.2代码示例,处理的原始数据类型是数据库,内含中文分词器IKAnalyzer-6.5.0.jar,支持Lucene6.x以上版本

    lucene facet查询示例

    lucene facet查询示例,区间查询多dim查询,key-field-value模型了解

    Lucene简单实例记录

    Lucene简单实例记录 简单Luncene的示例应用

    Lucene示例 BM25相似度计算

    用lucene 4.7.1做的一个Lucene构建索引、进行查询,对比默认的相似度计算与BM25相似度计算输出结果的示例。内容不多,供新手参考

    lucene 站内搜索示例

    完整的lucene示例,用于搜索指定网站内的所有url资源 能很好的嵌入在你自己的项目中,实现站内搜索 very good ,vert strong

    Lucene使用代码实例之搜索文档

    对于初学者很快掌握lucene 该文档详细的介绍了如何使用lucene 以及快速理解掌握lucene

    Lucene 全文检索的 各种例子

    各种分词器都用了,有ikAnalyzer和smartChineseAnalyzer等等分词器。使用jdk7,Lucene-core4.5。非常不错。

    lucene 3.0 java示例

    自己写的一个Java lucene查询数据库的示例,内部有数据库脚本,SqlServer 2005数据库

    lucene入门代码示例

    最受欢迎的java开源全文搜索引擎开发工具包。 提供了完整的查询引擎和... Lucene的目的是为软件开发人员提供一个简单易用的工具包, 以方便在目标系统中实现全文检索功能, 或者是以此为基础建立起完整的全文检索引擎。

    lucene 索引小示例

    NULL 博文链接:https://blaiu.iteye.com/blog/588054

    lucene4.0实例

    自己做的一些 lucene4.0的 demo 希望能帮助各位 写的不好 亲们 别喷我

    盘古分词、lucene3.0.3搜索的使用示例.zip

    盘古分词 lucene3.0.3 使用 示例 可以方便地整合到项目中使用,.net 4.0的。

    lucene示例代码

    易懂的lucene示例代码,可以用来做开发

    lucene IndexSearcher相关和查询示例

    一步一步跟我学习lucene是对近期做lucene索引的总结,大家有问题的话联系本人的Q-Q: 891922381,同时本人新建Q-Q群:106570134(lucene,solr,netty,hadoop),如蒙加入,不胜感激,大家共同探讨,本人争取每日一博,...

    盘古分词、lucene3.0.3搜索的使用示例v1.2

    盘古分词 lucene3.0.3 使用 示例 可以方便地整合到项目中使用,.net 4.0的。新加分页功能。

    lucene实例lucene实例

    lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例

Global site tag (gtag.js) - Google Analytics