sphinx 全文搜索应用(一)

  • A+
所属分类:Sphinx

一.安装配置

安装参见: http://www.ttlsa.com/html/1236.html

配置如下:

# vi sphinx.conf

# source块设置数据源

# index块设置索引保存目录等

# indexer块设置索引选项,比如内存限制大小

# searchd块设置搜索索引时使用的选项

二.新建相关表

三.创建索引

# indexer --all

using config file '/etc/sphinxsearch/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.072 sec, 2644 bytes/sec, 54.80 docs/sec

total 3 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg

total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg

 

四.测试

# search test

using config file '/etc/sphinxsearch/sphinx.conf'...

index 'test1': query 'test ': returned 3 matches of 3 total in 0.022 sec

 

displaying matches:

1. document=1, weight=2421, group_id=1, date_added=Fri Jun 15 15:58:41 2012

id=1

group_id=1

group_id2=5

date_added=2012-06-15 15:58:41

title=test one

content=this is my test document number one. also checking search within phrases.

2. document=2, weight=2421, group_id=1, date_added=Fri Jun 15 15:58:41 2012

id=2

group_id=1

group_id2=6

date_added=2012-06-15 15:58:41

title=test two

content=this is my test document number two

3. document=4, weight=1442, group_id=2, date_added=Fri Jun 15 15:58:41 2012

id=4

group_id=2

group_id2=8

date_added=2012-06-15 15:58:41

title=doc number four

content=this is to test groups

 

words:

1. 'test': 3 documents, 5 hits

 

五.使用sphinx php API测试

1.将sphinxapi.php(此文件在源码api目录下)拷贝到正确位置(取决于个人环境)

# vi search.php

结果如下:

说明: [docs] => 3有三条结果想匹配,[hits] => 5命中5次,与数据库相对应的id是1,2,4.耗时[time] => 0.000

查询数据库结果如下:

mysql> select *, (select unix_timestamp(date_added)) as ut from documents where id in (1,2,4);

+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+------------+

| id | group_id | group_id2 | date_added | title | content | ut |

+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+------------+

| 1 | 1 | 5 | 2012-06-15 15:58:41 | test one | this is my test document number one. also checking search within phrases. | 1339747121 |

| 2 | 1 | 6 | 2012-06-15 15:58:41 | test two | this is my test document number two | 1339747121 |

| 4 | 2 | 8 | 2012-06-15 15:58:41 | doc number four | this is to test groups | 1339747121 |

+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+------------+

3 rows in set (0.00 sec)

因此对于大量数据,为了提高查询速度,可以先通过sphinx查找出id,再通过id来查询数据库数据。

如需转载请注明出处:http://www.ttlsa.com/html/1346.html

weinxin
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  0   博主  0   引用   1

    来自外部的引用: 1

    • sphinx 全文搜索应用(二)