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

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

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

    敬的世界

    常用鏈接

    統(tǒng)計(jì)

    最新評(píng)論

    用Lucene 實(shí)現(xiàn)全文檢索

    來(lái)源:http://bbs.bc-cn.net/thread-180603-1-1.html


    在本文我又提到lucene了,在java業(yè)界,提到全文檢索,幾乎沒(méi)有什么人不知道它。
    用google搜索一下,滿世界都是有關(guān)資料。具有代表性的就是車東的“基于Java的全文索引引擎Lucene簡(jiǎn)介”,
    我要寫的也就只有最簡(jiǎn)單的三板斧,再加上支持中文的ChineseAnalyzer以及按照時(shí)間排序的搜索結(jié)果排序方法。
    這些都可以在其他地方找到相關(guān)資料,我只是把他們提出來(lái),作為lucence應(yīng)用中經(jīng)常遇到的麻煩解決辦法。
    去年MSN上面有個(gè)朋友跟我提到希望用lucene構(gòu)建個(gè)網(wǎng)站的全文檢索,我當(dāng)時(shí)就覺(jué)得很簡(jiǎn)單,直說(shuō)沒(méi)問(wèn)題沒(méi)問(wèn)題,
    不過(guò)他提到一個(gè)要求就是搜索結(jié)果要安裝時(shí)間排序,我查閱了些資料,發(fā)現(xiàn)lucene并不提供用戶自定義排序方式,
    而只能按照自己相關(guān)性算法排序。后來(lái)我在車東的weblucene項(xiàng)目找到了IndexOrderSearcher。
    解決了結(jié)果排序常規(guī)需求。
    IndexOrderSearcher跟一般IndexSearch使用差不多,僅僅在構(gòu)建對(duì)象的時(shí)候多加一個(gè)參數(shù)IndexOrderSearcher.ORDER_BY_DOCID_DESC
    IndexOrderSearcher indexsearcher = new IndexOrderSearcher("/home/lucenetest/index",IndexOrderSearcher.ORDER_BY_DOCID_DESC);
    新版本的lucene還提供了一個(gè)MultiFieldQueryParser,可以同時(shí)檢索多個(gè)字段,以前QueryParser比較麻煩。
    private static ChineseAnalyzer chineseAnalyzer = new ChineseAnalyzer();
    public Hits search(String queryText){
    if (queryText == null){
    return null;
    }
    Query query;
    try{
    query = MultiFieldQueryParser.parse(queryText, new String[]{"title"},chineseAnalyzer);
    return indexsearcher.search(query);
    }catch(Exception e){
    return null;
    }
    }
    下面是構(gòu)建索引,定時(shí)從數(shù)據(jù)庫(kù)取出數(shù)據(jù)索引,做完記錄完成時(shí)間,我是把時(shí)間寫入一個(gè)txt文件。
    package com.test.search;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.cn.*;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.*;
    import org.apache.lucene.index.*;

    import java.io.*;
    import java.sql.*;
    import java.util.Date;

    import com.test.db.*;
    import com.test.utility.*;

    /**
    * Title: SearchIndexer
    * Description: 全文索引
    * Copyright: Copyright (c) 2001
    * Company: test
    * @author Sean
    * @version 1.0
    */
    public class SearchIndexer {
    private String indexPath = null;
    protected Analyzer analyzer = new ChineseAnalyzer();

    public SearchIndexer(String s) {
    this.indexPath = s;
    }
    /**
    * 索引某日期以前的所有文檔
    * @param fromdate
    * @return
    */
    public final void updateIndex(String fromdate) {
    Connection conn = DbUtil.getCon();
    IndexWriter indexWriter = null;
    try {
    indexWriter = getWriter(false);
    //索引發(fā)布系統(tǒng)內(nèi)部文件
    PreparedStatement pstm = conn.prepareStatement(
    "select title,body,creationtime from document where creationtime > '" + fromdate +
    "' order by creationtime");
    ResultSet rs = pstm.executeQuery();
    while (rs.next()) {
    String creationtime = rs.getString("creationtime");
    String title = rs.getString("title");
    String body = rs.getString("body");


    if (title == null || body == null) {
    continue;
    }
    try {
    addDocsToIndex(title,body, creationtime,indexWriter);
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    indexWriter.optimize();
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    finally {
    try {
    indexWriter.close();
    conn.close();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    /**
    * 檢查索引文件是否存在
    * @param s
    * @return 索引是否存在
    */
    private boolean indexExists(String s) {
    File file = new File(s + File.separator + "segments");
    return file.exists();
    }
    /**
    * 增加一組索引
    * @param title
    * @param body
    * @param creationtime
    * @param indexwriter
    * @return
    */
    private final void addNewsToIndex(String docid, String url,String title, String body,
    String ptime, IndexWriter indexwriter) throws
    IOException {
    if (indexwriter == null) {
    return;
    }
    else {
    try {
    Document document = new Document();
    document.add(Field.Text("title", title));
    document.add(Field.Text("body", body));
    document.add(new Field("creationtime", creationtime, true, true, false));
    indexwriter.addDocument(document);
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    return;
    }
    }
    /**
    * 取得IndexWriter
    * @param flag 是否新建索引
    * @return IndexWriter
    */
    private IndexWriter getWriter(boolean flag) throws IOException {
    String s = indexPath;
    if (s == null) {
    throw new IOException("索引文件路徑設(shè)置錯(cuò)誤.");
    }
    indexPath = s + File.separator + "search";
    IndexWriter indexwriter = null;
    if (flag) {
    try {
    indexwriter = new IndexWriter(indexPath, analyzer, true);
    }
    catch (Exception exception) {
    System.err.println("ERROR: Failed to create a new index writer.");
    exception.printStackTrace();
    }
    }
    else {
    if (indexExists(indexPath)) {
    try {
    indexwriter = new IndexWriter(indexPath, analyzer, false);
    }
    catch (Exception exception1) {
    System.err.println("ERROR: Failed to open an index writer.");
    exception1.printStackTrace();
    }
    }
    else {
    try {
    indexwriter = new IndexWriter(indexPath, analyzer, true);
    }
    catch (Exception exception2) {
    System.err.println("ERROR: Failed to create a new index writer.");
    exception2.printStackTrace();
    }
    }
    }
    return indexwriter;
    }

    public static void main(String[] args) {
    String lastUpdate = "/home/lucenetest/lastUpdate.txt";
    SearchIndexer searchIndexer = new SearchIndexer("/home/lucenetest/index");
    //取出上次更新時(shí)間
    String str = Util.readTxtFile(lastUpdate);
    if(str==null || str.length()==0){
    str = new java.util.Date().toString();
    }
    searchIndexer.updateIndex(str);
    //寫入當(dāng)前時(shí)間
    Util.writeTxtFile(lastUpdate,new java.util.Date(),false);
    }
    }
    寫個(gè)cmd或者sh在相應(yīng)操作系統(tǒng)下面定時(shí)執(zhí)行SearchIndexer就可以了。

    posted on 2009-01-28 23:56 picture talk 閱讀(180) 評(píng)論(0)  編輯  收藏


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费播放春色aⅴ视频| 一个人免费观看www视频| 99久久免费中文字幕精品| 国产AⅤ无码专区亚洲AV| 一级做a爰片久久毛片免费看 | 色噜噜的亚洲男人的天堂| 最近的免费中文字幕视频| 337p日本欧洲亚洲大胆艺术| 久久久精品免费国产四虎| 国产V亚洲V天堂无码| 国产一精品一AV一免费| 亚洲成AV人片在线观看ww| 午夜理伦剧场免费| 精品亚洲麻豆1区2区3区| 亚洲美女免费视频| 国产v亚洲v天堂a无| 国产婷婷高清在线观看免费| 精品免费AV一区二区三区| 亚洲性在线看高清h片| 两个人看的www免费视频| 久久亚洲精品中文字幕| 97无码免费人妻超级碰碰碰碰| 亚洲日韩AV一区二区三区四区| 四虎影视免费永久在线观看 | 97在线免费观看视频| 亚洲欧洲国产日韩精品| 免费看片在线观看| 亚洲AV无码专区国产乱码不卡| 亚洲国产成人久久一区久久| 免费观看91视频| 亚洲乱码在线观看| 亚洲高清偷拍一区二区三区| 中文字幕日本人妻久久久免费| 亚洲一卡2卡3卡4卡国产网站 | 亚洲精品综合久久| 午夜影院免费观看| 亚洲风情亚Aⅴ在线发布| 亚洲色无码专区在线观看| 嘿嘿嘿视频免费网站在线观看| 蜜臀亚洲AV无码精品国产午夜.| 亚洲国产日韩在线视频|