linux配置sphinx


 

1. 配置索引

cd /usr/local/sphinx/etc/

cp sphinx.conf.dist sphinx.conf  //備份配置文件,防止改錯

vim sphinx.conf

 

配置文件結構:

# 主數據源,(main名字可更改)
source main{
  type     = mysql    #數據庫類型
  sql_host  = localhost  #MySQL主機IP
  sql_user  = test     #MySQL用戶名
  sql_pass  =        #MySQL密碼
  sql_db   = test     #MySQL數據庫
  sql_port  = 3306     #MySQL端口
  sql_sock  = /tmp/mysql.sock    #Linux下需要開啟,指定sock文件
  sql_query_pre = SET NAMES utf8   #MySQL檢索編碼
  sql_query_pre = SET SESSION query_cache_type=OFF #關閉緩存
  sql_query = \             #獲取數據的SQL語句
    SELECT id, title, content FROM post

  # 以下是用來過濾或條件查詢的屬性,這里列出的字段將可以進行條件查詢,同時不參與全文檢索
  #sql_attr_uint = group_id
  #sql_attr_timestamp = date_added
  
}

# 增量數據源(inherited source), 繼承主數據源
source src1throttled : main{

}

# 主索引(local index),(main名字可更改)
index main{
  source  = main  # 指定主數據源
  path   = /usr/local/sphinx/var/data/main  # 索引路徑
}

# 增量索引(inherited index)
index test1stemmed : test1{

}

# 分布式索引(distributed index)
index dist1{

}

# 實時索引(realtime index)
index rt{

}

# 索引器設置,(調整最小內存到最佳)
indexer{
  mem_limit = 256M  #內存大小限制,默認128M,推薦256M
              #其它用默認即可
}

# 服務進程設置,(監聽端口號)
searched{
  #全部默認即可,默認端口號就是9312
}

# 公共配置
common{

}

  

2. 創建索引

創建索引命令:indexer

-c     指定配置文件

--all    對所有索引重新編制索引

--rotate  用於輪換索引,在不停止服務的時候(searchd運行時)增加索引;searchd運行時不加會報錯。

--merge  合並索引,增量索引合並到主索引的時候用

 

生成全部索引: /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --all

或指定索引(例如main): /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf main

 

(1)如果這里出現報錯:

【ERROR: index 'main': sql_connect: Can't connect to local MySQL server through socket '/tmp/mysql.sock'】

沒找到/tmp/mysql.sock, 通過find / -name mysql.sock -print查找到位置,在配置sphinx.conf里更改正確。

如:mysql_sock = /var/lib/mysql/mysql.sock 保存退出。

 

(2)繼續創建索引,警告:

【WARNING: Attribute count is 0: switching to none docinfo】

改sphinx.conf里的docinfo = none就沒有警告了。(http://sphinxsearch.com/docs/current.html#conf-docinfo)

 

創建索引出現如下提示,表示生成成功:

 

3. 啟動Sphinx

重建索引:./searchd -c /usr/local/sphinx/etc/sphinx.conf

輪換索引: ./searchd -c /usr/local/sphinx/etc/sphinx.conf goods_list --rotate

      ./searchd -c /usr/local/sphinx/etc/sphinx.conf store_list --rotate

停止服務:./searchd -c /usr/local/sphinx/etc/sphinx.conf --stop

 

4. 使用sphinx

在web根目錄下建立一個search目錄(當然不在根目錄也行,同樣目錄名也可以隨取),復制E:\coreseek\api\ sphinxapi.php文件到search目錄(sphinxapi.php這個是sphinx官方提供的api),開始php程序的編寫。
在search目錄建立一個文件,名字叫啥都行,我管它叫index.php,其內容如下

<?php
include 'sphinxapi.php';  // 加載Sphinx API
$sc = new SphinxClient(); // 實例化Api
$sc->setServer('localhost', 9312); // 設置服務端,第一個參數sphinx服務器地址,第二個sphinx監聽端口
$res = $sc->query('sphinx', 'mysql'); // 執行查詢,第一個參數查詢的關鍵字,第二個查詢的索引名稱,mysql索引名稱(這個也是在配置文件中定義的),多個索引名稱以,分開,也可以用*表示所有索引。
print_r($res);

  

打印結果

Array
(
   ………省略………
    [matches] => Array
        (
            [2] => Array
                (
                    [weight] => 2
                    [attrs] => Array
                        (
                            [addtime] => 1282622004
                        )
                )
            [4] => Array
                (
                    [weight] => 2
                    [attrs] => Array
                        (
                            [addtime] => 1282622079
                        )
                )
        )
  ………省略………
)

  

Matches中就是查詢的結果了,但是仿佛不是我們想要的數據,比如titile,content字段的內容就沒有查詢出來,根據官方的說明是Sphinx並沒有連接到MySQL去取數據,只是根據它自己的索引內容進行計算,因此如果想用Sphinx提供的API去取得我們想要的數據,還必須以查詢的結果為依據,再次查詢MySQL從而得到我們想要的數據。

查詢結果中鍵值分別表示

2唯一主鍵

 

weight權重

attrs sql_attr_*中配置

至此,搜索引擎算是完成一大半了,剩下的大家可以自行完成。

比如:

<?php

$ids    = array_keys($res['matches']); // 獲取主鍵

$ids = join(',', $ids);

$query  = mysql_query("SELECT * FROM post WHERE id IN ({$ids})");

while($row = mysql_fetch_assoc($query)) {

    .....

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM