星期二, 七月 24, 2007

Search Engine - Apache Lucene 入手指南

Open Source Search Engine 很多, 但是要找到穩定又支援 Unicode, 評價又不錯的, 目前看到的就是 Apache Lucene, 主要是 Java base, 其它語言的版本暫不比較(有些語言的版本是要付費, 而且速度並不比 Java 快).

主要原因是有很多大廠在用, 目前使用 Lucene  的站比較紅的有 Joost, Digg, CNet 等, 參考自: WikiPedia Lucene. (我猜目前最多企業使用的應該是 SQL Like 吧!. Orz..)

本篇先研究將環境建立, 和基本如何 build index, query search 的功能, 其它深入的, 以後有空研究再慢慢寫吧~ :)

Apache Lucene 下載點: Lucene Download

首先建立 java 環境(Lucene 需要 java compile)

  • apt-get install free-java-sdk # 會安裝這麼一堆 (classpath-tools fastjar fontconfig free-java-sdk java-common jikes jikes-sablevm libatk1.0-0 libcairo2 libdatrie0 libffi4 libgtk2.0-0 libgtk2.0-common libpango1.0-0 libpango1.0-common libsablevm-classlib1-java libsablevm-native1 libsablevm1 libthai-data libthai0 libxcursor1 libxfixes3 libxft2 libxi6 libxinerama1 libxrandr2 libxrender1 sablevm)

建立要被 search 的文件暫存檔(build index 要用)

  1. mkdir /tmp/dump
  2. cd /tmp/dump
  3. vim dump.php # 以 lifetype 為例, 拉 category 來用(id -> cateogry name)
    <?php
    $dbh = mysql_connect('localhost', 'root', '');
    mysql_select_db('lifetype', $dbh);

    $sql = 'select id,name from plog_articles_categories;';
    $r = mysql_query($sql, $dbh);
    while($t = mysql_fetch_assoc($r)) {
        $fp = fopen($t['id'], 'w');
        $content = $t['id'] . ',' . $t['name'];
        fwrite($fp, $content);
        fclose($fp);
    }
    ?>
  4. php dump.php # 這樣子就會產生以 id 為檔名的檔案, 內容應該一看就懂.

抓 Lucene Source code

  1. cd $HOME/search
  2. wget http://apache.ntu.edu.tw/lucene/java/lucene-2.2.0.tar.gz
  3. tar zxvf lucene-2.2.0.tar.gz

先設定 java classpath 和 lucene 所需 jar 檔路徑參數

  1. vim ~/.bashrc # 加入下述兩行
  2. LECENE=$HOME/search/lucene-2.2.0
  3. CLASSPATH=$CLASSPATH:$LECENE/lucene-core-2.2.0.jar:$LECENE/lucene-demos-2.2.0.jar; export CLASSPATH
  4. 再 source ~/.bashrc

Compile Lucene source code

  1. cd ~/search/lucene-2.2.0/src/demo/org/apache/lucene/demo # compile 範例程式即可使用
  2. javac *.java

Build index(索引)

  • java org.apache.lucene.demo.IndexFiles /tmp/dump/ # 這樣就會開始 build index
  • 跑完會在 ~/search/lucene-2.2.0/src/demo/org/apache/lucene/demo 產生一個 index 目錄, 存 build 完成的 index file.
  • 如果要再重建索引, 必須要將此目錄砍掉才能再重 build index: rm -fr index

Query Search

  • java org.apache.lucene.demo.SearchFiles # 就可以開始 Query 囉~
  • 如下呈現方式:
    Searching for: note  (輸入 note, 就會將有 note 的檔名都列出來)
    5 total matching documents
    1. /tmp/dump/17
    2. /tmp/dump/15
    3. /tmp/dump/16
    4. /tmp/dump/18
    5. /tmp/dump/19

Query 語法可以參考: Apache Lucene - Query Parser Syntax

大致上這樣子就完成囉, 目前要研究修改成 Web API/中文斷詞 等.

推薦參考連結:

相關標籤

this is comment icon 直接針對 SQL 做索引 [回覆]

呵呵
寫的真是不錯
淺險易懂

只是小弟好奇問一下
您是把資料先從 SQL 撈出來存進檔案再做索引
不能直接針對 SQL 做索引嘛?不知道您有沒有看到相關的東西呢?

Comment by Thoams (08/01/2007 15:51)

this is comment icon 回 Thoams [回覆]

當然有囉, 不過就是要寫 java, 用 jdbc 直接連, 基本上就是拿預設的程式來改, 就可以進去了, 不過這段我還沒測... XD
(因為同事已經改好了, 我就沒動手了.. Orz..)
應該下星期比較有空後才會再測試吧.. 測出來再貼出來囉 :P
PS: 不過我們後來是用 solr, 因為 solr 已經把這個包成 Web API 來用了, 基本上是用 post 方式送進去, 這個也等之後研究完再貼吧 :)

Comment by Tsung (08/01/2007 20:24)

this is comment icon 請大大分享一下solr心得吧... [回覆]

請大大分享一下solr的使用心得吧
最近在考慮要如何做站內搜尋
用solr還是Google Local Search

Comment by heywilly (01/21/2008 11:36)

this is comment icon 回 heywilly [回覆]

嗯, solr 可能要到過年後了耶, 最近比較忙一點.. Orz..
基本上, 您如果是要做站內搜尋, 不曉得您那邊的量是如何, 然後需求是怎麼樣.
如果量不大, 那用 SQL like 的 % 做就可以了. 量大再來考慮 Lucene/solr 這些.
至於 solr 和 google local search, 如果沒有特別 刪除/新增/修改 這些更新時間的考量的話, 我當然是建議直接用 google 做 local search 會比較快, 又省事 :P

Comment by Tsung (01/21/2008 12:17)

this is comment icon 中文化Lucene search engine [回覆]

想問一下我的檔案多是big 5 或 iso-8859-1編碼的。Lucene是否支援 big5 或是要utf-8 先可以?

安裝時有否特別細節要留意? Thank you!

Comment by Manfred (07/11/2008 10:30)

this is comment icon 回 Manfred [回覆]

應該只支援 UTF-8, 您可以於匯入資料前, 先轉換成 UTF-8? :)

Comment by Tsung (07/12/2008 15:05)

this is comment icon 有沒有試過 kinosearch ?? [回覆]

最近看到 kinosearch 這個東西,不過我不知道怎樣讓他可以支援 multi-bye traditional chinese ?? 聽說是有支援。

另外一方面,他號稱是 Plucene (perl 版的 Lucene) 的後繼取代者,所以我猜你可能會比較瞭解吧~

Comment by kftseng (09/08/2008 17:25)

this is comment icon 回 kftseng [回覆]

這個我沒研究耶, 因為 Lucene 和 Solr 已經很夠用囉!
所以我沒去研究其它的耶.. @.@a..

Comment by Tsung (09/08/2008 17:31)
Add this page to del.icio.us Add this page to Yahoo Taiwan's bookmark Add this page to MyShare

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~