lpi-lab/search-bundle
使用 lucene 搜索引擎的打包工具
0.1.8
2015-05-04 08:38 UTC
Requires
- php: >=5.3.3
- egeloen/lucene-search-bundle: ~1.0
- zendframework/zendsearch: dev-master
This package is not auto-updated.
Last update: 2024-09-14 17:20:43 UTC
README
搜索包是一个提供轻量级搜索引擎的包。它基于 lucene 搜索库并包含 zend 组件。
安装
- 在 AppKernel 中
new Ivory\LuceneSearchBundle\IvoryLuceneSearchBundle(), new Lpi\Bundle\SearchBundle\LpiSearchBundle(),
配置
- 在 app/config/config.yml 中
ivory_lucene_search: # Index identifier search_index: # Path to store the index (Required) path: %kernel.cache_dir%/search_index # Index analyser (Optional) # See http://framework.zend.com/manual/en/zend.search.lucene.charset.html analyzer: ZendSearch\Lucene\Analysis\Analyzer\Common\Text\CaseInsensitive # Max Buffered documents (Optional) # See http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html#zend.search.lucene.index-creation.optimization.maxbuffereddocs max_buffered_docs: 10 # Max merge documents (Optional) # See http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html#zend.search.lucene.index-creation.optimization.maxmergedocs max_merge_docs: 10000 # (default: PHP_INT_MAX) # Merge factor (Optional) # See http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html#zend.search.lucene.index-creation.optimization.mergefactor merge_factor: 10 # Index directory permission (Optional) # See http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html#zend.search.lucene.index-creation.permissions permissions: 0777 # Auto optmized flag (Optional) # If this flag is true, each time you request an index, it will be optmized # See http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html#zend.search.lucene.index-creation.optimization auto_optimized: false # Query parser encoding (Optional) # See http://framework.zend.com/manual/en/zend.search.lucene.searching.html#zend.search.lucene.searching.query_building.parsing query_parser_encoding: "UTF-8" # (default: "")
- 对于您想要索引的每个实体,您需要添加 "Lpi\Bundle\SearchBundle\Model\IndexableInterface",一个实体需要具有以下 4 个方法:
- getId()
- getTitle()
- getSlug()
- getDescription()
- 在 app/config/config.yml 中
lpi_search: mappings: - { value: ApplicationLpiEventBundle:Event , path: programmation_detail} #name of the entity you want to be indexed
使用
现在您已经注册了所有索引,并且想要使用它们,非常简单!
例如,在控制器中
/** * @param Request $request * @return array * @Route("/search", name="path_search") * @Template() * @Method({"GET", "POST"}) */ public function searchAction(Request $request) { $results = null; if ($request->request->has('term') and '' !== $request->request->get('term')) { $results = $this->get('lpi_lucene.search')->search($request->request->get('term')); } return array( 'results' => $results ); }
并在您的视图中渲染,例如
{% if results is defined and results|length > 0 %}
{% for result in results %}
<div class="col-xs-12">
<a href="{{ result.url }}" title="{{ result.title }}">
{{ result.title }}
</a>
</div>
{% endfor %}
{% else %}
<div class="alert alert-warning">{{ 'search.result.nothing'|trans({}, 'messages') }}</div>
{% endif %}