Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addDocumentFromArray is error,please help me #46

Closed
itmeicn opened this issue Jul 21, 2022 · 7 comments
Closed

addDocumentFromArray is error,please help me #46

itmeicn opened this issue Jul 21, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@itmeicn
Copy link

itmeicn commented Jul 21, 2022

       $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
Copy link
Author

itmeicn commented Jul 21, 2022

$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 MacFJA added the bug Something isn't working label Jul 21, 2022
@MacFJA
Copy link
Owner

MacFJA commented Jul 21, 2022

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
Copy link
Author

itmeicn commented Jul 22, 2022

$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
Copy link
Author

itmeicn commented Jul 23, 2022

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
Copy link
Owner

MacFJA commented Jul 23, 2022

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
Copy link
Author

itmeicn commented Jul 27, 2022

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
Copy link
Author

itmeicn commented Aug 12, 2022

ths

@itmeicn itmeicn closed this as completed Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants