php中如何使用sphinx搜尋引擎

小云云
發布: 2023-03-21 20:56:01
原創
2526 人瀏覽過

 sphinx是一個高效的搜尋引擎,分詞搜尋的速度比較快,索引建立儲存在硬碟文件,不會幹擾資料庫,有自己內建的一套資料庫.希望能幫助大家。

php中使用sphinx搜尋引擎

sphinx是一個高效的搜尋引擎,分詞搜尋的速度比較快,索引建立儲存在硬碟文件,不會幹擾資料庫,有自己內建的一套資料庫

1.ubuntu安裝sphinx

如果沒有安裝aptitude ,需要先安裝aptitude 因為因為用apt get install 安裝下面指令會出現問題.<br> sudo apt-get install aptitude<br>sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y

2.設定

################################# ########
<br>
登入後複製
登入後複製
登入後複製
###1######2##########
<br>
登入後複製
登入後複製
登入後複製
###cd /etc/sphinxsearch/######cp sphinx.conf. sample sphinx.conf################

修改設定檔如下<br>

<br>
登入後複製
登入後複製
登入後複製

<br>

  1. 執行指令分詞,會在/var/lib/sphinxsearch/data/test1 目錄下產生一堆索引檔<br>sudo indexer - c /etc/sphinxsearch/sphinx.conf test1

    #test1為上述設定檔的index名字

4.命令列測試搜尋

sudo search -c /etc/sphinxsearch/sphinx.conf google

二.在php中使用

1.安裝php sphinx的依賴函式庫

1.安裝aptitude

apt-get install aptitude<br>sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y

2.安裝php sphinx的擴充功能

安裝pecl<br>sudo apt-get install php-pear php5-dev##在安裝sphinx<br><br>sudo pecl install sphinx

3.在設定檔php.ini中加入sphinx的擴充功能,
我的php.ini檔為

<br># sudo vim /etc/php5/fpm/php.ini#取得自己的php.ini檔案位置使用<br><br>php5-fpm -i|grep ini

#######################################

新增:<br>extension=sphinx.so<br>4.重啟php5-fpm,查看php是否載入sphinx模組<br>sudo /etc/init.d/php5 -fpm restart<br>5.將search程式運行在後台<br>sudo searchd -c /etc/sphinxsearch/sphinx.conf<br>預設監聽設定檔中的連接埠:9312

6.在thinkphp中呼叫搜尋<br>

<br>
登入後複製
登入後複製
登入後複製

1

2

##3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#40

41

42

43

44

45

#46

#47

48

49

50

#51

source src1

#{

type = mysql

sql_host = localhost

sql_user = root

sql_pass = magicmoma

#sql_db = coupon_20160901

sql_port = 3306 # optional, default is 3306

sql_query = SELECT couponid,title,description FROM app_coupon_api

欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引列包含主鍵,分詞索引欄位包含主鍵,分詞索引欄位包含主鍵,分詞索引位包含主鍵碼

#}

index test1

{

source = src1

path = /var/lib/sphinxsearch/ data/test1 #索引存放目錄

docinfo = extern

mlock = 0

morphology = none

min_word_len = 1

charset_type = utf-8

min_prefix_len = 0

min_infix_len = 0

ngram_len = 1

#html_strip = 0

}

#indexer

{

mem_limit = 2048M

}<br>

##searchd

#{

listen = 9312

listen = 9306:mysql41

log = /var/log/sphinxsearch/searchd.log

query_log = /var/log /sphinxsearch/query.log

read_timeout = 5

client_timeout = 300

max_children = 30

pid_file = /var/run/sphinxsearch/searchd. pid

max_matches = 1000

seamless_rotate = 1

preopen_indexes = 1

##unlink_old = 1

mva_updates_pool = 1M

max_packet_size = 8M

max_filters = 256

max_filter_values = 4096

##max_batch_queries = 32

workers = threads # for RT to work

##}

#
<br>
登入後複製
登入後複製
登入後複製
#
<br>
登入後複製
登入後複製
登入後複製

1

##2

3

4

5

6

7

#8

9

10

11

12

13

public function testSphinx()

{

$s = new \SphinxClient;

$s->setServer("localhost", 9312);

$s->SetArrayResult (true );

$s->setMatchMode(SPH_MATCH_ANY);

$s->setMaxQueryTime(3);

$result = $s->query("test");

$result = $result['matches'];

$result = array_column($result,'id');

$list = M('CouponApi')-> ;field('couponid,title,description')->where(array('couponid'=>array('in',$result)))->select();

dump( $list);

}

搜尋完畢,回傳結果(預設回傳20條,修改回傳條數用新增 

$ s->SetLimits(0, 1000, 1000);),搜尋速度相當快,索引80w條資料的title和description兩個欄位耗時不到10s,該搜尋引擎支援增量索引,多種模式的搜索,網上的資料也非常多

以上是php中如何使用sphinx搜尋引擎的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!