- public void index(List<IArticle> list)
- {
- //IArticle接口提供getName(標(biāo)題)和getContent(內(nèi)容)
- //list就是從數(shù)據(jù)庫(kù)里查詢出來(lái)的要建立索引的對(duì)象的列表
- if(list != null && list.size() > 0)
- {
- try {
- //標(biāo)記是否重新建立索引,true為重新建立索引
- boolean flag = true;
- //如果已存在索引,則追加索引
- if(IndexReader.indexExists(path))
- {
- flag = false;
- }
- ChineseAnalyzer ca = new ChineseAnalyzer();
- IndexWriter indexWriter = new IndexWriter("c:/lucene/index",ca,flag);
- Document doc = null;
- for(int i=0;i<list.size();i++)
- {
- doc = new Document();
- doc.add(new Field("title",article.getName(),Field.Store.YES,Field.Index.TOKENIZED));
- //添加內(nèi)容屬性,內(nèi)容只索引,不存儲(chǔ)
- doc.add(new Field("content",new StringReader(list.get(i).getContent())));
- indexWriter.addDocument(doc);
- }
- //優(yōu)化并關(guān)閉
- indexWriter.optimize();
- indexWriter.close();
- } catch (Exception e)
- {
- // TODO 自動(dòng)生成 catch 塊
- //e.printStackTrace();
- }
- }
- }
簡(jiǎn)單說(shuō)下需要注意的地方:
1.ChineseAnalyzer ca = new ChineseAnalyzer();這個(gè)是分析器,Lucene內(nèi)置多個(gè),處理中文搜索我會(huì)用ChineseAnalyzer.
2.IndexWriter indexWriter = new IndexWriter(c:/lucene/index,ca,true);處理索引的類,注意其構(gòu)造方法里的最后一個(gè)參數(shù),如果為true則表示,下次建立索引時(shí)會(huì)清除這次建立的索引重新建立索引,如果為false則表示追加索引,在原來(lái)索引的基礎(chǔ)上追加.看實(shí)際情況定true或false.
3.doc.add(new Field("title",article.getName(),Field.Store.YES,Field.Index.TOKENIZED));這一句表示為文章標(biāo)題建立索引并存儲(chǔ).
4.doc.add(new Field("content",new StringReader(list.get(i).getContent())));這句是為內(nèi)容建立索引但不存儲(chǔ)
這樣我們就為文章對(duì)象建立好索引了,然后就可以利用Lucene的其他類對(duì)這個(gè)索引目錄進(jìn)行搜索了,關(guān)于搜索部分我們稍后再補(bǔ)充上.
下面是搜索部分的代碼,寫的簡(jiǎn)陋了點(diǎn),比較簡(jiǎn)單,不再多說(shuō),請(qǐng)參看注釋:
- public class Search
- {
- //定義一個(gè)索引搜索類對(duì)象
- private IndexSearcher searcher = null;
- //定義一個(gè)Query對(duì)象
- private Query query = null;
- //定義中文分析器
- ChineseAnalyzer analyzer = new ChineseAnalyzer();
- //構(gòu)造方法里完成searcher的實(shí)例化
- public Search()
- {
- try
- {
- //這里的參數(shù)就是上面我們生成索引的目錄
- searcher = new IndexSearcher(IndexReader.open("c:/lucene/index"));
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public Hits search(String keyword) throws Exception
- {
- //開始搜索的時(shí)間
- Date start = new Date();
- //對(duì)我們索引的content字段進(jìn)行搜索
- QueryParser qp = new QueryParser("content",analyzer);
- this.query = qp.parse(keyword);
- Hits hits = this.searcher.search(query);
- Date end = new Date();
- System.out.println("檢索完成,用時(shí)"+(end.getTime()-start.getTime())+"毫秒");
- //////////打印測(cè)試////////
- if(hits != null && hits.length() > 0)
- {
- for(int i = 0; i < hits.length(); i++)
- {
- try
- {
- Document doc = hits.doc(i);
- System.out.println("結(jié)果"+(i+1)+":"+doc.get("title")+" createTime:"+doc.get("content"));
- //System.out.println(doc.get("path"));
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- return hits;
- }
- ///調(diào)用,主方法
- public static void main(String[] args)
- {
- try
- {
- Search test = new Search();
- Hits h = test.search("你好");
- } catch (Exception e)
- {
- // TODO 自動(dòng)生成 catch 塊
- e.printStackTrace();
- }
- }
- }
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】