隨筆-167  評論-65  文章-0  trackbacks-0
          環境: sphinx 0.9.8 rc2 + libmmseg 0.7.3 + thinking_sphinx 1.2.8 + ubuntu 8.1.0 Desktop version

          效果預覽:
          1, http://www.zikii.com/ 這個網站 也是基于 sphinx 實現的
          2,我的



          由于項目需要,需要實現站內搜索,全文檢索和 中文分詞 功能,最后選型了 sphinx + libmmseg + thinking_sphinx , 折騰了好大一會,最后測試成功
          sphinx 是俄國人開發的opensourse 搜索引擎,索引速度很快, libmmseg 是中國人開發的中文分詞程序,thinking_sphinx 是封裝調用sphinx的rails plugin

          下面用到的所有資源 打包下載:
          http://www.uushare.com/user/fl1429/file/2043579

          1,項目的所有的源碼編譯安裝 , 全部移到 /root 下進行,凡是出現權限不夠的, 在命令前加上 sudo

          2,安裝 libmmseg

          ./configure
          make
          make install
          具體可以參考:
          http://www.coreseek.cn/opensource/mmseg/

          3,安裝 ruby 擴展,進入 mmseg ruby 目錄下 (注意最后面有一個點)

          cd ruby
          cp /usr/local/include/mmseg/*.h .
          cp ../src/*.h .   

          cp ../src/css/*.h .
          ruby extconf.lin.rb
          make
          sudo make install
          成功的話,可以到irb下,測試看看是否成功了
          irb 
          require 'mmseg'
          注意:如果在這一步出錯,且出錯提示為:

          css/UnigramCorpusReader.cpp:89: error: ’strncmp’ was not declared in this scope

          則需手工編輯.src/css目錄下UnigramCorpusReader.cpp 文件,在其第一行加上

          #include <string.h>

          然后重新 make,即可通過

          4,創建辭典文件

          進入 mmseg 的 data 目錄下

          mmseg -u unigram.txt

          把生成的文件名改為 uni.lib

          5,安裝sphinx (把兩個補丁放入 解壓后 的 sphinx下)

          cd sphinx-0.9.8-rc2

          patch -p1 < sphinx-0.98rc2.zhcn-support.patch #patch中文補丁

          patch -p1 < fix-crash-in-excerpts.patch # patch 防crash 補丁

          Copy mmseg安裝文件夾下 src/csr_typedefs.h 和

          src/css/SegmenterManager.h 到Sphinx安裝文件夾下的 src

          文件夾下

          ./configure

          make
          sudo make install

          注意:如果在這一步出現

          /usr/local/include/mmseg/freelist.h:22: error: ‘strlen’ was not declared in this scope

          的錯誤,手工修改 /usr/local/mmseg/include/mmseg/freelist.h

          在上面添加
          #include <string.h>

          以上的配置 sphinx 就支持 中文分詞 搜索了。。。

          Demo:

          1,依照 Ryan 的例子 配置 好基本的英文搜索

          http://railscasts.com/episodes/120-thinking-sphinx

          2,重點是 model 里的 定義索引

          define_index do

          indexes content
          indexes :name, :sortable => true
          indexes comments.content, :as => :comment_content
          indexes [author.first_name, author.last_name], :as => :author_name
          has author_id, created_at
          end

          3,把上面生成的 uni.lib 辭典文件 拷入 項目的 lib 下 (或者別的地方,看你配置里的charset_dictpath的,不編譯的話,可以用別人現成的uni.lib)

          4,在項目的 config 下新建一個 sphinx.yml 文件
          development: &my_settings
          enable_star: 1
          min_prefix_len: 0
          min_infix_len: 2
          min_word_len: 1
          max_results: 70000
          morphology: none
          listen: localhost:3312
          charset_dictpath: /home/feng/RailsProject/Search/lib
          test:
          <<: *my_settings
          production:
          <<: *my_settings

          上面的
          /home/feng/RailsProject/Search/lib 可以寫成 "#{RAILS_ROOT}/lib"
          5,生成配置文件

          rake ts:config

           這樣config 下會生成一個 development.sphinx.conf 文件

          找到 charset_type = utf-8 把后面 utf-8 修改為 zh_cn.utf-8,并在下面添加如下語句

          charset_dictpath = /home/feng/RailsProject/Search/lib (你工程的lib路徑)

          6,建立索引

          rake ts:index INDEX_ONLY=true
          注意后面必須添加 INDEX_ONLY=true不然的話,配置文件會被重置,還有不要寫成 INDEX_ONLY = true


          7,啟動sphinx服務

          rake ts:start
          8,啟動webrick

          注意,sphinx 必須在webrick的前面 先啟動
          我的demo項目,其實就是上面ryan 的,下載后可以看到我的具體配置,或者詳細的內容

          http://www.uushare.com/user/fl1429/file/2043752


          ref:
          http://wiki.github.com/cogentsoft/zbs/180787
          http://www.javaeye.com/topic/431217?page=1
          http://www.javaeye.com/topic/196451
          http://blog.sina.com.cn/s/blog_412d58ec010090pq.html
          http://freelancing-god.github.com/ts/en/rake_tasks.html


          write by feng
          posted on 2009-09-22 17:15 fl1429 閱讀(1739) 評論(0)  編輯  收藏 所屬分類: Rails
          已訪問數:
          free counters
          主站蜘蛛池模板: 大埔区| 讷河市| 靖西县| 叶城县| 江达县| 定远县| 容城县| 三台县| 突泉县| 凤山县| 资阳市| 儋州市| 重庆市| 留坝县| 岚皋县| 甘德县| 建水县| 榕江县| 宜城市| 溆浦县| 桂阳县| 穆棱市| 定边县| 榕江县| 海伦市| 柞水县| 盐亭县| 霞浦县| 建水县| 章丘市| 甘谷县| 金堂县| 连江县| 宽城| 宜黄县| 南木林县| 乡宁县| 乳山市| 三台县| 康保县| 娄烦县|