包:

lucene-analyzers-3.6.1.jar

lucene-core-3.6.1.jar

lucene-highlighter-3.6.1.jar

lucene-smartcn-3.6.1.jar

 

1. package
2.   
3. import
4. import
5. import
6.   
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17. import
18. import
19. import
20. import
21. import
22. import
23. import
24. import
25.   
26. public class
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
39. //创建索引目录  
40.             Directory directory = FSDirectory.open(indexFile);    
41. //建立索引创建类  
42. new
43. //总是重新创建索引
44. new
45.         
46. //建立索引  
47. new
48. if (files.length > 0) {    
49. long
50. for (int i = 0; i < files.length; i++) {    
51. new
52. new Field("content", new
53. new Field("filepath", files[i].getName(), Field.Store.YES, Field.Index.ANALYZED));    
54.                     writer.addDocument(document);    
55.                 }    
56. long
57. "创建了" + writer.numDocs() + "索引");    
58. "一共花了" + (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
71.         
72.             IndexReader indexReader = IndexReader.open(FSDirectory.open(indexFile));    
73. //创建搜索类  
74. new
75. new
76. "大幅度");    
77. 10000);    
78. "一共查到:" + topDocs.totalHits + "记录");    
79.             ScoreDoc[] scoreDoc = topDocs.scoreDocs;    
80.         
81. for (int i = 0; i < scoreDoc.length; i++) {    
82. //内部编号  
83. int
84. "内部编号:"
85. //根据文档id找到文档  
86.                 Document mydoc = indexSearcher.doc(doc);    
87. "文档路径:" + mydoc.get("filepath"));    
88.             }    
89.         }    
90.         
91.         
92.     }