digitlab/search-parser

Laravel 的搜索字符串解析器。

v1.0.0 2016-04-22 06:13 UTC

This package is not auto-updated.

Last update: 2024-09-26 01:00:06 UTC


README

StyleCI Build Status Total Downloads Latest Stable Version License

将 Lucene/Google 风格的搜索字符串解析为 PostgreSQL 全文查询字符串。

安装

使用 composer 安装

composer require digitlab/search-parser

在 app/config/app.php 中添加服务提供者

DigitLab\SearchParser\SearchParserServiceProvider::class,

使用方法

基本使用

要解析全文查询,您可以直接使用 SearchParser

$parser = new SearchParser();
$filters = $parser->parse('string to parse');

将产生

[
    'search' => 'string&to&parse'
]

过滤器

要处理过滤器,您需要扩展 SearchParser 并添加处理函数或添加透传过滤器

class CustomSearchParser extends SearchParser
{
    /**
     * The filters that should be returned without handlers.
     *
     * @var array
     */
    protected $passthruFilters = ['other'];

    /**
     * Handle the state filter.
     *
     * @param mixed $state
     * @return array
     */
    protected function handleState($state)
    {
        return ['some' => $state];
    }
}
$parser = new CustomSearchParser();
$filters = $parser->parse('state:pending other:string string to parse');

将产生

[
    'search' => 'string&to&parse',
    'some' => 'pending',
    'other' => 'string'
]

自定义查询键

您可以通过覆盖自定义类中的 $queryName 变量来自定义查询的数组键。

class CustomSearchParser extends SearchParser
{
    /**
     * The name of the query in the result array.
     *
     * @var string
     */
    protected $queryName = 'other';
}
$parser = new CustomSearchParser();
$filters = $parser->parse('string to parse');

将产生

[
    'other' => 'string&to&parse'
]

许可

Adaptive View 在 MIT 许可证 (MIT) 下发布。