Lucene3.5例子

原文地址:http://www.juziku.com/sunlightcs/wiki/4205.htm

Lucene3.5+IK分词器的例子,Lucene3.5改动有点大,很多方法都不推荐使用了。 
示例代码如下所示: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import  java.io.File;
import  java.io.IOException;
 
import  org.apache.lucene.analysis.Analyzer;
import  org.apache.lucene.document.Document;
import  org.apache.lucene.document.Field;
import  org.apache.lucene.index.IndexReader;
import  org.apache.lucene.index.IndexWriter;
import  org.apache.lucene.index.IndexWriterConfig;
import  org.apache.lucene.index.Term;
import  org.apache.lucene.queryParser.QueryParser;
import  org.apache.lucene.search.IndexSearcher;
import  org.apache.lucene.search.Query;
import  org.apache.lucene.search.ScoreDoc;
import  org.apache.lucene.search.TopDocs;
import  org.apache.lucene.search.highlight.Formatter;
import  org.apache.lucene.search.highlight.Highlighter;
import  org.apache.lucene.search.highlight.QueryScorer;
import  org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import  org.apache.lucene.store.Directory;
import  org.apache.lucene.store.FSDirectory;
import  org.apache.lucene.util.Version;
import  org.wltea.analyzer.lucene.IKAnalyzer;
import  org.wltea.analyzer.lucene.IKSimilarity;
 
 
/**
  * lucene3.5+ik的例子
  *
  * @author steven
  * @date   2012-3-1 下午2:12:24
  */
public  class  LuceneDemo {
     //索引文件位置
     File dataFile =  new  File( "D://indexFile" );
     //使用IK分词器
     Analyzer analyzer =  new  IKAnalyzer();
     
     
     public  void  bulidIndex(){
         Directory directory =  null ;
         IndexWriter writer =  null ;
         try  {
             directory = FSDirectory.open(dataFile);
             
             IndexWriterConfig writerConfig =  new IndexWriterConfig(Version.LUCENE_35, analyzer);
             writer =  new  IndexWriter(directory, writerConfig);
             
             writer.addDocument(addDocument( 1 "聚资库" "聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。" ));
             writer.addDocument(addDocument( 2 "聚资库" "聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。" ));
             writer.addDocument(addDocument( 3 "聚资库" "聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。" ));
             writer.addDocument(addDocument( 4 "资料" "聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。" ));
             writer.addDocument(addDocument( 5 "微知识" "聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。" ));
             
         } catch (Exception ex){
             ex.printStackTrace();
         finally  {
             try  {
                 writer.close();
                 directory.close();
             catch  (Exception e) {
                 e.printStackTrace();
             }
         }
     }
     
     
     /**
      * 添加Document
      */
     public  Document addDocument(Integer id, String title, String content) {
         Document doc =  new  Document();
         
         //Field.Index.NO 表示不索引
         //Field.Index.ANALYZED 表示分词且索引
         //Field.Index.NOT_ANALYZED 表示不分词且索引
         doc.add( new  Field( "id" , String.valueOf(id), Field.Store.YES, Field.Index.NOT_ANALYZED));
         doc.add( new  Field( "title" , title, Field.Store.YES, Field.Index.ANALYZED));
         doc.add( new  Field( "content" , content, Field.Store.YES, Field.Index.ANALYZED));
         
         return  doc;
     }
     
     
     /**
      * 更新索引
      */
     public  void  update(Integer id, String title, String content) {
         try  {          
             Directory directory = FSDirectory.open(dataFile);
             
             IndexWriterConfig writerConfig =  new IndexWriterConfig(Version.LUCENE_35, analyzer);
             IndexWriter writer =  new  IndexWriter(directory, writerConfig);
             
             Document doc = addDocument(id, title, content);
             
             Term term =  new  Term( "id" , String.valueOf(id));
             
             writer.updateDocument(term, doc);
             
             writer.close();
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
     
     
     /**
      * 删除索引
      */
     public  void  delete(Integer id) {
         try  {
             Directory directory = FSDirectory.open(dataFile);
             
             IndexWriterConfig writerConfig =  new IndexWriterConfig(Version.LUCENE_35, analyzer);
             IndexWriter writer =  new  IndexWriter(directory, writerConfig);
             Term term =  new  Term( "id" , String.valueOf(id));
             writer.deleteDocuments(term);
             
             writer.close();
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
     
     
     /**
      * 搜索
      * @param where   搜索条件   
      * @param after   分页时要用到,不分页时为null
      */
     public  void  search(String where, ScoreDoc after) {
         IndexSearcher isearcher =  null ;
         Directory directory =  null ;
         try  {
             directory = FSDirectory.open(dataFile);
             
             //创建索引搜索器 且只读
             IndexReader reader = IndexReader.open(FSDirectory.open(dataFile), true ); 
             isearcher =  new  IndexSearcher(reader);
             
             //在索引器中使用IKSimilarity相似度评估器
             isearcher.setSimilarity( new  IKSimilarity());
             
             QueryParser parser =  new QueryParser(Version.LUCENE_35,  null , analyzer);
             Query query = parser.parse(where);
             
             //lucene3.5深度分页,每页显示10条记录
             TopDocs topDocs = isearcher.searchAfter(after, query,  10 );
             ScoreDoc[] hits = topDocs.scoreDocs;
             
             //关键字高亮
             Formatter formatter =  new  SimpleHTMLFormatter( "" "" );
             Scorer scorer =  new  QueryScorer(query);
             Highlighter highlighter =  new Highlighter(formatter, scorer);
 
             for (ScoreDoc scoreDoc : hits){
                 Document hitDoc = isearcher.doc(scoreDoc.doc);
                 String id = hitDoc.get( "id" );
                 String title = hitDoc.get( "title" );
                 String content = hitDoc.get( "content" );
                 float  score = scoreDoc.score;
                 
                 title = highlighter.getBestFragment(analyzer, "title" , title);
                 content = highlighter.getBestFragment(analyzer,  "content" , content);
                 
                 if (title ==  null ){
                     title = hitDoc.get( "title" );
                 }
                 
                 if (content ==  null ){
                     content = hitDoc.get( "content" );
                 }
                 
                 System.out.println( "doc:"  + scoreDoc.doc + "    score:"  + score +  "   id:"  + id +  "   title:"  + title + "    content:"  + content);
             }
             
         catch  (Exception e) {
             throw  new  RuntimeException(e);
         finally  {
             try  {
                 isearcher.close();
                 directory.close();
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
         
     }
}


测试代码如下所示: 
?
1
2
3
4
5
6

你可能感兴趣的:(java)