av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

php啟用sphinx全文搜索的方法

時間:2025-05-02 06:32:07 php語言 我要投稿

php啟用sphinx全文搜索的方法

  主要介紹了php啟用sphinx全文搜索的實現(xiàn)方法,詳細講述了sphinx相關(guān)的配置與使用技巧,具有一定的參考借鑒價值,需要的朋友可以參考下。

  本文實例講述了php啟用sphinx全文搜索的實現(xiàn)方法。分享給大家供大家參考。具體分析如下:

  在編譯安裝 sphinx 的時候出現(xiàn)很多中文亂碼,最后拋出錯誤卡住了,我去到官方直接下載一個 rpm 包,安裝就很爽,具體錯誤不想研究了,忙開發(fā)呢.

  安裝兩個包,一個是 mmseg 這個是生成中文字典的程序,一個是  csft 也就是中國版的sphinx .

  rpm -ivh 安裝完以后,很順利~~不到半分鐘就裝完了.

  中文字典庫,我直接去 csft 官方下載了,挺好的想得很周到.

  unigram.txt  uni.lib

  unigram.txt  字典文本,可以在里面添加你自己的關(guān)鍵字.

  然后使用:mmseg -u unigram.txt 生成字典文件:unigram.txt.uni 然后重命名一下  uni.lib 這個就是sphinx 認識的字典了.

  放哪里?放你在 sphinx.conf 里面配置的字典路徑里面,等會說到,然后基本就差不多了,在看下sphinx 幾個實用的程序:

  復制代碼 代碼如下:

  [root@beihai365 /]# csft-

  csft-indexer  csft-search   csft-searchd

  csft-indexer  是生成全文搜索索引的 程序

  csft-search  是測試搜索是否生效用的,也很好用,不如我還沒用客戶端腳本開發(fā),就可以用這個來查看全文搜索是否成功

  csft-searchd  這個就是 sphinx 搜索的守護程序了。 啟動以后,就可以用腳本 php python 等,開查詢了。

  就那么簡單,在看下關(guān)鍵的兩部分東西.

  sphinx.conf 配置文件:

  復制代碼 代碼如下:

  source tmsgs

  {

  type                                    = mysql

  sql_host                                = localhost

  sql_user                                = root

  sql_pass                                = 1

  sql_db                                  = phpwind75sp3

  sql_port                                = 3306  # optional, default is 3306

  #sql_sock                                = /tmp/mysql3307.sock

  sql_query_pre                           = SET NAMES gbk

  sql_query                               = SELECT id,name,type,stock FROM pw_tools

  #sql_attr_uint                          = id

  sql_attr_uint                           = stock

  }

  index tmsgsindex

  {

  source                                  = tmsgs

  path                                    = /var/mmseg/searchdata/beihai365

  docinfo                                 = extern

  charset_type                            = zh_cn.gbk

  #min_prefix_len  = 0

  #min_infix_len  = 2

  #ngram_len = 2

  charset_dictpath                        = /var/mmseg/data

  #min_prefix_len                          = 0

  #min_infix_len                           = 0

  #min_word_len                            = 2

  }

  indexer

  {

  mem_limit                               = 128M

  }

  searchd

  {

  #listen                                = 3312

  log                                 = /var/log/searchd.log

  query_log                           = /var/log/query.log

  read_timeout                        = 5

  max_children                        = 30

  pid_file                            = /var/log/searchd.pid

  max_matches                         = 1000

  #seamless_rotate                     = 1

  #preopen_indexes                     = 0

  #unlink_old                          = 1

  }

  source tmsgs

  {

  type                                    = mysql

  sql_host                                = localhost

  sql_user                                = root

  sql_pass                                = 1

  sql_db                                  = phpwind75sp3

  sql_port                                = 3306  # optional, default is 3306

  #sql_sock                                = /tmp/mysql3307.sock

  sql_query_pre                           = SET NAMES gbk

  sql_query                               = SELECT id,name,type,stock FROM pw_tools

  #sql_attr_uint                          = id

  sql_attr_uint                           = stock

  }

  index tmsgsindex

  {

  source                                  = tmsgs

  path                                    = /var/mmseg/searchdata/beihai365

  docinfo                                 = extern

  charset_type                            = zh_cn.gbk

  #min_prefix_len  = 0

  #min_infix_len  = 2

  #ngram_len = 2

  charset_dictpath                        = /var/mmseg/data

  #min_prefix_len                          = 0

  #min_infix_len                           = 0

  #min_word_len                            = 2

  }

  indexer

  {

  mem_limit                               = 128M

  }

  searchd

  {

  #listen                                = 3312

  log                                 = /var/log/searchd.log

  query_log                           = /var/log/query.log

  read_timeout                        = 5

  max_children                        = 30

  pid_file                            = /var/log/searchd.pid

  max_matches                         = 1000

  #seamless_rotate                     = 1

  #preopen_indexes                     = 0

  #unlink_old                          = 1

  }

  再看一下,測試客戶端代碼:

  復制代碼 代碼如下:

  <?php

  header("Content-type:text/html;charset=utf-8");

  include 'sphinxapi.php';

  $cl = new SphinxClient();

  $cl->SetServer('localhost',3312);

  $cl->SetMatchMode(SPH_MATCH_ALL);

  $cl->SetArrayResult(true);

  $res = $cl->Query("名卡","*");

  print_r($res);

  ?>

  <?php

  header("Content-type:text/html;charset=utf-8");

  include 'sphinxapi.php';

  $cl = new SphinxClient();

  $cl->SetServer('localhost',3312);

  $cl->SetMatchMode(SPH_MATCH_ALL);

  $cl->SetArrayResult(true);

  $res = $cl->Query("名卡","*");

  print_r($res);

  ?>

  “名卡”這個關(guān)鍵字是我自己手動在字典里面添加的,看是否能真的搜到,實例代碼如下:

  復制代碼 代碼如下:

  Array

  (

  [error] =>

  [warning] =>

  [status] => 0

  [fields] => Array

  (

  [0] => name

  [1] => type

  )

  [attrs] => Array

  (

  [stock] => 1

  )

  [matches] => Array

  (

  [0] => Array

  (

  [id] => 8

  [weight] => 1

  [attrs] => Array

  (

  [stock] => 100

  )

  )

  )

  [total] => 1

  [total_found] => 1

  [time] => 0.018

  [words] => Array

  (

  [名卡] => Array

  (

  [docs] => 1

  [hits] => 1

  )

  )

  )

  Array

  (

  [error] =>

  [warning] =>

  [status] => 0

  [fields] => Array

  (

  [0] => name

  [1] => type

  )

  [attrs] => Array

  (

  [stock] => 1

  )

  [matches] => Array

  (

  [0] => Array

  (

  [id] => 8

  [weight] => 1

  [attrs] => Array

  (

  [stock] => 100

  )

  )

  )

  [total] => 1

  [total_found] => 1

  [time] => 0.018

  [words] => Array

  (

  [名卡] => Array

  (

  [docs] => 1

  [hits] => 1

  )

  )

  )

  完全沒問題,搜索出來了,幾個關(guān)鍵的操作:

  [root@beihai365 /]# csft-searchd --stop 停止搜索守護

  [root@beihai365 /]# csft-indexer --all 針對所有節(jié)點生成索引,你也可以針對某個節(jié)點生成索引比如:csft-indexer  xx

  [root@beihai365 /]# csft-search App 搜索關(guān)鍵字 App,不過看下面信息沒有搜到和沒有命中任何的文檔.

  Coreseek Full Text Server 3.1

  Copyright (c) 2006-2008 coreseek.com

  using config file './csft.conf'...

  1,

  pt:1, 1;        index 'tmsgsindex': query 'App ': returned 0 matches of 0 total in 0.017 sec

  words:

  1. 'app': 0 documents, 0 hits

  當大家在運行這些命令的時候發(fā)現(xiàn),需要你自己手動的置頂 --config  sphinx.conf   配置文件的路徑,很不方便,所以我干脆 ln -s 一個在 ./,這樣不用每次都去敲入  --config.

  希望本文所述對大家的php程序設(shè)計有所幫助。

【php啟用sphinx全文搜索的方法】相關(guān)文章:

php啟用sphinx全文搜索的實現(xiàn)方法03-26

PHP實現(xiàn)搜索查詢功能的方法技巧08-01

php+ajax實時輸入自動搜索匹配的方法05-16

PHP構(gòu)建自定義搜索引擎的方法05-14

如何啟用Apache的PHP-FPM多實例06-04

PHP的安裝方法03-03

php算法學習之寬度優(yōu)先搜索07-29

自學PHP方法04-26

jquery+php實現(xiàn)搜索框自動提示07-16