Sphinx在windows下安装使用

字号+ 编辑: 国内TP粉 修订: 种花家 来源: ThinkPHP 2023-09-22 我要说两句(0)

前一阵子尝试使用了一下 Sphinx ,一个能够被各种语言 (PHP/Python/Ruby/etc) 方便调用的全文检索系统。网上的资料大多是在 linux 环境下的安装使用,当然,作为生产环境很有必要部署在 *nix 环境下,作为学习测试,还是 windows 环境比较方便些。本文旨在提供一种便捷的方式让 Sphinx 在 windows 下安装配置以支持中文全文检索,配置部分在 linux 下通用。

Sphinx的介绍

Sphinx是一个以GPLv2许可发布的全文搜索引擎,如果需要在商业项目中使用(例如嵌入到其他程序中),则需要联系Sphinx Technologies Inc.获取商业授权。

一般来说,Sphinx是一个独立的搜索引擎,旨在为其他应用程序提供高速、低空间占用和高相关性的全文搜索功能。Sphinx可以与SQL数据库和脚本语言轻松集成。

目前,Sphinx内置了对MySQL和PostgreSQL数据库的支持,并且还支持从标准输入读取特定格式的XML数据。通过修改源代码,用户可以自行添加新的数据源,例如对其他类型的数据库管理系统的原生支持。

Sphinx的搜索API支持PHP、Python、Perl、Ruby和Java,并且还可以用作MySQL存储引擎。搜索API非常简单,可以在几个小时内移植到新的编程语言上使用。

Sphinx 特性:

  1. 高速的建立索引 ( 在当代 CPU 上,峰值性能可达到 10MB/ 秒 );

  2. 高性能的搜索 ( 在 2–4GB 的文本数据上,平均每次检索响应时间小于 0.1 秒 ); 可处理海量数据 ( 目前已知可以处理超过 100GB 的文本数据 , 在单一 CPU 的系统上可处理 100M 文档 );

  3. 提供了优秀的相关度算法,基于短语相似度和统计( BM25 )的复合 Ranking 方法 ;

  4. 支持分布式搜索 ;

  5. 提供文件的摘录生成 ;

  6. 可作为 MySQL 的存储引擎提供搜索服务 ;

  7. 支持布尔、短语、词语相似度等多种检索模式 ;

  8. 文档支持多个全文检索字段 ( 最大不超过 32 个 );

  9. 文档支持多个额外的属性信息 ( 例如:分组信息,时间戳等 );

  10. 停止词查询 ;

  11. 支持单一字节编码和 UTF-8 编码 ;

  12. 原生的 MySQL 支持 ( 同时支持 MyISAM 和 InnoDB);

  13. 原生的 PostgreSQL 支持 .

Sphinx 在 windows 上的安装

1. 下载sphinx包

直接在 http://www.sphinxsearch.com/downloads.html 找 到最新的 windows 版本,我这里下的是<a href="http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1-win32.zip" target="_blank"> Win32 release binaries with MySQL support</a>,下载后解压在 D:/sphinx 目录下;

2. 建立索引文件

在 D:/sphinx/ 下新建一个 data 目录用来存放索引文件,一个 log 目录放日志文件;

3. 修改配置

复制 D:/sphinx/sphinx.conf.in 到 D:/sphinx/bin/sphinx.conf (注意修改文件名)

修改 D:/sphinx/bin/sphinx.conf ,我这里 列出需要修改的几个:

type           = mysql # 数据源,我这里是mysql
sql_host       = localhost # 数据库服务器
sql_user       = root # 数据库用户名
sql_pass       = '' # 数据库密码
sql_db         = test # 数据库
sql_port       = 3306 # 数据库端口
sql_query_pre      = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8 编码的
index test1
{
# 放索引的目录

  path      = D:/sphinx/data/
# 编码

  charset_type     = utf-8

  #  指定utf-8 的编码表

  charset_table     = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F

  # 简单分词,只支持0 和1 ,如果要搜索中文,请指定为1

  ngram_len       = 1
# 需要分词的字符,如果要搜索中文,去掉前面的注释

  ngram_chars      = U+3000..U+2FA1F
}
# 搜索服务需要修改的部分
searchd
{

  # 日志

  log        = D:/sphinx/log/searchd.log
# PID file, searchd process ID file name

  pid_file      = D:/sphinx/log/searchd.pid
# windows 下启动searchd 服务一定要注释掉这个

  # seamless_rotate     = 1
}

4. 导入测试数据

将sql文件导入数据库(sql 文件在 D:/sphinx/example.sql)

C:/Program Files/MySQL/MySQL Server 5.0/bin>mysql -uroot blog<d:/sphinx/example.sql

说明:

(1)由于我的数据库名称叫blog,因此这里我写的是blog,blog数据库编码设置为utf8

(2)example.sql中默认的数据库名为test,在导入前修改为统一替换为blog即可,若本身就是在test数据库下测试,就不用修改了

5. 建立索引

打开cmd窗口,进入目录D:\sphinx\bin

D:/sphinx/bin>indexer.exe test1 ( 备注 :test1 为 sphinx.conf 的 index test1() )
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec

6. 搜索“test”试试

D:/sphinx/bin>search.exe test

显示结果如下

using config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groups
words:
1. ‘test’: 3 documents, 5 hits

6. 测试中文搜索

修改 blog数据库中 documents 数据表,

UPDATE `blog`.`documents` SET `title` = ‘ 测试中文 ’, `content` = ‘this is my test document number two ,应该搜的到吧 ’ WHERE `documents`.`id` = 2;

重建索引:

D:/sphinx/bin>indexer.exe test1

搜索关键字: 中文 试试:

D:/sphinx/bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…index ‘test1′: query ‘ 中文 ‘: returned 0 matches of 0 total in 0.000 sec
words:
D:/sphinx/bin>

貌似没有搜到,这是因为 windows 命令行中的编码是 gbk ,当然搜不出来。我们可以用程序试试,在 D:/sphinx/api 下新建一个 foo.php 的文件,注意 utf-8 编码

<?php
require ’sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(’localhost’,9312);
$result = $s->Query(’ 中文 ’);
var_dump($result);

启动 Sphinx searchd 服务

D:/sphinx/bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
WARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:9312
accepting connections

执行 PHP 查询:

访问 http://www.test.com/sphinx/api/foo.php ( 自己配置的虚拟主机 )

阅完此文,您的感想如何?
  • 有用

    0

  • 没用

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • Windows台式机和笔记本电脑连接到系统中的设备没有发挥作用

  • Windows系统的桌面快捷方式删不掉的解决方法

  • 电脑拨号上网或宽带连接显示错误651

  • 取消搜狗弹出的“搜狐微门户”广告网页窗口的实现方法(杀死SohuNews.exe进程)

我要说说
网上宾友点评