首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>PHP专区>编程技巧>用PHP来构建自定义搜索引擎
用PHP来构建自定义搜索引擎
来源: 发布时间:2007-09-26 发布人: 浏览: 人次   字体: [ ]  

  最后一个配置步骤是构建索引。清单 8 显示了数据源 catalog 的索引。

  清单 8. 描述 catalog 数据源的一个可能的索引

index catalog
{
 source = catalog
 path = /var/data/sphinx/catalog
 morphology = stem_en

 min_word_len = 3
 min_prefix_len = 0
 min_infix_len = 3
}

  第 1 行将指向 sphinx.conf 文件中的指定数据源。第 2 行将定义存储索引数据的位置;按照约定,Sphinx 索引将被存储到 /var/data/sphinx 中。第 3 行将允许索引使用英文词法。并且第 5 行至第 7 行将告诉索引器只索引含有三个字符或更多字符的那些单词,并且为每个这样的字符的子字符串创建中缀索引(为了便于引用,清单 9 显示了 Body Parts 的完整示例 sphinx.conf 文件)。

  清单 9. Body Parts 的示例 sphinx.conf

source catalog
{
 type = mysql

 sql_host = localhost
 sql_user = reaper
 sql_pass = s3cr3t
 sql_db = body_parts
 sql_sock = /var/run/mysqld/mysqld.sock
 sql_port = 3306

 # indexer query
 # document_id MUST be the very first field
 # document_id MUST be positive (non-zero, non-negative)
 # document_id MUST fit into 32 bits
 # document_id MUST be unique

 sql_query = \
SELECT \
 id, partno, description, \
 assembly, model \
FROM \
 Catalog;

 sql_group_column = assembly
 sql_group_column = model

 # document info query
 # ONLY used by search utility to display document information
 # MUST be able to fetch document info by its id, therefore
 # MUST contain '$id' macro
 #

 sql_query_info = SELECT * FROM Inventory WHERE id=$id
}

index catalog
{
 source = catalog
 path = /var/data/sphinx/catalog
 morphology = stem_en

 min_word_len = 3
 min_prefix_len = 0
 min_infix_len = 3
}

searchd
{
 port = 3312
 log = /var/log/searchd/searchd.log
 query_log = /var/log/searchd/query.log
 pid_file = /var/log/searchd/searchd.pid
}

  底部的 searchd 部分将配置 searchd 守护程序本身。该部分中的条目不言自明。query.log 尤为有用:它将在运行时显示每次搜索并显示结果,例如搜索的文档数和匹配总数。
构建和测试索引

  您现在已经准备好为 Body Parts 应用程序构建索引。为此,需要执行以下步骤:

  键入 $ sudo mkdir -p /var/data/sphinx 创建目录结构 /var/data/sphinx
  假定 MySQL 正在运行,使用如下所示的代码运行索引器来创建索引。

  清单 10. 创建索引

$ sudo /usr/local/bin/indexer --config /usr/local/etc/sphinx.conf --all
Sphinx 0.9.7
Copyright (c) 2001-2007, Andrew Aksyonoff

using config file '/usr/local/etc/sphinx.conf'...
indexing index 'catalog'...
collected 8 docs, 0.0 MB
sorted 0.0 Mhits, 82.8% done
total 8 docs, 149 bytes
total 0.010 sec, 14900.00 bytes/sec, 800.00 docs/sec

  注: -all 参数将重构 sphinx.conf 中列出的所有索引。如果不需要重构所有索引,您可以使用其他参数只对部分索引进行重构。

  您现在可以使用如下所示的代码用 search 实用程序测试索引(不必运行 searchd 即可使用 search)。

  清单 11. 用 search 测试索引

$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf ENG
Sphinx 0.9.7
Copyright (c) 2001-2007, Andrew Aksyonoff

index 'catalog': query 'ENG ': returned 2 matches of 2 total in 0.000 sec

displaying matches:
1. document=8, weight=1, assembly=5, model=7
id=8
partno=ENG088
description=Cylinder head
price=55
2. document=9, weight=1, assembly=5, model=3
id=9
partno=ENG976
description=Large cylinder head
price=65

words:
1. 'eng': 2 documents, 2 hits

$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf wind
Sphinx 0.9.7
Copyright (c) 2001-2007, Andrew Aksyonoff

index 'catalog': query 'wind ': returned 2 matches of 2 total in 0.000 sec

displaying matches:
1. document=1, weight=1, assembly=3, model=1
id=1
partno=WIN408
description=Portal window
price=423
2. document=5, weight=1, assembly=3, model=1
id=5
partno=WIN958
description=Windshield, front
price=500

words:
1. 'wind': 2 documents, 2 hits

$ /usr/local/bin/search \
--config /usr/local/etc/sphinx.conf --filter model 3 ENG
Sphinx 0.9.7
Copyright (c) 2001-2007, Andrew Aksyonoff

index 'catalog': query 'ENG ': returned 1 matches of 1 total in 0.000 sec

displaying matches:
1. document=9, weight=1, assembly=5, model=3
id=9
partno=ENG976
description=Large cylinder head
price=65

words:
1. 'eng': 2 documents, 2 hits

  第一条命令 /usr/local/bin/search --config /usr/local/etc/sphinx.conf ENG 在零件号中找到了两个含有 ENG 的结果。第二条命令 /usr/local/bin/search --config /usr/local/etc/sphinx.conf wind 在两个零件描述中找到了子字符串 wind。而第三条命令把结果限定为 model 为 3 的条目。

共4页: 上一页 [1] [2] 3 [4] 下一页
相 关 文 章   发布商链接
·用PHP程序直接调用文本文件内容实例
·如何用PHP对文本文件加密并限制特定...
·PHP实现Mysql远程同步代码
·PHP程序开发利用PDFLib扩展模块动态...
·PHP利用Jmai组件实现邮件的发送代码
·实例讲解php实现中文水印的代码
·PHP是如何设置COOKIE的保留时间为浏...
·PHP读取站点的链接函数代码实例
·实例讲解PHP原生代码生成RSS文档
·利用PHP制作简单的内容采集器
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·实例讲解PHP原生代码生成RSS...
·PHP读取站点的链接函数代码实
·PHP是如何设置COOKIE的保留时
·实例讲解php实现中文水印的代
·PHP利用Jmai组件实现邮件的发
·PHP程序开发利用PDFLib扩展模
·PHP实现Mysql远程同步代码
·如何用PHP对文本文件加密并限
·用PHP程序直接调用文本文件内
·利用PHP制作简单的内容采集器
热 门 文 章
·PHP实现Mysql远程同步代码
·PHP程序开发利用PDFLib扩展模...
·如何用PHP对文本文件加密并限...
·PHP利用Jmai组件实现邮件的发...
·利用PHP制作简单的内容采集器
·实例讲解php实现中文水印的代...
·用PHP程序直接调用文本文件内...
·实例讲解PHP原生代码生成RSS...
·PHP读取站点的链接函数代码实...
·PHP是如何设置COOKIE的保留时...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .