|
用 PHP 构建自定义搜索引擎(6) 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) 在视图中,字段 id 将指回 Inventory 表中的零件条目。partno 和 description 列是要搜索的主要文本,而 assembly 和 model 列用作进一步过滤结果的组。视图就绪后,构造数据源查询就是小事一桩。清单 7 显示了 catalog 数据源定义的其余部分。
清单 7. 查询创建待索引的行
# 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 } sql_query 必须包括后续查找需要使用的主键,并且它必须包括需要索引和用作组的所有字段。两个 sql_group_column 条目将声明 Assembly 和 Model 可用于过滤结果。并且 search 实用程序将使用 sql_query_info 来查找匹配记录。在查询中,$id 被替换为 searchd 返回的每个主键。
最后一个配置步骤是构建索引。清单 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 文件)。
|