承接 macfja/redisearch 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

macfja/redisearch

Composer 安装命令:

composer require macfja/redisearch

包简介

PHP Client for RediSearch

README 文档

README

MacFJA/redisearch is a PHP Client for RediSearch.

The implemented API is for RediSearch 2.x

Installation

composer require macfja/redisearch

Usage

Get a Redis client

This lib can use several connector for Redis:

You can pick the connector depending on your need.

$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();

// With Predis
$client = $clientFacade->getClient(new \Predis\Client(/* ... */));

// With Phpredis extension
$client = $clientFacade->getClient(new \Redis([/* ... */]));

// With Phpiredis extension
$client = $clientFacade->getClient(phpiredis_connect($host));

// With Amp\Redis
$client = $clientFacade->getClient(new \Amp\Redis\Redis(new RemoteExecutor(Config::fromUri(/* ... */))));

// With Cheprasov
$client = $clientFacade->getClient(new \RedisClient\Client\Version\RedisClient6x0([/* ... */]));

// With Rediska
$client = $clientFacade->getClient(new \Rediska(['servers' => [[/* ... */]]]));

// With Redisent
$client = $clientFacade->getClient(new \redisent\Redis(/* ... */));

// With TinyRedisClient
$client = $clientFacade->getClient(new \TinyRedisClient(/* ... */));

// With Credis
$client = $clientFacade->getClient(new \Credis_Client(/* ... */));

You can add your own implementation, all you need is to implement the interface \MacFJA\RediSearch\Redis\Client and add it to the client facace with:

$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
$clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);

Create a new index

$client = /* ... */;
$builder = new \MacFJA\RediSearch\IndexBuilder();

// Field can be created in advance
$address = (new \MacFJA\RediSearch\Redis\Command\CreateCommand\GeoFieldOption())
    ->setField('address');

$builder
    ->setIndex('person')
    ->addField($address)
    // Or field can be created "inline"
    ->addTextField('lastname', false, null, null, true)
    ->addTextField('firstname')
    ->addNumericField('age')
    ->create($client);

The builder can also be used with withXxx and withAddedXxx instead of setXxx and addXxx. This will give you a new instance of the builder with the configured data.

Add a document

$client = /* ... */;
$index = new \MacFJA\RediSearch\Index('person', $client);
$index->addDocumentFromArray([
    'firstname' => 'Joe',
    'lastname' => 'Doe',
    'age' => 30,
    'address' => '-74.044502,40.689247'
]);

Search

$client = /* ... */;
$search = new \MacFJA\RediSearch\Redis\Command\Search();

$search
    ->setIndex('person')
    ->setQuery('Doe')
    ->setHighlight(['lastname'])
    ->setWithScores();
$results = $client->execute($search);

Create a search query

use MacFJA\RediSearch\Query\Builder\GeoFacet;
use MacFJA\RediSearch\Query\Builder\Negation;
use MacFJA\RediSearch\Query\Builder\NumericFacet;
use MacFJA\RediSearch\Query\Builder\Optional;
use MacFJA\RediSearch\Query\Builder\Word;
use MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption;

$queryBuilder = new \MacFJA\RediSearch\Query\Builder();
$query = $queryBuilder
    ->addElement(NumericFacet::greaterThan('age', 17))
    ->addString('Doe')
    ->addElement(
        new Negation(
            new GeoFacet(['address'], -74.044502, 40.589247, 40, GeoFilterOption::UNIT_KILOMETERS)
        )
    )
    ->addElement(new Optional(new Word('John')))
    ->render();

// The value of $query is:
// @age:[(17 +inf] Doe -@address:[-74.044502 40.589247 40.000000 km] ~John

Pipeline

use MacFJA\RediSearch\Redis\Command\Aggregate;
use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
use MacFJA\RediSearch\Redis\Command\Search;
use MacFJA\RediSearch\Redis\Command\SugGet;

$client = /* ... */;

$query = '@age:[(17 +inf] %john%';
$search = new Search();
$search->setIndex('people')
    ->setQuery($query);

$stats = new Aggregate();
$stats->setIndex('people')
    ->setQuery($query)
    ->addGroupBy(new GroupByOption([], [
        ReduceOption::average('age', 'avg'),
        ReduceOption::maximum('age', 'oldest')
    ]));

$aggregate = new Aggregate();
$aggregate->setIndex('people')
    ->setQuery($query)
    ->addGroupBy(new GroupByOption(['lastname'], [ReduceOption::count('count')]));

$suggestion = new SugGet();
$suggestion->setDictionary('names')
    ->setPrefix('john')
    ->setFuzzy();

$result = $client->pipeline($search, $stats, $aggregate, $suggestion);

// $result[0] is the search result
// $result[1] is the first aggregation result
// $result[2] is the second aggregation result
// $result[3] is the suggestion result

Use Predis (v1.x) shorthand syntax

$client = new \Predis\Client(/* ... */);
\MacFJA\RediSearch\Redis\Initializer::registerCommandsPredis($client->getProfile());

$client->ftsearch('people', '@age:[(17 +inf] %john%');
// But you will have raw Redis output.

Use Rediska shorthand syntax

$client = new \Rediska(/* ... */);
\MacFJA\RediSearch\Redis\Initializer::registerCommandsRediska();

$client->ftsearch('people', '@age:[(17 +inf] %john%');
// But you will have raw Redis output.

Similar projects

Contributing

You can contribute to the library. To do so, you have Github issues to:

  • ask your question
  • request any change (typo, bad code, new feature etc.)
  • and much more...

You also have PR to:

  • suggest a correction
  • suggest a new feature
  • and much more...

See CONTRIBUTING for more information.

License

The MIT License (MIT). Please see License File for more information.

macfja/redisearch 适用场景与选型建议

macfja/redisearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 171.06k 次下载、GitHub Stars 达 67, 最近一次更新时间为 2020 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 macfja/redisearch 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 171.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 67
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 67
  • Watchers: 2
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-05