PHP - Sphinx安裝與使用方法

Sphinx搜索引擎

安裝

1、安裝mysql的開發包

sudo apt-get install mysql-client mysql-server mysql-common
sudo apt-get install libmysqld-dev libmysqlclient-dev

2、安裝sphinx

  • 下載sphinx 下載地址
  • 安裝命令./configure --prefix=/usr/local/sphinx --with-mysql & make & make install
  • 生成xdict文件/usr/local/sphinx/bin/mkdict xdict_1.1.txt xdict
  • 複製xdict到sphinx根目錄cp xdict /usr/local/sphinx/etc/
  • 生成sphinx配置文件cp /usr/local/sphinx/etc/sphinx.conf.dist /usr/local/sphinx/etc/sphinx.conf

3、配置sphinx

  • 示例如下:
source basic
{
    type                = mysql
    sql_host            = 192.168.1.10
    sql_user            = user
    sql_pass            = password
    sql_db              = db_name
    sql_port            = 3306
    sql_ranged_throttle = 0
}
source ad_src : basic {
    sql_query_pre       = SET NAMES utf8
    sql_query           = SELECT id,material
    sql_attr_uint       = id
    sql_field_string    = material
}
index ad
{
    source             = ad_src
    path               = /usr/local/sphinx/var/data/ad
    chinese_dictionary = /usr/local/sphinx/etc/xdict
    docinfo            = extern
    dict               = keywords
    mlock              = 0
    morphology         = none
    min_word_len       = 1
    min_prefix_len     = 0
    min_infix_len      = 1
    ngram_len          = 1
    html_strip         = 0
}

indexer
{
    mem_limit       = 128M
}

searchd
{
    listen                       = 9312
    log                          = /usr/local/sphinx/var/log/searchd.log
    query_log                    = /usr/local/sphinx/var/log/query.log
    read_timeout                 = 5
    client_timeout               = 300
    max_children                 = 30
    persistent_connections_limit = 30
    pid_file                     = /usr/local/sphinx/var/log/searchd.pid
    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
}
  • 簡單的解釋如下:

1、source basic是定義一個數據庫基本信息,用來給其他的sql查詢源做基。

2、ad_src是一個繼承於basic的查詢源,就擁有了mysql的基礎信息,不用再設置了

3、index ad是索引名,主要用來配置索引路徑與中文路徑

4、indexer與searchd是根據配置文件創建索引和開啓守護進程

使用方法

1、索引的建立

  • 某個索引 /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf <indexname>
  • 全部索引 indexname設置爲空,增加--all 選項
  • 強制建立,忽略守護進程 增加--rotate

2、守護進程

  • 開啓 /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf
  • 關閉 立即/usr/local/sphinx/bin/searchd --stop 或 等待結束後停止--stopwait

3、php中加載

  • 示例代碼
<?php
include_once("sphinxapi.php");
$sphinx = new SphinxClient();
$sphinx->SetServer($ip,$port);  
$sphinx->SetConnectTimeout(3);  
$sphinx->SetMaxQueryTime(2000); 
$sphinx->Query($keyword,$indexname);
$sphinx->GetLastError();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章