sphinx 安裝測試 (通過)

Sphinx是一個基於SQL的全文檢索引擎,但對中文用戶來說一個致命的缺陷是不支持中文。後來在網上發現了一個基於 Sphinx 的支持切詞的全文搜索引擎 sphinx-for-chinese。下載下來安裝使用後發現很好用,下面介紹一下具體的安裝過程。

1. 下載所需的安裝包
sphinx-for-chinese-0.9.9-r2117.tar.gz
xdict_1.1.tar.gz
下載地址:http://code.google.com/p/sphinx-for-chinese/downloads/list
2. 安裝 sphinx-for-chinese

      $ tar zxvf sphinx-for-chinese-0.9.9-r2117.tar.gz
      $ cd sphinx-for-chinese-0.9.9-r2117
      $ ./configure --prefix=/usr/local/sphinx
      $ make
      $ sudo make install

3. 創建test數據庫,並創建sphinx用戶

      mysql> create database test;
      mysql>create user 'test'@'localhost' identified by 'test';
      mysql>grant all privileges on test.* to 'test'@'localhost';

4. 指定sphinx配置文件

      $ cd /usr/local/sphinx/etc
      $ sudo cp sphinx.conf.dist sphinx.conf

5. 編輯配置文件

      sql_host        = localhost
      sql_user        = test
      sql_pass        = test
      sql_db          = test
      sql_port        = 3306  # optional, default is 3306

      說明:加粗部分是修改的內容

到這裏爲止,sphinx已經可以使用了,但還不能支持中文切詞,以下是加入中文切詞的步驟

   1. 解壓字典文件 xdict_1.1.tar.gz

      $ tar zxvf xdict_1.1.tar.gz

   2. 藉助先前安裝的 mkdict 工具生成字典

      $ /usr/local/sphinx/bin/mkdict xdict.txt xdict

   3. 將字典 xdict 拷貝到 /usr/local/sphinx/etc目錄下
   4. 配置中文切詞
      打開 sphinx.conf文件,找到 ‘charset_type    = sbcs’ 字樣,將其改爲

      charset_type    = utf-8
      chinese_dictionary = /usr/local/sphinx/etc/xdict

至此中文切詞配置完成,下面做一個簡單的測試

   1. 編輯sphinx-for-chinese自帶的SQL腳本,加入中文數據
      $ vi /usr/local/sphinx/etc/example.sql

      REPLACE INTO test.documents ( id, group_id, group_id2, date_added, title, content ) VALUES
        ( 1, 1, 5, NOW(), 'test one', 'this is my test document number one. also checking search within phrases.' ),
        ( 2, 1, 6, NOW(), 'test two', 'this is my test document number two' ),
        ( 3, 2, 7, NOW(), 'another doc', 'this is another group' ),
        ( 4, 2, 8, NOW(), 'doc number four', 'this is to test groups' ),
        ( 5, 2, 8, NOW(), 'doc number five', '一個' ),
        ( 6, 2, 8, NOW(), 'doc number six', '我' ),
        ( 7, 2, 8, NOW(), 'doc number seven', '中國人' );

      說明:加粗部分是添加的中文測試數據
   2. 導入數據

      $ mysql -usphinx -psphinx < example.sql

   3. 建立索引

      $ sudo /usr/local/sphinx/bin/indexer --all

   4. 檢索

      $ /usr/local/sphinx/bin/search 我是一箇中國人
      Sphinx 0.9.9-release (r2117)
      Copyright (c) 2001-2009, Andrew Aksyonoff

      using config file '/usr/local/sphinx/etc/sphinx.conf'...
      index 'test1': query '我是一箇中國人 ': returned 0 matches of 0 total in
      0.000 sec

      words:
      1. '我': 1 documents, 1 hits
      2. '是': 0 documents, 0 hits
      3. '一個': 1 documents, 1 hits
      4. '中國人': 1 documents, 1 hits

      index 'test1stemmed': query '我是一箇中國人 ': returned 0 matches of 0
      total in 0.000 sec

      words:
      1. '我': 1 documents, 1 hits
      2. '是': 0 documents, 0 hits
      3. '一個': 1 documents, 1 hits
      4. '中國人': 1 documents, 1 hits

至此,sphinx-for-chinese已經成功安裝並順利通過測試

發佈了52 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章