MacFJA / php-redisearch

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

addDocumentFromArray is error,please help me #46

Closed itmeicn closed 2 years ago

itmeicn commented 2 years ago
       $index=new \MacFJA\RediSearch\Index($this->name,$this->searchConnect);
            //dump($index);
            $res=$index->addDocumentFromArray([
                'zid'=>'553',
                'bank_number'=>'666',
                'today'=>'2022-07-21',
                'shou'=>0,
                'zhi'=>0,
                'bank_balance'=>0
            ],$this->name.':'.'553');
              echo $res;

is error: assert(is_array($rawDefinitions));

    /**
     * @return null|array<string,mixed>|mixed
     */
    public function getIndexDefinition(?string $key = null)
    {
        $rawDefinitions = self::getValue($this->rawLines, 'index_definition');
        assert(is_array($rawDefinitions));
        $data = self::getPairs($rawDefinitions);
        if (!is_string($key)) {
            return $data;
        }

        return $data[$key] ?? null;
    }

  -rawLines: array:48 [▼
    0 => true
    1 => true
    2 => true
    3 => []
    4 => true
    5 => array:6 [▶]
    6 => true
    7 => array:6 [▶]
    8 => true
    9 => "0"
    10 => true
    11 => "0"
    12 => true
    13 => "0"
    14 => true
    15 => "0"
    16 => true
    17 => "0"
    18 => true
    19 => "0"
    20 => true
    21 => "0"
    22 => true
    23 => "0"
    24 => true
    25 => "0"
    26 => true
    27 => "0"
    28 => true
    29 => "0"
    30 => true
    31 => "-nan"
    32 => true
    33 => "-nan"
    34 => true
    35 => "-nan"
    36 => true
    37 => "-nan"
    38 => true
    39 => "0"
    40 => true
    41 => "0"
    42 => true
    43 => "1"
    44 => true
    45 => array:14 [▶]
    46 => true
    47 => array:8 [▶]
itmeicn commented 2 years ago
$builder=new \MacFJA\RediSearch\IndexBuilder();
    $res=$builder->setIndex($this->name)->addPrefixes('finance_fun_daylog:')
        ->addTagField('zid',null,true)
        ->addTextField('bank_number',false,2,null,true,false)
        ->addTextField('today',false,2,null,true,false)
        ->addNumericField('shou')
        ->addNumericField('zhi')
        ->addNumericField('bank_balance')
        ->create($this->searchConnect);
    dump($res);//1

create index is ok

MacFJA commented 2 years ago

Can you dump the content of $rawDefinitions ?

/**
     * @return null|array<string,mixed>|mixed
     */
    public function getIndexDefinition(?string $key = null)
    {
        $rawDefinitions = self::getValue($this->rawLines, 'index_definition');
        dd($rawDefinitions); // Add this line
        assert(is_array($rawDefinitions));
        $data = self::getPairs($rawDefinitions);
        if (!is_string($key)) {
            return $data;
        }

        return $data[$key] ?? null;
    }
itmeicn commented 2 years ago

$rawDefinitions = self::getValue($this->rawLines, 'index_definition'); dump($rawDefinitions);exit(); is result:null. $a=$cacheRedis->rawCommand('ft.info','finance_fun_daylog'); dump($a);//The result is the same as the -rawLines: value,I use the open source RediSearch2.4.9. I use phpredis.

itmeicn commented 2 years ago

today,It's ok useing old version1.4, I use the new version 2.1.2 to work with predis1.1.10,But 2.1.2 and phpredis5.3.3 have errors. Common use of php8.0.

MacFJA commented 2 years ago

I see in the result of your FT.INFO request that all even value is equal to true instead of the name of the data.

So it's impossible to find the right data (even result lines are the data key, and odd result lines are the data value)


I can't reproduce yet your case.


But as a work around for now you can just call redis HSET:

// instead of
/*
$res=$index->addDocumentFromArray([
                'zid'=>'553',
                'bank_number'=>'666',
                'today'=>'2022-07-21',
                'shou'=>0,
                'zhi'=>0,
                'bank_balance'=>0
            ],$this->name.':'.'553');
*/
// Do this
$this->searchConnect->executeRaw(
    'HSET',
    $this->name . ':' . '553',
    'zid', '553',
    'bank_number', '666',
    'today', '2022-07-21',
    'shou', 0,
    'zhi', 0,
    'bank_balance', 0
);

It's pretty much what do the addDocumentFromArray, except that addDocumentFromArray try to be smart and add document hash with the good prefix if you don't provide it yourself. Which is not your case

itmeicn commented 2 years ago

vendor/macfja/redisearch/src/Redis/Client/PhpredisClient.php

    public function execute(Command $command)
    {
        $arguments = $command->getArguments();
        if (0 === count($arguments)) {
            /** @psalm-suppress TooFewArguments */
            $rawResponse = $this->redis->rawCommand($command->getId());
        } else { 
           // add line start 
            if ($command->getId()=='FT.INFO'){
                $this->redis->setOption(\Redis::OPT_REPLY_LITERAL, true); //add this line solve the problems
            }
           // add line end,ft.info can get the correct value
            $rawResponse = $this->redis->rawCommand($command->getId(), ...$arguments);
        }

        return $command->parseResponse($rawResponse);
    }
itmeicn commented 2 years ago

ths