从实际结果中显示带有相关文本片段的搜索结果的最佳做法 [英] Best practices on displaying search results with associated text snippets from actual result
问题描述
我有一个不错的,轻量级的搜索引擎,该网站使用MySQL全文索引和php解析结果,可用于我的一个网站.工作正常,但我想提供更多类似Google的搜索结果",其中包含搜索结果中的文字摘录以及突出显示的单词.寻找基于php的解决方案.有什么建议吗?
I have a decent, lightweight search engine working for one of my sites using MySQL fulltext indexes and php to parse the results. Work fine but I'd like to offer more 'google-like' results with text snippets from the results and the found words highlighted. Looking for a php based solution. Any recommendations?
推荐答案
搜索实际的数据库很好,直到您想要添加像上面的那样令人眼花features乱的功能为止.以我的经验,最好创建一个专用的搜索表,其中包含关键字和页面ID/URL/等.然后每 n 小时用内容填充此表.在此期间,您可以为每个文档的每个关键字添加摘录.
Searching the actual database is fine until you want to add snazzy features like the one above. In my experience it is best to create a dedicated search table, with keywords and page IDs/URLs/etc. Then populate this table every n hours with content. During this population you can add snippets for each document for each keyword.
或者,一种快速的破解方法可能是:
Alternatively a quick hack might be:
<?php
$text = 'This is an example text page with content. It could be red, green or blue.';
$keyword = 'red';
$size = 5; // size of snippet either side of keyword
$snippet = '...'.substr($text, strpos($text, $keyword) - $size, strpos($text, $keyword) + sizeof($keyword) + $size).'...';
$snippet = str_replace($keyword, '<strong>'.$keyword.'</strong>', $snippet);
echo $snippet;
?>
这篇关于从实际结果中显示带有相关文本片段的搜索结果的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!