Mac下使用Homebrew安裝Sphinx和MySQL

安裝

假設你的系統上已經安裝好餓 Homebrew,執行如下命令:php

➜  ~ brew install mysql

啓動MySQL:html

➜  ~ mysql.server start

而後關閉MySQL:mysql

➜  ~ mysql.server stop

安裝Sphinx並將其支持MySQL:sql

➜  ~ brew install sphinx --with-mysql
# Sphinx 默認安裝在 /usr/local/Celler/sphinx/[版本號]/

安裝PHP的Sphinx擴展數據庫

➜  ~ brew install homebrew/php/php56-sphinx
# PHP-Sphinx 擴展默認安裝在 /usr/local/Cellar/php56-sphinx/[版本號]/

檢查 Sphinx 及擴展安裝是否成功

第一步:配置 Sphinx 與數據庫鏈接app

配置文件:/usr/local/Celler/sphinx/sphinx.conf測試

# 若是配置文件不存在,複製 sphinx.conf.dist 至 sphinx.conf
# 下面是配置:
source src1
{
    type                    = mysql             // 數據庫類型
    sql_host                = localhost         // 所鏈接的 ip
    sql_user                = user              // 數據庫用戶名
    sql_pass                = pass              // 數據庫密碼
    sql_db                  = test              // 數據庫名稱
    sql_port                = 3306              // 數據庫端口
....

默認狀況下只須要修改數據庫用戶名和密碼就能夠了。ui

第二步:在數據庫中新建一個須要被 Sphinx 索引的測試數據庫this

➜  ~ mysql -u root -p             // 登陸數據庫
mysql> create database test;        // 建立名爲 test 的數據庫
mysql> exit;                        // 退出mysql
// 導入測試數據
mysql -u [數據庫用戶名] -p [數據庫密碼] < /usr/local/Cellar/sphinx/[版本號]/etcexample.sql

若是沒有出現什麼錯誤就說明數據庫已經建立成功了。接下來創建索引。code

第三步:使用 Indexer 創建索引

➜  ~ /usr/local/Cellar/sphinx/[版本號]/bin/indexer --all

輸出以下信息(版本號可能會有出入):

Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/usr/local/Cellar/sphinx/2.2.10/etc/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.160 sec, 1198 bytes/sec, 24.84 docs/sec
indexing index 'test1stemmed'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.005 sec, 32339 bytes/sec, 670.24 docs/sec
skipping non-plain index 'dist1'...
skipping non-plain index 'rt'...
total 8 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 24 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
rotating indices: successfully sent SIGHUP to searchd (pid=1342).

第四步:啓動 searchd

➜  ~ searchd

輸入以下信息:

Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/usr/local/Cellar/sphinx/2.2.10/etc/sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
precaching index 'test1stemmed'                             
precaching index 'rt'                                       
precached 3 indexes in 0.003 sec

出現上面這些信息,說明啓動成功!

第五步:使用 PHP 檢測 Sphinx 及擴展是否安裝成功

<?php
header('Content-type: text/html; charset=utf-8');

// 檢測 PHP-Spinx 模塊是否安裝成功
if (!in_array('sphinx', get_loaded_extensions())) {
    die('模塊不存在,請檢查!');
}

$docs = array
(
    "this is my test text to be highlighted, and for the sake of the testing we need to pump its length somewhat",
    "another test text to be highlighted, below limit",
    "test number three, without phrase match",
    "final test, not only without phrase match, but also above limit and with swapped phrase text test as well",
);
$words = "test text";
$index = "test1";
$opts = array
(
    "before_match"      => "<b>",
    "after_match"       => "</b>",
    "chunk_separator"   => " ... ",
    "limit"             => 60,
    "around"            => 3,
);

foreach ( array(0,1) as $exact )
{
    $opts["exact_phrase"] = $exact;
    print "exact_phrase=$exact\n";

    $cl = new SphinxClient ();
    $res = $cl->BuildExcerpts ( $docs, $index, $words, $opts );
    if ( !$res )
    {
        die ( "ERROR: " . $cl->GetLastError() . ".\n" );
    } else
    {
        $n = 0;
        foreach ( $res as $entry )
        {
            $n++;
            print "n=$n, res=$entry\n";
        }
        print "\n";
    }
}

以上源碼輸出:

exact_phrase=0
n=1, res=this is my <b>test</b> <b>text</b> to be highlighted,  ... 
n=2, res=another <b>test</b> <b>text</b> to be highlighted, below limit
n=3, res=<b>test</b> number three, without phrase match
n=4, res=final <b>test</b>, not only  ... with swapped phrase <b>text</b> <b>test</b> as well

exact_phrase=1
n=1, res=this is my <b>test text</b> to be highlighted,  ... 
n=2, res=another <b>test text</b> to be highlighted, below limit
n=3, res=test number three, without phrase match
n=4, res=final test, not only without phrase match, but also above  ...

搞定!

相關文章
相關標籤/搜索