sphinx在linux系統的安裝

本人機器系統:

[root@xxx ~]# cat /proc/version 
Linux version 2.6.32-431.23.3.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Thu Jul 31 17:20:51 UTC 2014


1.到官網下載源代碼安裝,你也可以根據系統直接下載rpm包安裝



我們這裏選擇源碼安裝


下載完之後,放到目錄/home 下

2.解壓>>:tar -xzvf sphinx-2.2.10-release.tar.gz 

 >>:mv sphinx-2.2.10-release sphinx

>>:cd sphinx

>>:./configure --prefix=/usr/local/sphinx

>>:make

 make install


cd /usr/local/sphinx/etc/

mv sphinx-min.conf.dist sphinx.conf

vim sphinx.conf

下面是裏面的內容配置


#
# Minimal Sphinx configuration sample (clean, simple, functional)
#


source src1
{
type = mysql


sql_host = 127.0.0.1    
sql_user = root
sql_pass =
sql_db = test  //數據庫name
sql_port = 3306 # optional, default is 3306


sql_query = \
SELECT id, realname,import_id, account, source ,UNIX_TIMESTAMP(import_time) AS import_timestamp \
FROM contact_list_test


sql_attr_uint = id
sql_attr_timestamp= import_timestamp
}




index test1
{
source = src1
path = /usr/local/sphinx/var/data/test1
}




index testrt
{
type = rt
rt_mem_limit = 128M


path = /usr/local/sphinx/var/data/testrt


rt_field = title
rt_field = content
rt_attr_uint = gid
}




indexer
{
mem_limit = 128M
}




searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/sphinx/var/log/searchd.pid
seamless_rotate= 1
preopen_indexes= 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /usr/local/sphinx/var/data
}


生成索引:

cd /usr/local/sphinx/bin

 ./indexer -c /usr/local/sphinx/etc/sphinx.conf --all

如果此時出現以下錯誤,

FATAL: failed to lock /usr/local/sphinx/var/data/test1.spl: Resource temporarily unavailable, will not index. Try --rotate option.

請改用命令

 ./indexer -c /usr/local/sphinx/etc/sphinx.conf --all --rotate


啓動spinx搜索服務器:./searchd -c /usr/local/sphinx/etc/sphinx.conf



使用sphinx php api測試:


<?php

require ( "sphinxapi.php" );   //此文件在sphinx源代碼的api文件夾中可以找到



$cl = new SphinxClient ();

$q = "weibo";
$sql = "test1";
$mode = SPH_MATCH_ALL;
$host = "localhost";
$port = 9312;
$index = "*";
// $groupby = "";
// $groupsort = "@group desc";
// $filter = "group_id";
// $filtervals = array();
// $distinct = "";
// $sortby = "";
// $sortexpr = "";
// $limit = 20;
// $ranker = SPH_RANK_PROXIMITY_BM25;
// $select = "";

$cl->SetServer ( $host, $port );
$cl->SetConnectTimeout ( 1 );
$cl->SetArrayResult ( true );
$cl->SetMatchMode ( $mode );
// if ( count($filtervals) )	$cl->SetFilter ( $filter, $filtervals );
// if ( $groupby )				$cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
// if ( $sortby )				$cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
// if ( $sortexpr )			$cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
// if ( $distinct )			$cl->SetGroupDistinct ( $distinct );
// if ( $select )				$cl->SetSelect ( $select );
// if ( $limit )				$cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
//$cl->SetRankingMode ( $ranker );
$res = $cl->Query ( $q, $index );

////////////////
// print me out
////////////////

if ( $res===false )
{
	print "Query failed: " . $cl->GetLastError() . ".<br/>";

} else
{
	if ( $cl->GetLastWarning() )
		print "WARNING: " . $cl->GetLastWarning() . "<br/><br/>";

	print "Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.<br/>";
	print "Query stats:<br/>";
	if ( is_array($res["words"]) )
		foreach ( $res["words"] as $word => $info )
			print "    '$word' found $info[hits] times in $info[docs] documents\n";
	print "<br/>";

	if ( is_array($res["matches"]) )
	{
		$n = 1;
		print "Matches:<br/>";
		foreach ( $res["matches"] as $docinfo )
		{
			print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]";
			foreach ( $res["attrs"] as $attrname => $attrtype )
			{
				$value = $docinfo["attrs"][$attrname];
				if ( $attrtype==SPH_ATTR_MULTI || $attrtype==SPH_ATTR_MULTI64 )
				{
					$value = "(" . join ( ",", $value ) .")";
				} else
				{
					if ( $attrtype==SPH_ATTR_TIMESTAMP )
						$value = date ( "Y-m-d H:i:s", $value );
				}
				print ", $attrname=$value";
			}
			print "<br/>";
			$n++;
		}
	}
}

//
// $Id$
//

?>


 打印的結果:

Query 'weibo' retrieved 1000 of 3873 matches in 0.001 sec.
Query stats:
'weibo' found 3873 times in 3873 documents 
Matches:
1. doc_id=1, weight=1, import_timestamp=2015-06-26 16:25:41
2. doc_id=4, weight=1, import_timestamp=2015-06-26 16:25:41
3. doc_id=5, weight=1, import_timestamp=2015-06-26 16:25:41
4. doc_id=6, weight=1, import_timestamp=2015-06-26 16:25:41
5. doc_id=8, weight=1, import_timestamp=2015-06-26 16:25:41
6. doc_id=9, weight=1, import_timestamp=2015-06-26 16:25:41
7. doc_id=10, weight=1, import_timestamp=2015-06-26 16:25:41
8. doc_id=11, weight=1, import_timestamp=2015-06-26 16:25:41
9. doc_id=12, weight=1, import_timestamp=2015-06-26 16:25:41
10. doc_id=13, weight=1, import_timestamp=2015-06-26 16:25:41
11. doc_id=14, weight=1, import_timestamp=2015-06-26 16:25:41
12. doc_id=15, weight=1, import_timestamp=2015-06-26 16:25:41
13. doc_id=16, weight=1, import_timestamp=2015-06-26 16:25:41
14. doc_id=17, weight=1, import_timestamp=2015-06-26 16:25:41
15. doc_id=18, weight=1, import_timestamp=2015-06-26 16:25:41
16. doc_id=19, weight=1, import_timestamp=2015-06-26 16:25:41
17. doc_id=20, weight=1, import_timestamp=2015-06-26 16:25:41
18. doc_id=21, weight=1, import_timestamp=2015-06-26 16:25:41
19. doc_id=22, weight=1, import_timestamp=2015-06-26 16:25:41
20. doc_id=23, weight=1, import_timestamp=2015-06-26 16:25:41






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章