sphinx 在linux 系统的安装

孤街浪徒 提交于 2020-04-04 06:38:31

1.到官网下载源代码安装,你也可以根据系统直接下载rpm包安装

本人系统:

[root@web-01 ~]# cat /proc/version
Linux version 2.6.32-573.22.1.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Wed Mar 23 03:35:39 UTC 2016

我们这里选择源码安装

下载完之后放到目录/home下 版本是sphinx-2.2.10

 

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

如果报错:
/usr/local/sphinx/bin/indexer:error while loading shared libraries:libmysqlclient.so.18 cannot opent shared object file: No such file or directory
这主要是因为你安装库后,没有配置相应的环境变量.可以通过连接修正这个问题
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
对于64bit Linux,命令为:
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

然后再重试.

 

使用sphinx php api测试:


  1. <?php  
  2.   
  3. require ( "sphinxapi.php" );   //此文件在sphinx源代码的api文件夹中可以找到  
  4.   
  5.   
  6.   
  7. $cl = new SphinxClient ();  
  8.   
  9. $q = "weibo";  
  10. $sql = "test1";  
  11. $mode = SPH_MATCH_ALL;  
  12. $host = "localhost";  
  13. $port = 9312;  
  14. $index = "*";  
  15. // $groupby = "";  
  16. // $groupsort = "@group desc";  
  17. // $filter = "group_id";  
  18. // $filtervals = array();  
  19. // $distinct = "";  
  20. // $sortby = "";  
  21. // $sortexpr = "";  
  22. // $limit = 20;  
  23. // $ranker = SPH_RANK_PROXIMITY_BM25;  
  24. // $select = "";  
  25.   
  26. $cl->SetServer ( $host, $port );  
  27. $cl->SetConnectTimeout ( 1 );  
  28. $cl->SetArrayResult ( true );  
  29. $cl->SetMatchMode ( $mode );  
  30. // if ( count($filtervals) )    $cl->SetFilter ( $filter, $filtervals );  
  31. // if ( $groupby )              $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );  
  32. // if ( $sortby )               $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );  
  33. // if ( $sortexpr )         $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );  
  34. // if ( $distinct )         $cl->SetGroupDistinct ( $distinct );  
  35. // if ( $select )               $cl->SetSelect ( $select );  
  36. // if ( $limit )                $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );  
  37. //$cl->SetRankingMode ( $ranker );  
  38. $res = $cl->Query ( $q, $index );  
  39.   
  40. ////////////////  
  41. // print me out  
  42. ////////////////  
  43.   
  44. if ( $res===false )  
  45. {  
  46.     print "Query failed: " . $cl->GetLastError() . ".<br/>";  
  47.   
  48. else  
  49. {  
  50.     if ( $cl->GetLastWarning() )  
  51.         print "WARNING: " . $cl->GetLastWarning() . "<br/><br/>";  
  52.   
  53.     print "Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.<br/>";  
  54.     print "Query stats:<br/>";  
  55.     if ( is_array($res["words"]) )  
  56.         foreach ( $res["words"] as $word => $info )  
  57.             print "    '$word' found $info[hits] times in $info[docs] documents\n";  
  58.     print "<br/>";  
  59.   
  60.     if ( is_array($res["matches"]) )  
  61.     {  
  62.         $n = 1;  
  63.         print "Matches:<br/>";  
  64.         foreach ( $res["matches"] as $docinfo )  
  65.         {  
  66.             print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]";  
  67.             foreach ( $res["attrs"] as $attrname => $attrtype )  
  68.             {  
  69.                 $value = $docinfo["attrs"][$attrname];  
  70.                 if ( $attrtype==SPH_ATTR_MULTI || $attrtype==SPH_ATTR_MULTI64 )  
  71.                 {  
  72.                     $value = "(" . join ( ",", $value ) .")";  
  73.                 } else  
  74.                 {  
  75.                     if ( $attrtype==SPH_ATTR_TIMESTAMP )  
  76.                         $value = date ( "Y-m-d H:i:s", $value );  
  77.                 }  
  78.                 print ", $attrname=$value";  
  79.             }  
  80.             print "<br/>";  
  81.             $n++;  
  82.         }  
  83.     }  
  84. }  
  85.   
  86. //  
  87. // $Id$  
  88. //  
  89.   
  90. ?>  


 打印的结果:

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


 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!