用PHP构建自定义搜索引擎

豆豆网   技术应用频道   2007年09月12日  【字号: 收藏本文

内容摘要:虽然 Google 及其系列产品几乎无所不能,但是 Web 形式的强大搜索引擎并不能很好地适用于每个站点。如果站点内容已被高度专业化或已明确分类,那就需要使用 Sphinx 和 PHP 来创建一个优化的本地搜索系统。

  接下来,创建一个查询以生成要被索引的行。通常,将创建 SELECT 子句,可能需要把许多表 JOIN 在一起才能得到行。但这里存在一个问题:搜索型号和年份必须使用 Assembly 表,但是零件号和零件描述只能在 Inventory 表中找到。为此,Sphinx 必须能够把搜索结果与 32 位整型主键绑定在一起。

  要获得右侧表单中的数据,需要创建一个视图 —— MySQL V5 中的新结构,它将把来自其他表的列整合到单独的合成虚拟表中。使用视图,各类搜索所需的所有数据都在一个位置,但是活动数据实际上存在于其他表中。清单 6 显示了定义 Catalog 视图的 SQL。

  清单 6. Catalog 视图将把数据整合到虚拟表中

CREATE OR REPLACE VIEW Catalog AS
SELECT
 Inventory.id,
 Inventory.partno,
 Inventory.description,
 Assembly.id AS assembly,
 Model.id AS model
FROM
 Assembly, Inventory, Model, Schematic
WHERE
 Schematic.partno_id=Inventory.id
 AND Schematic.model_id=Model.id
 AND Schematic.assembly_id=Assembly.id;

  如果用前面所示的表和数据创建名为 body_parts 的数据库,则 Catalog 视图应当类似以下内容:

mysql> use body_parts;
Database changed
mysql> select * from Catalog;
+----+---------+---------------------+----------+-------+
| id | partno | description     | assembly | model |
+----+---------+---------------------+----------+-------+
| 6 | 765432 | Bolt        |    5 |   1 |
| 8 | ENG088 | Cylinder head    |    5 |   1 |
| 1 | WIN408 | Portal window    |    3 |   1 |
| 5 | WIN958 | Windshield, front  |    3 |   1 |
| 4 | ACC5409 | Cigarette lighter  |    7 |   3 |
| 9 | ENG976 | Large cylinder head |    5 |   3 |
| 8 | ENG088 | Cylinder head    |    5 |   7 |
| 6 | 765432 | Bolt        |    5 |   7 |
+----+---------+---------------------+----------+-------+
8 rows in set (0.00 sec)

来源:ibm    作者:Martin Streicher    责编:豆豆技术应用

正在加载评论...