vaened/php-search-engine
Composer 安装命令:
composer require vaened/php-search-engine
包简介
A flexible PHP search library that provides essential features for implementing search engines. It includes capabilities for filtering, indexing, and search functionalities. Designed to be easily extensible, it integrates seamlessly with different ORM frameworks to enhance search operations and mana
README 文档
README
The Search Library provides a fundamental and flexible foundation for building search engines in applications. This library is not tied to any specific ORM implementation; rather, it serves as the core upon which more complex search solutions can be built in other libraries that integrate with specific ORMs.
class ProductSearchEngine extends SearchEngine { use Flaggable, Indexable; public function __construct( private readonly IndexRepository $index, private readonly FlagFiltrator $filter ) { $this->apply(Criterias\ProductStatus::active()); } protected function filterer(): Filterer { return $this->filter; } protected function indexer(): indexer { return $this->index; } }
It offers a robust structure for handling common search functionalities, such as custom filters, text searches, and range management. Its modular design ensures efficient integration and the ability to extend to cover more sophisticated search needs as project requirements evolve.
Installation
PHP Search Engine requires PHP 8.1. To get the latest version, simply require the project using Composer:
composer require vaened/php-search-engine
Usage
This library provides a foundational framework for creating search engines. It includes the AbstractSearchEngine class as a base for
custom search implementations and two key traits: Flaggable for filtering based on flags, and Indexable for search operations using
indices. These components work together to offer a flexible and extensible solution for managing search criteria.
Search Engine
The library provides a base class called AbstractSearchEngine which offers fundamental functionality typically extended by other libraries
for specific implementations. However, it's not mandatory to extend this class—custom implementations of SearchEngine can be created from
scratch.
The key feature of AbstractSearchEngine is the apply method:
protected function apply(Scope|Expression|Filter ...$criterias): void { $this->criterias = array_merge($this->criterias, $criterias); }
This method progressively builds search criteria based on applied filters, ultimately constructing the Criteria object.
Criteria
The criteria method is responsible for creating a Criteria instance that encapsulates all the search criteria:
protected function criteria(int $page = 1, ?int $perPage = null): Criteria { $perPage ??= $this->perPage; $offset = ($page - 1) * $perPage; return new Criteria($this->criterias, $this->order, $perPage, $offset); }
This method calculates the pagination offset and initializes the Criteria object with the accumulated search criteria, order, and pagination details.
Flaggable Trait
The Flaggable trait adds the filter method to the SearchEngine, allowing you to apply filters based on specific flags. This method takes a
FlagBag containing a set of flags and applies the corresponding filters. Each flag becomes a search criterion that is added to the engine.
public function filter(FlagBag $flags): self { $this->filterer() ->only($flags) ->each( fn(Scope|Expression|Filter $criteria) => $this->apply($criteria) ); return $this; }
Indexable Trait
The Indexable trait provides the search method, which allows you to perform searches based on a specific index. It uses an index defined
by
the BackedEnum and a queryString to search for the associated criterion in the index repository. If a valid criterion is found, it is
applied to the search engine.
public function search(BackedEnum $index, ?string $queryString): static { if ( null !== $queryString && null !== ( $criteria = $this->indexer()->search($index, $queryString) ) ) { $this->apply($criteria); } return $this; }
Concepts
Filters
Filters represent conditions applied during the search process. These filters are crucial for defining and refining search criteria according to specific requirements. Filters can be simple, such as a direct value check, or more complex, combining multiple conditions to restrict results in detail.
Example of internal definition
interface Filter { public function field(): FilterField; public function operator(): FilterOperator; public function value(): FilterValue; }
Flags
Flags are represented by classes that define binary filters, meaning they do not require additional parameters to determine whether a condition is met or not. These filters act as boolean indicators that apply predefined conditions without needing extra input values. Flags return a collection of filters based on the flag parameter provided.
Example of internal implementation
public function only(FlagBag $flags): ArrayList { return $this->flags() ->only($flags) ->map(static fn(callable $criteria) => apply($criteria)); }
Indexes
Indexes are represented by classes that manage specific search indices. Each index is associated with a search criterion that is applied to a given value. The primary function of an Index is to return a single filter based on the provided index and value. This structure allows for optimized and precise searches using predefined indices.
Example of internal implementation
public function search(BackedEnum $index, string $queryString): null|Scope|Expression|Filter { $criteria = $this->indexes()->get($index); if (null === $criteria) { return null; } return apply($criteria, [$queryString]); }
Usage
Design Principles
In the Search Library, each filter is treated as a separate PHP class with its own specific logic. This approach promotes order and cohesion in the design of the search system. By encapsulating filtering logic in separate classes, the code becomes easier to maintain and extend.
For instance, instead of having scattered filtering functions, classes like PersonId and PersonName are defined, each containing methods
such as equals(value) or startsWith(value). This not only enhances code clarity but also enables more efficient reuse of filters in
different contexts. By keeping filtering logic within dedicated classes, each filter has a single, well-defined responsibility, contributing
to a cleaner and more organized design.
License
This library is licensed under the MIT License. For more information, please see the license file.
vaened/php-search-engine 适用场景与选型建议
vaened/php-search-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128.67k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「search」 「filter」 「query」 「search-engine」 「indexing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaened/php-search-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaened/php-search-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaened/php-search-engine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Nova searchable filter for belongsTo relationships.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Symfony DataGridBundle
Data provider for yii2
统计信息
- 总下载量: 128.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-11