php调用redisearch

2020年10月7日00:30:37

github:https://github.com/RediSearch/RediSearch

操作手册 https://oss.redislabs.com/redisearch/Quick_Start/

经过一段时间的折腾,终于算是可以实现php调用redisearch,一下几点请注意:

1,官方推荐的 redisearch-php,已经过时被弃用了 ,如果你使用的是旧版本的redis 3.x和php比如5.x到7.0.x应该可以

可以自行测试,我个人的测试结果是放弃

测试环境的redis 6.0.5 php 7.3.4,laravel8

https://github.com/ethanhann/redisearch-php

目前支持到redis 6.0的php redis客户端只有

https://github.com/cheprasov/php-redis-client

RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 6.0

官方明确说明支持6.0.x

composer require cheprasov/php-redis-client
use RedisClientClientFactory;

$Redis = ClientFactory::create([
                    'server' => '192.168.3.15:6379', // or 'unix:///tmp/redis.sock'
                    'timeout' => 2,
                    'version' => '6.0.5'
        ]);

        echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL;

        $Redis->executeRawString('FT.CREATE myIdx ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT');
        $Redis->executeRawString('hset doc:1 title "hello world" body "lorem ipsum" url "http://redis.io"');
        $rr = $Redis->executeRawString('FT.SEARCH myIdx "hello world"');
        print_r($rr);

执行结果

RedisClient: 6.0
Array
(
    [0] => 2
    [1] => doc:1
    [2] => Array
        (
            [0] => title
            [1] => hello world
            [2] => body
            [3] => lorem ipsum
            [4] => url
            [5] => http://redis.io
        )

    [3] => doc1
    [4] => Array
        (
            [0] => title
            [1] => hello world
            [2] => body
            [3] => lorem ipsum
            [4] => url
            [5] => http://redis.io
        )

)

其他扩展比如phpredis也应该可以,这个是php的扩展,但是我闲麻烦,因为的我php版本太新,可能还没那么快支持到最新版本

目前mysql同步到redis还是有工具的,但是同步到RediSearch只能自己写脚本了,可能也是RediSearch的相对于elasticsearch的一个小缺点吧

原文地址:https://www.cnblogs.com/zx-admin/p/13776104.html