---
title: "防暴力破解"
author: "wbj0110"
date: "2014-07-02T09:25:00Z"
tags: ["Lucene"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2016-03-12T13:27:29Z"
id: 2087418
url: "https://soledede.iteye.com/blog/2087418"
---

包：

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代码  [![收藏代码](http://qq510219366.iteye.com/images/icon_star.png)]()

01. package com.nanjing.chaoxing.lucene;
02. import java.io.File;
03. import java.io.FileReader;
04. import java.io.IOException;
05. import org.apache.lucene.analysis.Analyzer;
06. import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
07. import org.apache.lucene.document.Document;
08. import org.apache.lucene.document.Field;
09. 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 &gt; 0) {
45. long time1 = System.currentTimeMillis();
46. for (int i = 0; i &lt; 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 &lt; 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