服务器之家:专注于服务器技术及软件下载分享
分类导航

服务器资讯|IT/互联网|云计算|区块链|软件资讯|操作系统|手机数码|百科知识|免费资源|头条新闻|

服务器之家 - 新闻资讯 - 百科知识 - coreseek是什么?coreseek入门教程详解

coreseek是什么?coreseek入门教程详解

2020-04-29 17:33月夜星星雨 百科知识

Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索、论坛/站内搜索、数据库搜索、文档/文献检索、信息检索、数据挖掘等应用场景,

coreseek是什么?Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索、论坛/站内搜索、数据库搜索、文档/文献检索、信息检索、数据挖掘等应用场景,用户可以免费下载使用。

coreseek安装需要预装的软件:

yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-deve

  1. cd /usr/local/src 
  2.  
  3. wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz 
  4.  
  5. tar -xzvf coreseek-3.2.14.tar.gz 
  6.  
  7. cd coreseek-3.2.14 
  8.  
  9.  
  10. ##安装mmseg 
  11.  
  12. cd mmseg-3.2.14 
  13.  
  14. ./bootstrap #输出的warning信息可以忽略,如果出现error则需要解决 
  15.  
  16. ./configure --prefix=/usr/local/mmseg3 
  17.  
  18. make && make install 
  19.  
  20. cd .. 
  21.  
  22. ## 安装完成后,mmseg使用的词典和配置文件将自动安装到/usr/local/mmseg3/etc中 
  23.  
  24.  
  25. ##安装coreseek 
  26.  
  27. cd csft-3.2.14 
  28.  
  29. sh buildconf.sh #输出的warning信息可以忽略,如果出现error则需要解决 
  30.  
  31. ./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql ##如果提示mysql问题,可以查看MySQL数据源安装说明 
  32.  
  33. make && make install 
  34.  
  35. cd .. 
  36.  
  37.  
  38. cd /usr/local/coreseek/etc 
  39.  
  40. cp sphinx-min.conf.dist sphinx.conf 
  41.  
  42. vi sphinx.conf 
  43.  
  44. 内容示例如下(localhost,DB_USER,DB_PASSWORD,DB_NAME自行修改) 
  45.  
  46. # 
  47.  
  48. # Minimal Sphinx configuration sample (clean, simple, functional) 
  49.  
  50. # 
  51.  
  52.  
  53. source content 
  54.  
  55.  
  56. type = mysql 
  57.  
  58.  
  59. sql_host = localhost 
  60.  
  61. sql_user = DB_USER 
  62.  
  63. sql_pass = DB_PASSWORD 
  64.  
  65. sql_db = DB_NAME 
  66.  
  67. sql_port = 3306 # optional, default is 3306 
  68.  
  69. sql_query_pre = SET NAMES utf8 
  70.  
  71.  
  72. sql_query = \ 
  73.  
  74. SELECT id, title, pub_time, group_id, content FROM contents where status = '1' 
  75.  
  76.  
  77. sql_attr_uint = group_id 
  78.  
  79. sql_attr_timestamp = pub_time 
  80.  
  81.  
  82. sql_query_info = SELECT * FROM contents WHERE id=$id 
  83.  
  84.  
  85. index content 
  86.  
  87.  
  88. source = content 
  89.  
  90. path = /usr/local/coreseek/var/data/content 
  91.  
  92. docinfo = extern 
  93.  
  94. charset_dictpath = /usr/local/mmseg3/etc/ 
  95.  
  96. charset_type = zh_cn.utf-8 
  97.  
  98. ngram_len = 0 
  99.  
  100.  
  101. indexer 
  102.  
  103.  
  104. mem_limit = 32M 
  105.  
  106.  
  107.  
  108.  
  109. searchd 
  110.  
  111.  
  112. port = 9312 
  113.  
  114. log = /usr/local/coreseek/var/log/searchd.log 
  115.  
  116. query_log = /usr/local/coreseek/var/log/query.log 
  117.  
  118. read_timeout = 5 
  119.  
  120. max_children = 30 
  121.  
  122. pid_file = /usr/local/coreseek/var/log/searchd.pid 
  123.  
  124. max_matches = 1000 
  125.  
  126. seamless_rotate = 1 
  127.  
  128. preopen_indexes = 1 
  129.  
  130. unlink_old = 1 
  131.  

然后根据以上配置建立索引文件

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

启动命令 /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf

然后在coreseek目录下,新建3个sh脚本,以便操作 停止服务stop.sh

  1. #!/bin/bash 
  2. /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf --stop 

建立索引build.sh

  1. #!/bin/bash 
  2. /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate 

启动服务start.sh

  1. #!/bin/bash 
  2. /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf 

添加可执行权限

  1. chmod +x start.sh 
  2. chmod +x stop.sh 
  3. chmod +x build.sh 

运行start.sh后,使用crontab定时执行build.sh,就可更新索引。(注:因为数据量小且更新不算很频繁,未使用增量索引,只是定时重建主索引,新版本CoreSeek全文搜索 4.1 支持实时索引)

  1. crontab -e 
  2. 0 2 * * * sh /usr/local/coreseek/build.sh >/dev/null 2>&1 

每天凌晨2点重建一次索引,忽略日志输出。

在/usr/local/src/coreseek.3.2.14/csft-3.2.14/api目录下提供了PHP的接口文件 sphinxapi.php,这个文件包含一个SphinxClient的类,copy到自己的web目录下 通过如下方式进行搜索

  1. $s_key = trim($s_key); 
  2. if(strpos($s_key,'\'') || strpos($s_key,'\"') || strpos($s_key,'\;')) { 
  3. exit('非法字符'); 
  4. require("sphinxapi.php"); 
  5. $page_nums = 20; 
  6. $offset_start = ($page_index-1)*$page_nums; 
  7. $offset_end = $offset_start + $page_nums; 
  8. $cl = new SphinxClient(); 
  9. $cl->SetServer('localhost', 9312); 
  10. $cl->SetArrayResult(true); 
  11. $cl->SetMatchMode(SPH_MATCH_ALL); 
  12. $cl->SetLimits($offset_start,$offset_end); 
  13. $cl->SetSortMode(SPH_SORT_RELEVANCE); 
  14. $res = $cl->Query($s_key,"content"); 

安装包括两个部分,mmseg和csft

安装成功会在/usr/local文件夹下面出现coreseek文件夹

source bt {  sql_pass                = ****  #如果密码里面有#号需要使用转意字符,否则连接不了数据库    sql_query_pre  = SET NAMES utf8 #要根据你自己数据库的编码改变,比如如果编码是utf8mb4而编码写的是utf8 会出现没有搜索结果的问题   }   index bt {     source                    = bt  #这个地方的值要和前面配置的source名对应 }

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf --stop  停止服务 /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf --all --rotate  建立索引

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf   开启服务

默认配置文件是csft.conf 如果配置文件是其他名字的话,需要-c 来制定配置文件路径 ---------------

配置文件中

sql_query                =   xxxx

xxxx代表一个sql语句,sql语句select的第一个字段将被sphinx认作表的主键来进行索引,所以数据表的主键字段不是int类型也没有关系,选一个是int类型的字段排在select语句的第一个就行了,但是这个字段要保证唯一性,否则会导致搜索结果不完整,计算出来的值也可以被当做主键来进行索引 比如SELECT unix_timestamp(time),name, age .......unix_timestamp(time)是计算出来的,它排在第一个的时候,就会被sphinx当做表的主键来进行索引。 --------------------- 

延伸 · 阅读

精彩推荐