定制 kennedytedesco/meilisearch-search-filter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kennedytedesco/meilisearch-search-filter

Composer 安装命令:

composer require kennedytedesco/meilisearch-search-filter

包简介

A fluent and intuitive way to construct filters for Meilisearch queries.

README 文档

README

Meilisearch Search Filter

Build Status PHPStan License

This zero-dependency library provides a fluent and intuitive way to construct filters for Meilisearch queries. It simplifies the process of building filters by offering a chainable API.

Installation

Minimum requirements: PHP 8.1 or higher.

To use this library in your project, you can install it using Composer:

composer require kennedytedesco/meilisearch-search-filter "^1.0"

Usage

You can learn how Meilisearch filters work by reading the official documentation.

You should also check out the Meilisearch PHP SDK.

Here are some examples that demonstrate how to build filters using this library:

use Meilisearch\Client;
use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter;

$client = new Client('http://127.0.0.1:7700', 'masterKey');

$filter = SearchFilter::new()
    ->where(function (SearchFilter $filter) {
        $filter->whereGreaterThan('rating.critics', 80)
            ->whereGreaterThanOrEqual('rating.users', 70);
    })
    ->whereIn('genres', ['Horror', 'Thriller']);

$index = $client->index('movies');

$results = $index->search('wonder', [
    // (rating.critics > 80 AND rating.users >= 70) AND genres IN ["Horror", "Thriller"]
    'filter' => $filter->build(),
]);

You can also use alias methods when constructing filters:

use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter;

$filter = SearchFilter::new()
    ->where(function (SearchFilter $filter) {
        $filter->whereGt('rating.critics', 80)
            ->whereGte('rating.users', 70);
    })
    ->orWhereIn('genres', ['Horror', 'Thriller']);

echo $filter->build(); 

// Output: (rating.critics > 80 AND rating.users >= 70) OR genres IN ["Horror", "Thriller"]

Alternatively, you can use the where(...) method and pass the operator as the second argument:

use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter;

$filter = SearchFilter::new()
    ->where(function (SearchFilter $filter) {
        $filter->where('rating.critics', '>', 80)
            ->where('rating.users', '>=', 70);
    })
    ->orWhereIn('genres', ['Horror', 'Thriller']);

echo $filter->build(); 

// Output: (rating.critics > 80 AND rating.users >= 70) OR genres IN ["Horror", "Thriller"]

Using Between

The TO operator is equivalent to >= AND <=.

For more details, see this link.

use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter;

$filter = SearchFilter::new()
    ->whereBetween('rating.critics', 80, 90);

echo $filter->build(); 

// Output: rating.critics 80 TO 90

If you want your results to only include "comedy" and "horror" movies released after March 1995, it's mandatory to group the OR conditions:

use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter;

$filter = SearchFilter::new()
    ->where(function (SearchFilter $filter) {
        $filter->where('genres', 'horror')
            ->orWhere('genres', 'comedy');
    })
    ->where('release_date', '>', '795484800');

echo $filter->build();

// Output: (genres = "horror" OR genres = "comedy") AND release_date > 795484800

So, when you provide a closure to the where() or orWhere() methods, a fresh SearchFilter instance is passed to the closure as the first argument. This lets you craft nested filters within parentheses.

Using when()

The when() method allows you to conditionally add filters to the query. For example:

use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter;

$filter = SearchFilter::new()
    ->when($request->filled('type'), function (SearchFilter $filter) use($request) {
        $filter->where('type', $request->get('type'));
    })
    ->where('release_date', '>', '795484800');

All available filter methods

Method Description
where(...$args) Adds a filter condition using the AND logical operator.
whereGreaterThan(...$args) Adds a greater than comparison filter condition.
whereGt(...$args) Alias for whereGreaterThan.
whereGreaterThanOrEqual(...$args) Adds a greater than or equal to comparison filter condition.
whereGte(...$args) Alias for whereGreaterThanOrEqual.
whereLessThan(...$args) Adds a less than comparison filter condition.
whereLt(...$args) Alias for whereLessThan.
whereLessThanOrEqual(...$args) Adds a less than or equal to comparison filter condition.
whereLte(...$args) Alias for whereLessThanOrEqual.
whereNot(...$args) Adds a not equal comparison filter condition.
whereIn(...$args) Adds a filter to check if the attribute value is in a given array of values.
whereNotIn(...$args) Adds a filter to check if the attribute value is not in a given array of values.
whereExists(...$args) Adds a filter to check if the attribute value exists.
whereNotExists(...$args) Adds a filter to check if the attribute value does not exist.
orWhere(...$args) Adds a filter condition using the OR logical operator.
orWhereGreaterThan(...$args) Adds a greater than comparison filter condition using OR logical operator.
orWhereGt(...$args) Alias for orWhereGreaterThan.
orWhereGreaterThanOrEqual(...$args) Adds a greater than or equal to comparison filter condition using OR logical operator.
orWhereGte(...$args) Alias for orWhereGreaterThanOrEqual.
orWhereLessThan(...$args) Adds a less than comparison filter condition using OR logical operator.
orWhereLt(...$args) Alias for orWhereLessThan.
orWhereLessThanOrEqual(...$args) Adds a less than or equal to comparison filter condition using OR logical operator.
orWhereLte(...$args) Alias for orWhereLessThanOrEqual.
orWhereNot(...$args) Adds a not equal comparison filter condition using OR logical operator.
orWhereIn(...$args) Adds a filter to check if the attribute value is in a given array of values using OR logical operator.
orWhereNotIn(...$args) Adds a filter to check if the attribute value is not in a given array of values using OR logical operator.
orWhereExists(...$args) Adds a filter to check if the attribute value exists using OR logical operator.
orWhereNotExists(...$args) Adds a filter to check if the attribute value does not exist using OR logical operator.
whereBetween(...$args) Adds a filter to check if the attribute value is between two values.
whereNotBetween(...$args) Adds a filter to check if the attribute value is not between two values.
orWhereBetween(...$args) Adds a filter to check if the attribute value is between two values using OR logical operator.
orWhereNotBetween(...$args) Adds a filter to check if the attribute value is not between two values using OR logical operator.
whereEmpty(...$args) Adds a filter to check if the attribute value is empty.
whereNotEmpty(...$args) Adds a filter to check if the attribute value is not empty.
orWhereEmpty(...$args) Adds a filter to check if the attribute value is empty using OR logical operator.
orWhereNotEmpty(...$args) Adds a filter to check if the attribute value is not empty using OR logical operator.
whereNull(...$args) Adds a filter to check if the attribute value is null.
whereNotNull(...$args) Adds a filter to check if the attribute value is not null.
orWhereNull(...$args) Adds a filter to check if the attribute value is null using OR logical operator.
orWhereNotNull(...$args) Adds a filter to check if the attribute value is not null using OR logical operator.

Contributing

If you'd like to contribute to this project, feel free to submit pull requests or open issues on the GitHub repository.

kennedytedesco/meilisearch-search-filter 适用场景与选型建议

kennedytedesco/meilisearch-search-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.58k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「api」 「client」 「search」 「filter」 「filters」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kennedytedesco/meilisearch-search-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kennedytedesco/meilisearch-search-filter 我们能提供哪些服务?
定制开发 / 二次开发

基于 kennedytedesco/meilisearch-search-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-30