ITeye博客

防暴力破解

wbj0110 · 2014-07-02 09:25 · Lucene · 编程语言 · Lucene · Markdown

包:

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

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。