来叩网
请输入搜索词:
  

sphinx 全文搜索应用(一) – 运维生存时间

以下是大数据处理后,找到符合xml全文搜索 sphinx 全文搜索应用(一) – 运维生存时间的所有相关结果,可以点击标题打开查看!

一.安装配置

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

配置如下:

# vi sphinx.conf

# source块设置数据源

123456789101112131415 source src1{type = mysqlsql_host = localhostsql_user = rootsql_pass =sql_db = sphinxsql_port = 3306 # optional, default is 3306sql_query = \SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \FROM documentssql_attr_uint = group_idsql_attr_timestamp = date_addedsql_query_info = SELECT * FROM documents WHERE id=$id}

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

1234567 index test1{source = src1path = /data/sphinx/docinfo = externcharset_type = sbcs}

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

1234 indexer{mem_limit = 32M}

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

12345678910111213 searchd{port = 9312log = /var/log/searchd.logquery_log = /var/log/query.logread_timeout = 5max_children = 30pid_file = /var/log/searchd.pidmax_matches = 1000seamless_rotate = 1preopen_indexes = 0unlink_old = 1}

二.新建相关表

12345678910 mysql> CREATE TABLE documents-> (-> id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,-> group_id INTEGER NOT NULL,-> group_id2 INTEGER NOT NULL,-> date_added DATETIME NOT NULL,-> title VARCHAR(255) NOT NULL,-> content TEXT NOT NULL-> );Query OK, 0 rows affected (0.00 sec)

1234567 mysql> REPLACE INTO documents ( id, group_id, group_id2, date_added, title, content ) VALUES-> ( 1, 1, 5, NOW(), 'test one', 'this is my test document number one. also checking search within phrases.' ),-> ( 2, 1, 6, NOW(), 'test two', 'this is my test document number two' ),-> ( 3, 2, 7, NOW(), 'another doc', 'this is another group' ),-> ( 4, 2, 8, NOW(), 'doc number four', 'this is to test groups' );Query OK, 4 rows affected (0.02 sec)Records: 4 Duplicates: 0 Warnings: 0

1234567 mysql> CREATE TABLE tags-> (-> docid INTEGER NOT NULL,-> tagid INTEGER NOT NULL,-> UNIQUE(docid,tagid)-> );Query OK, 0 rows affected (0.01 sec)

1234567 mysql> INSERT INTO tags VALUES-> (1,1), (1,3), (1,5), (1,7),-> (2,6), (2,4), (2,2),-> (3,15),-> (4,7), (4,40);Query OK, 10 rows affected (0.02 sec)Records: 10 Duplicates: 0 Warnings: 0

三.创建索引

# 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

12345678910

结果如下:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 Array( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => content )  [attrs] => Array  ( [group_id] => 1 [date_added] => 2 )  [matches] => Array //匹配的结果 ( [0] => Array ( [id] => 1 [weight] => 2 [attrs] => Array ( [group_id] => 1 [date_added] => 1339747121 ) )  [1] => Array ( [id] => 2 [weight] => 2 [attrs] => Array ( [group_id] => 1 [date_added] => 1339747121 ) ) [2] => Array ( [id] => 4 [weight] => 1 [attrs] => Array ( [group_id] => 2 [date_added] => 1339747121 ) ) )  [total] => 3 [total_found] => 3 [time] => 0.000 [words] => Array ( [test] => Array ( [docs] => 3 [hits] => 5 ) ))

说明: [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

收 藏
相关搜索
网友推荐
睡母邪恶漫画图片大全 知识产权保护漫画图片 社会喵壁纸漫画图片 福利网韩国漫画图片大全下载 秋游漫画图片 五年级 笔的漫画图片 简单的漫画人图片大全大图 简单的组合漫画图片大全 简笔画人物漫画图片大全图片大全图片大全 简笔画翻纸漫画图片
揭秘金字塔的未解之谜 百慕大三角未解之谜的英语 月相未解之谜 古剑三的未解之谜 世界未解之谜大全第一部 埃及法老墓未解之谜 世界上未解的三大之谜 骷髅复活未解之谜 茂县未解之谜 和平精英未解之谜大全