Ubuntu 下安装配置 Sphinx 及 PHP 中使用

23 篇文章 0 订阅
12 篇文章 0 订阅

Sphinx 介绍

Sphinx 是一个可全文搜索的开源搜索引擎。最大的特点是可以有效地执行大数据的搜索。要索引的数据可以来自完全不同的源:SQL 数据库,纯文本文件,HTML文件,邮箱等

install

sudo apt-get install sphinxsearch -y

configuring Sphinx

sudo vim /etc/sphinxsearch/sphinx.conf

Sphinx 配置包含 3个必须运行的主要块。它们是 index searchd source

source

The source block contains the type of source, username and password to the MySQL server. The first column of the SQL query should be a unique id. The SQL query will run on every index and dump the data to Sphinx index file. Below are descriptions of each field and the source block itself.

  • sql_query: This is the query thats dumps data to index.
source src1
{
  type          = mysql
  sql_host      = localhost
  sql_user      = root
  sql_pass      = password
  sql_db        = test
  sql_port      = 3306 # optional, default is 3306
  sql_query     = \
  SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
  FROM documents
  sql_attr_uint     = group_id
  sql_attr_timestamp    = date_added
  sql_query_info        = SELECT * FROM documents WHERE id=$id
}

index

The index component contains the source and the path to store the data.

  • source: Name of the source block. In our example, this is src1.
  • path: This path to save the index.
  • charset_type: This is the charset of the index.
index test1
{
  source            = src1
  path          = /var/lib/sphinxsearch/data/test1
  docinfo           = extern
  charset_type      = utf-8
}

searchd

The searchd component contains the port and other variables to run the Sphinx daemon.

  • listen: This is the port which sphinx daemon will run. In our example, this is 9312.
  • query_log: This path to save the query log.
  • pid_file: This is path to PID file of Sphinx daemon.
  • max_matches: Maximum number matches to return per search term.
  • seamless_rotate: Prevents searchd stalls while rotating indexes with huge amounts of data to precache.
  • preopen_indexes: Whether to forcibly preopen all indexes on startup.
  • unlink_old: Whether to unlink old index copies on successful rotation.
searchd
{
  listen            = 9312   # Port to listen on
  log           = /var/log/sphinxsearch/searchd.log
  query_log     = /var/log/sphinxsearch/query.log
  read_timeout      = 5
  max_children      = 30
  pid_file      = /var/run/sphinxsearch/searchd.pid
  max_matches       = 1000
  seamless_rotate       = 1
  preopen_indexes       = 1
  unlink_old        = 1
  binlog_path       = /var/lib/sphinxsearch/data
}

adding data to the index

sudo indexer -c /etc/sphinxsearch/sphinx.conf test1
# sudo indexer --all

为了使索引保持最新,创建 cronjob

crontab -e
@hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf --all

starting Sphinx

sudo searchd -c /etc/sphinxsearch/sphinx.conf

other way

默认情况下,Sphinx 守护程序已关闭。要启用 Sphinx,首先打开 /etc/default/sphinxsearch

sudo vim /etc/default/sphinxsearch
#Find the line START=no and set it to yes.
START=yes

Finally, start the Sphinx daemon.

sudo service sphinxsearch start

但是我遇到:

Job for sphinxsearch.service failed because the control process exited with error code. See "systemctl status sphinxsearch.service" and "journalctl -xe" for details

stop

sudo searchd --stop

testing search

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

using Sphinx by PHP

install PHP extension

安装 PHP Sphinx 依赖库

sudo aptitude install libsphinxclient-dev  libsphinxclient-0.0.1 -y

安装 PHP Sphinx 扩展

sudo pecl install sphinx

在配置文件 php.ini 中添加 Sphinx 的扩展

sudo vim /etc/php5/fpm/php.ini
extension=sphinx.so

重启 php5-fpm

sudo /etc/init.d/php5-fpm restart

call in PHP

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');
    dump($result);
}
Ubuntu 20.04上安装pocketsphinx可以参考以下步骤: 1. 首先,安装所需的库和组件。使用以下命令来安装依赖项: ``` sudo apt-get install ros-noetic-audio-common sudo apt-get install libasound2 sudo apt-get install gstreamer1.0-* sudo apt-get install gstreamer1.0-pocketsphinx ``` 2. 下载pocketsphinx的源代码。可以从官方网站上下载tar.gz文件。解压缩下载的文件: ``` tar -zxvf pocketsphinx-5prealpha.tar.gz ``` 3. 进入解压后的目录: ``` cd pocketsphinx-5prealpha/ ``` 4. 配置pocketsphinx安装环境,使用以下命令: ``` PYTHON=python3 ./configure ``` 5. 编译pocketsphinx: ``` sudo make ``` 6. 安装pocketsphinx: ``` sudo make install ``` 7. 安装pocketsphinx-python,可以使用以下命令: ``` pip install pocketsphinx ``` 8. 安装sphinxbase依赖项,使用以下命令: ``` sudo apt-get install bison sudo apt-get install swig ``` 经过以上步骤,您应该成功地在Ubuntu 20.04上安装了pocketsphinx。请确保按照步骤进行安装,并根据需要进行相应的配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【ubuntu 20.04】【pocketsphinx】【ros noetic】ubuntu 20.04下安装pocketsphinx过程并测试](https://blog.csdn.net/weixin_45058446/article/details/131718662)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ROS联合Webots扩展(二)通过语音控制机器人方案](https://blog.csdn.net/xiaokai1999/article/details/117572419)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值