`
wbj0110
  • 浏览: 1612372 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

防暴力破解

阅读更多

包:

lucene-analyzers-3.6.1.jar

lucene-core-3.6.1.jar

lucene-highlighter-3.6.1.jar

lucene-smartcn-3.6.1.jar

 

 

 

Java代码  收藏代码
  1. package com.nanjing.chaoxing.lucene;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6.   
  7. import org.apache.lucene.analysis.Analyzer;  
  8. import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;  
  9. import org.apache.lucene.document.Document;  
  10. import org.apache.lucene.document.Field;  
  11. import org.apache.lucene.index.IndexReader;  
  12. import org.apache.lucene.index.IndexWriter;  
  13. import org.apache.lucene.index.IndexWriterConfig;  
  14. import org.apache.lucene.queryParser.MultiFieldQueryParser;  
  15. import org.apache.lucene.queryParser.ParseException;  
  16. import org.apache.lucene.queryParser.QueryParser;  
  17. import org.apache.lucene.search.IndexSearcher;  
  18. import org.apache.lucene.search.Query;  
  19. import org.apache.lucene.search.ScoreDoc;  
  20. import org.apache.lucene.search.TopDocs;  
  21. import org.apache.lucene.store.Directory;  
  22. import org.apache.lucene.store.FSDirectory;  
  23. import org.apache.lucene.util.Version;  
  24. import org.junit.Test;  
  25.   
  26. public class LuceneStudy {  
  27.         private String dataSourceFile = "D:\\test\\luceneDataSource";  
  28.         private File indexFile=new File("D:\\test\\luceneIndex");  
  29.         //创建简单中文分析器    
  30.         private Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_36,true);    
  31.         private String[] queryString={"filepath","content"};    
  32.         /**  
  33.          * 创建索引  
  34.          *  
  35.          * @throws IOException  
  36.          */    
  37.         @Test    
  38.         public void createIndex() throws IOException {    
  39.             //创建索引目录    
  40.             Directory directory = FSDirectory.open(indexFile);    
  41.             //建立索引创建类    
  42.             IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);    
  43.             indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);  //总是重新创建索引  
  44.             IndexWriter writer = new IndexWriter(directory, indexWriterConfig);    
  45.         
  46.             //建立索引    
  47.             File[] files = new File(dataSourceFile).listFiles();    
  48.             if (files.length > 0) {    
  49.                 long time1 = System.currentTimeMillis();    
  50.                 for (int i = 0; i < files.length; i++) {    
  51.                     Document document = new Document();    
  52.                     document.add(new Field("content"new FileReader(files[i])));    
  53.                     document.add(new Field("filepath", files[i].getName(), Field.Store.YES, Field.Index.ANALYZED));    
  54.                     writer.addDocument(document);    
  55.                 }    
  56.                 long time2 = System.currentTimeMillis();    
  57.                 System.out.println("创建了" + writer.numDocs() + "索引");    
  58.                 System.out.println("一共花了" + (time2 - time1) + "时间");    
  59.             }    
  60.         
  61.             writer.close();    
  62.         }    
  63.         
  64.         /**  
  65.          * 搜索文档  
  66.          * @throws IOException  
  67.          * @throws ParseException  
  68.          */    
  69.         @Test    
  70.         public void search() throws IOException, ParseException {    
  71.         
  72.             IndexReader indexReader = IndexReader.open(FSDirectory.open(indexFile));    
  73.             //创建搜索类    
  74.             IndexSearcher indexSearcher = new IndexSearcher(indexReader);    
  75.             QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_36, queryString, analyzer);    
  76.             Query query = queryParser.parse("大幅度");    
  77.             TopDocs topDocs = indexSearcher.search(query, 10000);    
  78.             System.out.println("一共查到:" + topDocs.totalHits + "记录");    
  79.             ScoreDoc[] scoreDoc = topDocs.scoreDocs;    
  80.         
  81.             for (int i = 0; i < scoreDoc.length; i++) {    
  82.                 //内部编号    
  83.                 int doc = scoreDoc[i].doc;    
  84.                 System.out.println("内部编号:" + doc);    
  85.                 //根据文档id找到文档    
  86.                 Document mydoc = indexSearcher.doc(doc);    
  87.                 System.out.println("文档路径:" + mydoc.get("filepath"));    
  88.             }    
  89.         }    
  90.         
  91.         
  92.     }    

http://qq510219366.iteye.com/blog/1673188

 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics