搜尋
首頁資料庫mysql教程【原创】用coreseek快速搭建sphinx中文分词搜索引擎

以下内容基于linux 系统。 yum -y install glibc-common libtool autoconf automake mysql-devel expat-devel#如果不安装这个 可能下面 sh buildconf.sh会报错!!!cd /data/srctar -xjf ../software/autoconf-2.64.tar.bz2cd autoconf-2.64/./configuremak

以下内容基于linux 系统。

yum -y install glibc-common libtool autoconf automake mysql-devel expat-devel
#如果不安装这个 可能下面 sh buildconf.sh会报错!!!
cd /data/src
tar -xjf ../software/autoconf-2.64.tar.bz2
cd autoconf-2.64/
./configure
make && make install
cd ../
cd /data/software
wget http://www.coreseek.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz
cd /data/src
tar zxf ../software/coreseek-4.1-beta.tar.gz
cd coreseek-4.1-beta/mmseg-3.2.14
./bootstrap
./configure --prefix=/usr/local/mmseg3
make && make install
cd ../
cd /data/src/coreseek-4.1-beta/csft-4.1/
sh buildconf.sh
./configure --prefix=/usr/local/coreseek  --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --without-mysql
make && make install
cd ../
##测试mmseg分词,coreseek搜索(需要预先设置好字符集为zh_CN.UTF-8,确保正确显示中文)
cd testpack
cat var/test/test.xml    #此时应该正确显示中文
/usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml
/usr/local/coreseek/bin/indexer -c etc/csft.conf --all
/usr/local/coreseek/bin/search -c etc/csft.conf 网络搜索
#创建sphinx创建索引的脚本:
mkdir -p /data/sh/other

vi /data/sh/other/sphinx_update_index.sh

#!/bin/bash
CONFFILE=/usr/local/coreseek/etc/sphinx_index.conf
/bin/sed s#var\/data\/#var\/data2\/#g ${CONFFILE} > ${CONFFILE}.2
mkdir -p /usr/local/coreseek/var/data2
#/usr/local/coreseek/bin/indexer --config ${CONFFILE}.2 --all --rotate
/usr/local/coreseek/bin/indexer --config ${CONFFILE}.2 --all
pkill -9 searchd
sleep 4
/bin/rm -rf /usr/local/coreseek/var/data/
/bin/mv /usr/local/coreseek/var/data2/ /usr/local/coreseek/var/data/
sleep 2
/usr/local/coreseek/bin/searchd --config ${CONFFILE}

chmod 755 /data/sh/other/sphinx_update_index.sh

#配置sphinx索引参数配置

vi /usr/local/coreseek/etc/sphinx_index.conf

################################### PHPCMS ############################################
source cc_phpcms
{
	type = mysql
	sql_host = 172.26.11.75  #此处请改成您的真实配置
	sql_user = phpcms  #此处请改成您的真实配置
	sql_pass = 123456   #此处请改成您的真实配置
	sql_db = phpcms   #此处请改成您的真实配置
	sql_port= 3306  #此处请改成您的真实配置
	sql_query_pre = SET SESSION query_cache_type=OFF
	sql_query_pre = SET character_set_client = 'gbk'
	sql_query_pre = SET character_set_connection ='gbk'
	sql_query_pre = SET character_set_results ='utf8'
	sql_query = SELECT `id`,`catid`,`typeid`,`title`,`status`,`updatetime` from `i_news` #此处请改成您的真实配置
	sql_range_step          = 1000
	sql_attr_timestamp      = updatetime
	sql_attr_uint           = catid
	sql_attr_uint           = typeid
	sql_attr_uint           = status
	sql_query_post  =
	sql_ranged_throttle= 0
}
index cc_phpcms
{
	source   = cc_phpcms
	path   = /dev/shm/cc_phpcms   #放这里比较好,因为这里是linux的内存区!
	docinfo   = extern
	mlock   = 0
	enable_star            = 1
	morphology   = none
	stopwords   =
	min_word_len  = 1
	charset_dictpath = /usr/local/mmseg3/etc/   #注意此处
	charset_type        = zh_cn.utf-8           #注意此处
	html_strip = 1
	html_remove_elements = style, script
	html_index_attrs = img=alt,title; a=title;
}
#################################### SETTING ############################################
indexer
{
	mem_limit   = 300M
}
searchd
{
	# address    = 0.0.0.0
	#listen                  = 3312
	#listen                  = 9312
	#listen                  = 9306:mysql41
	port    = 3312
	log     = /usr/local/coreseek/var/log/searchd.log
	query_log   = /usr/local/coreseek/var/log/query.log
	read_timeout  = 5
	max_children  = 30
	pid_file   = /usr/local/coreseek/var/log/searchd.pid
	max_matches   = 1000
	seamless_rotate  = 1
}

#接下来实现数据源支持:让sphinx支持MySQL数据源

yum -y install mysql-devel libxml2-devel expat-devel
cd /data/src/coreseek-4.1-beta/csft-4.1/
make clean
sh buildconf.sh
 ./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
make && make install
cd ../

##如果出现错误提示:“ERROR: cannot find MySQL include files…….To disable MySQL support, use –without-mysql option.“,可按照如下方法处理:
##请找到头文件mysql.h所在的目录,一般是/usr/local/mysql/include,请替换为实际的
##请找到库文件libmysqlclient.a所在的目录,一般是/usr/local/mysql/lib,请替换为实际的
##configure参数加上:–with-mysql-includes=/usr/local/mysql/include –with-mysql-libs=/usr/local/mysql/lib,执行后,重新编译安装
#跑sphinx服务脚本
/data/sh/other/sphinx_update_index.sh

好了,如果一切正常,将会顺利看到创建索引的信息如下:
112 【原创】用coreseek快速搭建sphinx中文分词搜索引擎

下面写一段php代码进行测试(基于sphinx php 的api方式):

		$page = (int)$_GET['page'];
		$page = ($page==0)?1:$page;
		$perpage = 200;
		$start = ($page -1) * $perpage;
		$keyword = urldecode($_GET['key']);
		require_once (S_ROOT . './api/sphinxapi.php');//请改成您的真实路径
 		$groupby = "";
		$groupsort = "@group desc";
		$filter = "fieldid";
		$filtervals = array ();
		$distinct = "";
		$sortby = "";
		$cl = new SphinxClient();
		$cl->SetServer("localhost", 3312);
		$cl->SetWeights(array (
				100,
				1
		));
		$cl->SetMatchMode(SPH_MATCH_ANY);
		if (count($filtervals)) {
				$cl->SetFilter($filter, $filtervals);
		}
		if ($groupby) {
				$cl->SetGroupBy($groupby, SPH_GROUPBY_ATTR, $groupsort);
		}
		$order = 1;
		if ($order == 0) { //按时间倒序
				$cl->SetSortMode(SPH_SORT_ATTR_DESC, "inputtime");
		}
		elseif ($order == 1) { //按相关度排序
				$cl->SetSortMode(SPH_SORT_RELEVANCE);
		}
		if ($distinct) {
				$cl->SetGroupDistinct($distinct);
		}
		$cl->SetLimits($start, $perpage, ($limit > 1000) ? $limit : 1000);
		$cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
		$cl->SetArrayResult(true);
		$res = $cl->Query($keyword, 'cc_phpcms');
		print_r($res);die;

上面的php代码没有做输入的字符过滤,这个请按自己的需要加上。
另外,
/data/sh/other/sphinx_update_index.sh 跑了一次后,

vi /data/sh/other/sphinx_update_index.sh

#/usr/local/coreseek/bin/indexer --config ${CONFFILE}.2 --all --rotate
/usr/local/coreseek/bin/indexer --config ${CONFFILE}.2 --all

变成

/usr/local/coreseek/bin/indexer --config ${CONFFILE}.2 --all --rotate
#/usr/local/coreseek/bin/indexer --config ${CONFFILE}.2 --all

也就是将注释调换,这样以后就可以设定个定时计划跑/data/sh/other/sphinx_update_index.sh 脚本了,
跑了/sphinx_update_index.sh 脚本后,自动会用–rotate的方式重建索引,也就是说新增加的内容也将会被索引到了。

当然,最好的方法还是做个实时索引的配置,下一篇将会重点介绍sphinx的实时索引功能!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存儲斑點安全嗎?mysql:存儲斑點安全嗎?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通過PHP Web界面添加用戶mySQL:通過PHP Web界面添加用戶May 14, 2025 am 12:04 AM

通過PHP網頁界面添加MySQL用戶可以使用MySQLi擴展。步驟如下:1.連接MySQL數據庫,使用MySQLi擴展。 2.創建用戶,使用CREATEUSER語句,並使用PASSWORD()函數加密密碼。 3.防止SQL注入,使用mysqli_real_escape_string()函數處理用戶輸入。 4.為新用戶分配權限,使用GRANT語句。

mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能