jbouzekri / sculpin-search-bundle
提供通过 indextank 搜索引擎在静态网站中进行搜索
Requires
- flaptor/indextank-php: @dev
This package is auto-updated.
Last update: 2024-09-12 18:56:57 UTC
README
此套餐为您的静态网站提供了一个使用 indextank 服务的搜索引擎。
您可以使用 Indexden 服务。
您可以在我的 个人法语博客 的右侧列中看到一个工作演示。
安装
使用 composer,将依赖项添加到您的 composer.json 文件中
"require": { "jbouzekri/sculpin-search-bundle": "1.*", "flaptor/indextank-php": "@dev" }, "repositories": [ { "type": "vcs", "url": "https://github.com/flaptor/indextank-php" } ]
然后运行 composer update 命令(indextank 必须在您的 composer.json 中声明,因为当前稳定版本不包括 composer 文件,并且它尚未在 packagist 上,因此需要仓库部分)
启用套餐。如果您已经有 app/SculpinKernel.php,请将此套餐添加到其中,否则创建文件并包含以下内容
<?php class SculpinKernel extends \Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel { protected function getAdditionalSculpinBundles() { return array( 'Jb\Bundle\SearchBundle\JbSearchBundle' ); } }
然后您需要在 sculpin_kernel.yml 中配置 indextank 服务
jb_search: options: url: http://login.api.indexden.com user: "Your login here" password: "Your password here" index: "The name of your index"
如果您使用 indexden,您可以使用私有 URL 填充这些参数:http://:password@login.api.indexden.com
使用方法
在您想要索引的每个帖子中,在文件的 markdown 部分添加 indexed: true 数据。
--- indexed: true ---
现在您可以重新生成您的网站。
现在您可以向网站添加 HTML 表单标记
<form id="search-form" action="/search" method="get"> <div class="input-group" id="search"> <input type="text" class="form-control" id="search-field" name="q" placeholder="Search" autocomplete="off" /> <span class="input-group-btn"> <button class="btn btn-default" type="submit"> <span class="glyphicon glyphicon-search"></span> </button> </span> </div> </form>
此套餐提供了一个简单的 indextank 客户端库(在 js 中使用)。将 Resources/public/js/indextank_client.js 添加到您的项目中。现在您可以使用以下代码在执行搜索时更新您的帖子列表
$('#search-form').indexTank({ url: "http://login.api.indexden.com", index: "index_name", display: function(result) { if (result.matches == 0) { $('.entries').html('<li>No result</li>'); return; } var html = ""; for (id in result.results) { var date = new Date(result.results[id].date*1000); html += '\ <li> \ <small>' + date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear() + '</small> \ <a href="' + result.results[id].link + '">' + result.results[id].title + '</a> \ </li>'; } $('.entries').html(html); } });
工作原理
一个事件监听器绑定到 afterRun 事件。它将所有带有 indexed 标志的文档索引到 indextank 中。
在索引时,它会清除索引并批量添加所选源,因此在您索引大量文档时,生成网站可能需要一些时间。
因此,我添加了一个 generate 命令,它包装了原始的 sculpin generate 命令,以便添加 no-index 选项。当运行此命令时,您可以工作在静态网站上,并且每次您在文件中更改内容时,内容都不会被索引。(但是,不要忘记在最后运行不带此参数的 generate 命令以更新搜索服务中的索引)。
$ php sculpin generate-search --server --watch --no-index
配置参考
jb_search: enabled: true # enable the indexation at the end of the generate task engine: indextank # change the engine (for now only indextank is supported) options: url: http://login.api.indexden.com user: "Your login here" password: "Your password here" index: "The name of your index"