Sphinx在windows下安裝使用

字號+ 編輯: 国内TP粉 修訂: 种花家 來源: ThinkPHP 2023-09-22 我要說兩句(0)

前一陣子嘗試使用了一下 Sphinx ,一個能夠被各種語言 (PHP/Python/Ruby/etc) 方便調用的全文檢索系統。網上的資料大多是在 linux 環境下的安裝使用,當然,作爲生産環境很有必要部署在 *nix 環境下,作爲學習測試,還是 windows 環境比較方便些。本文旨在提供一種便捷的方式讓 Sphinx 在 windows 下安裝配置以支持中文全文檢索,配置部分在 linux 下通用。

Sphinx的介紹

Sphinx是一個以GPLv2許可發布的全文搜索引擎,如果需要在商業項目中使用(例如嵌入到其他程序中),則需要聯繫Sphinx Technologies Inc.獲取商業授權。

一般來說,Sphinx是一個獨立的搜索引擎,旨在爲其他應用程序提供高速、低空間佔用和高相關性的全文搜索功能。Sphinx可以與SQL數據庫和腳本語言輕松集成。

目前,Sphinx内置了對MySQL和PostgreSQL數據庫的支持,並且還支持從標準輸入讀取特定格式的XML數據。通過修改源代碼,用戶可以自行添加新的數據源,例如對其他類型的數據庫管理系統的原生支持。

Sphinx的搜索API支持PHP、Python、Perl、Ruby和Java,並且還可以用作MySQL存儲引擎。搜索API非常簡單,可以在幾個小時内移植到新的編程語言上使用。

Sphinx 特性:

  1. 高速的建立索引 ( 在當代 CPU 上,峰值性能可達到 10MB/ 秒 );

  2. 高性能的搜索 ( 在 2–4GB 的文本數據上,平均每次檢索響應時間小於 0.1 秒 ); 可處理海量數據 ( 目前已知可以處理超過 100GB 的文本數據 , 在單一 CPU 的系統上可處理 100M 文档 );

  3. 提供了優秀的相關度算法,基於短語相似度和統計( BM25 )的複合 Ranking 方法 ;

  4. 支持分布式搜索 ;

  5. 提供文档的摘錄生成 ;

  6. 可作爲 MySQL 的存儲引擎提供搜索服務 ;

  7. 支持布爾、短語、詞語相似度等多種檢索模式 ;

  8. 文档支持多個全文檢索字段 ( 最大不超過 32 個 );

  9. 文档支持多個額外的屬性信息 ( 例如:分組信息,時間戳等 );

  10. 停止詞查詢 ;

  11. 支持單一字節編碼和 UTF-8 編碼 ;

  12. 原生的 MySQL 支持 ( 同時支持 MyISAM 和 InnoDB);

  13. 原生的 PostgreSQL 支持 .

Sphinx 在 windows 上的安裝

1. 下載sphinx包

直接在 http://www.sphinxsearch.com/downloads.html 找 到最新的 windows 版本,我這裡下的是<a href="http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1-win32.zip" target="_blank"> Win32 release binaries with MySQL support</a>,下載後解壓在 D:/sphinx 目錄下;

2. 建立索引文档

在 D:/sphinx/ 下新建一個 data 目錄用來存放索引文档,一個 log 目錄放日志文档;

3. 修改配置

複制 D:/sphinx/sphinx.conf.in 到 D:/sphinx/bin/sphinx.conf (注意修改文档名)

修改 D:/sphinx/bin/sphinx.conf ,我這裡 列出需要修改的幾個:

type           = mysql # 數據源,我這裡是mysql
sql_host       = localhost # 數據庫服務器
sql_user       = root # 數據庫用戶名
sql_pass       = '' # 數據庫密碼
sql_db         = test # 數據庫
sql_port       = 3306 # 數據庫耑口
sql_query_pre      = SET NAMES utf8 # 去掉此行前面的注釋,如果你的數據庫是uft8 編碼的
index test1
{
# 放索引的目錄

  path      = D:/sphinx/data/
# 編碼

  charset_type     = utf-8

  #  指定utf-8 的編碼表

  charset_table     = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F

  # 簡單分詞,只支持0 和1 ,如果要搜索中文,請指定爲1

  ngram_len       = 1
# 需要分詞的字符,如果要搜索中文,去掉前面的注釋

  ngram_chars      = U+3000..U+2FA1F
}
# 搜索服務需要修改的部分
searchd
{

  # 日志

  log        = D:/sphinx/log/searchd.log
# PID file, searchd process ID file name

  pid_file      = D:/sphinx/log/searchd.pid
# windows 下啓動searchd 服務一定要注釋掉這個

  # seamless_rotate     = 1
}

4. 導入測試數據

將sql文档導入數據庫(sql 文档在 D:/sphinx/example.sql)

C:/Program Files/MySQL/MySQL Server 5.0/bin>mysql -uroot blog<d:/sphinx/example.sql

說明:

(1)由於我的數據庫名稱叫blog,因此這裡我寫的是blog,blog數據庫編碼設置爲utf8

(2)example.sql中默認的數據庫名爲test,在導入前修改爲統一替換爲blog即可,若本身就是在test數據庫下測試,就不用修改了

5. 建立索引

打開cmd窗口,進入目錄D:\sphinx\bin

D:/sphinx/bin>indexer.exe test1 ( 備注 :test1 爲 sphinx.conf 的 index test1() )
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec

6. 搜索“test”試試

D:/sphinx/bin>search.exe test

顯示結果如下

using config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groups
words:
1. ‘test’: 3 documents, 5 hits

6. 測試中文搜索

修改 blog數據庫中 documents 數據表,

UPDATE `blog`.`documents` SET `title` = ‘ 測試中文 ’, `content` = ‘this is my test document number two ,應該搜的到吧 ’ WHERE `documents`.`id` = 2;

重建索引:

D:/sphinx/bin>indexer.exe test1

搜索關鍵字: 中文 試試:

D:/sphinx/bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…index ‘test1′: query ‘ 中文 ‘: returned 0 matches of 0 total in 0.000 sec
words:
D:/sphinx/bin>

貌似沒有搜到,這是因爲 windows 命令行中的編碼是 gbk ,當然搜不出來。我們可以用程序試試,在 D:/sphinx/api 下新建一個 foo.php 的文档,注意 utf-8 編碼

<?php
require ’sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(’localhost’,9312);
$result = $s->Query(’ 中文 ’);
var_dump($result);

啓動 Sphinx searchd 服務

D:/sphinx/bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
WARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:9312
accepting connections

執行 PHP 查詢:

訪問 http://www.test.com/sphinx/api/foo.php ( 自己配置的虛擬主機 )

閲完此文,您的感想如何?
  • 有用

    0

  • 沒用

    0

  • 開心

    0

  • 憤怒

    0

  • 可憐

    0

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: 2702237 13835667

相關課文
  • Windows系統的桌面快捷方式刪不掉的解決方法

  • Windows系統打不開exe文档了,如何解決?

  • Windows台式機和筆記本電腦連接到系統中的設備沒有發揮作用

  • 取消搜狗彈出的“搜狐微門戶”廣告網頁窗口的實現方法(殺死SohuNews.exe進程)

我要說說
網上賓友點評