MacFJA / php-redisearch

PHP Client for RediSearch
MIT License
66 stars 9 forks source link

How to manipulate CURD using JSON? #51

Closed itmeicn closed 2 years ago

itmeicn commented 2 years ago
  $builder=new \MacFJA\RediSearch\IndexBuilder();
            $res=$builder->setIndex($this->name.'Idx')->setStructure('JSON')->addPrefixes($this->name.':')
                ->setDefaultLanguage('chinese')
                ->addJSONTagField('id','id',null,true)
                ->addJSONTextField('userid','userid',false,10,null,true)
                ->create($this->clientPhprearch);

is error: Missing command option: fields

MacFJA commented 2 years ago

Hello,

Your issue is that by default the IndexBuilder consider that the RediSearch version is 2.0.0. But JSON schema only exist in RediSearch 2.2.0. You can pass the version of RediSearch as the second parameter of the create method:

$builder = new \MacFJA\RediSearch\IndexBuilder();
$res = $builder->setIndex($this->name . 'Idx')->setStructure('JSON')->addPrefixes($this->name . ':')
    ->setDefaultLanguage('chinese')
    ->addJSONTagField('id', 'id', null, true)
    ->addJSONTextField('userid', 'userid', false, 10, null, true)
    ->create($this->clientPhprearch, '2.2.0');

or

$builder=new \MacFJA\RediSearch\IndexBuilder();
$res=$builder->setIndex($this->name . 'Idx')->setStructure('JSON')->addPrefixes($this->name . ':')
    ->setDefaultLanguage('chinese')
    ->addJSONTagField('id', 'id', null, true)
    ->addJSONTextField('userid', 'userid', false, 10, null, true)
    ->create($this->clientPhprearch, \MacFJA\RediSearch\Redis\Initializer::getRediSearchVersion($this->clientPhprearch));
itmeicn commented 2 years ago
  $builder=new \MacFJA\RediSearch\IndexBuilder();
            $res=$builder->setIndex($this->name.'Idx')->setStructure('JSON')->addPrefixes($this->name.':')
                ->setDefaultLanguage('chinese')
                ->addJSONNumericField('$.id','id',true)
                ->addJSONTextField('$.userid','userid',false,10,null,true)
                ->addJSONTextField('$.username','username',false,8,null,true)
                ->addJSONNumericField('$.status','status',true)
                ->addJSONTextField('$.login_ip','login_ip')
                ->addJSONNumericField('$.login_time','login_time')
                ->addJSONTextField('$.token','token',false,6)
                ->addJSONTextField('$.mobile','mobile')
                ->addJSONTextField('$.create_time','create_time',false,null,null,true)
                 ->addJSONTextField('$.coid','coid',false,null,null,true)
                 ->addJSONTextField('$.shortcode','shortcode',false,4,null,true)
                 ->addJSONTextField('$.pushid','pushid')
                 ->addJSONNumericField('$.','kaoqin_remaind')
                 ->addJSONTextField('$.telephone_number','telephone_number')
                 ->addJSONNumericField('$.main_depart','main_depart',true)
                ->create($this->clientPhprearch,\MacFJA\RediSearch\Redis\Initializer::getRediSearchVersion($this->clientPhprearch));

Maybe add "$." to the field to execute correctly

MacFJA commented 2 years ago

Yes, I forgot to indicate this. The first parameter of addJSON* methods, must be valid JSON Path

itmeicn commented 2 years ago

ths

vgross commented 1 year ago

Maybe the good idea is to add this to the examples in the documentation... I've also been struggling with that issue for the last 24h :-/

Otherwise, thank you for this fantastic library. Everything else works very well!