<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-20  評論-3  文章-9  trackbacks-0

    package com.laozizhu.article.util;

    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import javax.sql.DataSource;
    import net.paoding.analysis.analyzer.PaodingAnalyzer;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.queryParser.MultiFieldQueryParser;
    import org.apache.lucene.search.BooleanClause;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocCollector;

    /**
    * 基于庖丁解牛的Lucene 2.4的全文搜索代碼。
    *
    * @author 老紫竹研究室(laozizhu.com)
    */
    public class LucenePaoDing {
    private static final String indexPath = "d:/indexpaoding/www.laozizhu.com";

    /**
    ?? * @param args
    ?? * @throws Exception
    ?? */
    public static void main(String[] args) throws Exception {
    ??? rebuildAll();
    ??? String keyword = "Spring.jar";
    ??? LucenePaoDing l = new LucenePaoDing();
    ??? System.out.println("索引搜索\n------------------------------");
    ??? System.out.println(l.seacherIndex(keyword));
    }

    public static void rebuildAll() {
    ??? synchronized (indexPath) {
    ????? LucenePaoDing l = new LucenePaoDing();
    ????? DataSource ds = (DataSource) Factory.getBean("dataSource");
    ????? Connection con = null;
    ????? Statement stat = null;
    ????? ResultSet rs = null;
    ????? try {
    ??????? con = ds.getConnection();
    ??????? stat = con.createStatement();
    ??????? rs = stat.executeQuery("select id,subject,content from t_article");
    ??????? if (rs != null) {
    ????????? l.Index(rs);
    ??????? }
    ????? } catch (Exception ex) {
    ??????? ex.printStackTrace();
    ????? } finally {
    ??????? if (rs != null) {
    ????????? try {
    ??????????? rs.close();
    ????????? } catch (Exception ex) {}
    ??????? }
    ??????? if (stat != null) {
    ????????? try {
    ??????????? stat.close();
    ????????? } catch (Exception ex) {}
    ??????? }
    ??????? if (con != null) {
    ????????? try {
    ??????????? con.close();
    ????????? } catch (Exception ex) {}
    ??????? }
    ????? }
    ??? }
    }

    public synchronized Analyzer getAnalyzer() {
    ??? return new PaodingAnalyzer();
    }

    private synchronized void Index(ResultSet rs) {// 通過結(jié)果集就可以獲得數(shù)據(jù)源了
    ??? try {
    ????? IndexWriter writer = new IndexWriter(indexPath, getAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
    ????? writer.setMaxFieldLength(10000000);
    ????? Date start = new Date();
    ????? int index = 1;
    ????? while (rs.next()) {
    ??????? Document doc = new Document();// 一個文檔相當與表的一條記錄
    ??????? doc.add(new Field("id", rs.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));// 字段id放的是數(shù)據(jù)庫表中的id,lucene的一條記錄的一個字段下的數(shù)據(jù)可以放多個值,這點與數(shù)據(jù)庫表不同
    ??????? doc.add(new Field("subject", rs.getString("subject"), Field.Store.YES, Field.Index.ANALYZED));
    ??????? doc.add(new Field("content", rs.getString("content"), Field.Store.YES, Field.Index.ANALYZED));
    ??????? writer.addDocument(doc);
    ??????? if (index++ == 1000) {
    ????????? writer.commit();
    ????????? index = 0;
    ??????? }
    ????? }
    ????? writer.commit();
    ????? writer.optimize();// 優(yōu)化
    ????? writer.close();// 一定要關(guān)閉,否則不能把內(nèi)存中的數(shù)據(jù)寫到文件
    ????? Date end = new Date();
    ????? System.out.println("重建索引成功?。。。? + "用時" + (end.getTime() - start.getTime()) + "毫秒");
    ??? } catch (IOException e) {
    ????? System.out.println(e);
    ??? } catch (SQLException e) {
    ????? System.out.println(e);
    ??? }
    }

    public void IndexSingle(long id, String subject, String content) {// 通過結(jié)果集就可以獲得數(shù)據(jù)源了
    ??? synchronized (indexPath) {
    ????? try {
    ??????? IndexWriter writer = new IndexWriter(indexPath, getAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED);
    ??????? writer.setMaxFieldLength(10000000);
    ??????? Date start = new Date();
    ??????? Document doc = new Document();// 一個文檔相當與表的一條記錄
    ??????? doc.add(new Field("id", Long.toString(id), Field.Store.YES, Field.Index.NOT_ANALYZED));// 字段id放的是數(shù)據(jù)庫表中的id,lucene的一條記錄的一個字段下的數(shù)據(jù)可以放多個值,這點與數(shù)據(jù)庫表不同
    ??????? doc.add(new Field("subject", subject, Field.Store.YES, Field.Index.ANALYZED));
    ??????? doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
    ??????? writer.addDocument(doc);
    ??????? // writer.optimize();// 優(yōu)化
    ??????? writer.close();// 一定要關(guān)閉,否則不能把內(nèi)存中的數(shù)據(jù)寫到文件
    ??????? Date end = new Date();
    ??????? System.out.println("索引建立成功?。。。? + "用時" + (end.getTime() - start.getTime()) + "毫秒");
    ????? } catch (IOException e) {
    ??????? System.out.println(e);
    ????? }
    ??? }
    }

    /**
    ?? * 最主要的搜索方法。
    ?? *
    ?? * @param queryString
    ?? * @return
    ?? */
    public List<Long> seacherIndex(String queryString) {// 根據(jù)關(guān)鍵字搜索
    ??? try {
    ????? IndexSearcher isearcher = new IndexSearcher(indexPath);
    ????? /* 下面這個表示要同時搜索這兩個域,而且只要一個域里面有滿足我們搜索的內(nèi)容就行 */
    ????? BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
    ????? TopDocCollector collector = new TopDocCollector(10);
    ????? Query query = MultiFieldQueryParser.parse(queryString, new String[] { "subject", "content" }, clauses, getAnalyzer());
    ????? isearcher.search(query, collector);
    ????? ScoreDoc[] hits = collector.topDocs().scoreDocs;
    ????? List<Long> rtn = new ArrayList<Long>();
    ????? Long id;
    ????? int docId;
    ????? for (int i = 0; i < hits.length; i++) {
    ??????? docId = hits[i].doc;
    ??????? Document doc = isearcher.doc(docId);
    ??????? id = Long.parseLong(doc.get("id").trim());
    ??????? if (!rtn.contains(id)) {
    ????????? rtn.add(id);
    ??????? }
    ????? }
    ????? isearcher.close();
    ????? return rtn;
    ??? } catch (Exception e) {
    ????? e.printStackTrace();
    ????? return null;
    ??? }
    }
    }

    posted on 2009-03-09 17:24 藍山 閱讀(484) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 成人免费在线观看网站| 国产L精品国产亚洲区久久| 亚洲AV无码国产精品永久一区| 四虎永久在线精品免费观看地址 | 亚洲精品视频久久久| 99久久免费国产精品热| 亚洲人成毛片线播放| 亚洲欧洲日本在线| 中文字幕无码播放免费| 一级毛片免费不卡| 亚洲伊人色一综合网| 亚洲一区二区三区AV无码| 国产又黄又爽又猛免费app| xxxxx做受大片视频免费| 亚洲三级视频在线| 激情综合色五月丁香六月亚洲| 成人黄色免费网站| 精品视频一区二区三区免费| 亚洲精品自偷自拍无码| 久久夜色精品国产亚洲AV动态图| 最新69国产成人精品免费视频动漫| 精品免费久久久久国产一区| 亚洲色无码国产精品网站可下载| 国产AV无码专区亚洲AV毛网站 | 亚洲熟女少妇一区二区| 天天天欲色欲色WWW免费| 久久久久国产精品免费网站| 色噜噜噜噜亚洲第一| 亚洲国产品综合人成综合网站| 亚洲国产一二三精品无码| 日韩精品视频免费在线观看| 最近2019中文字幕免费直播| 国产黄在线播放免费观看| 国产精品亚洲小说专区| 国产亚洲精品VA片在线播放| 亚洲色图视频在线观看| 曰韩亚洲av人人夜夜澡人人爽| 国产成人无码区免费A∨视频网站 国产成人涩涩涩视频在线观看免费 | 日韩a毛片免费观看| 亚洲一区二区三区写真| 亚洲综合无码一区二区|