MacFJA / php-redisearch

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

why use PREFIX with create ? #45

Closed itmeicn closed 2 years ago

itmeicn commented 2 years ago

FT.CREATE idx ON HASH PREFIX 1 blog:post: SCHEMA,PREFIX ? how to use it create?

MacFJA commented 2 years ago

Hello,

You have 2 ways to set prefix(es), with the IndexBuilder, or with the Create command.

IndexBuilder

/** @var \MacFJA\RediSearch\Redis\Client $client */
$builder = (new \MacFJA\RediSearch\IndexBuilder())
    ->setIndex('idx')
    ->setPrefixes(['blog:post:']) // Setter take an array
    ->addTextField('title')
    ->addTextField('body');
$builder->create($client);
/** @var \MacFJA\RediSearch\Redis\Client $client */
$builder = (new \MacFJA\RediSearch\IndexBuilder())
    ->setIndex('idx')
    ->addPrefixes('blog:post:') // "Adder" take one or more arguments as prefixes
    ->addTextField('title')
    ->addTextField('body');
$builder->create($client);

Create

/** @var \MacFJA\RediSearch\Redis\Client $client */
$create = (new \MacFJA\RediSearch\Redis\Command\Create())
   ->setIndex('idx')
   ->setPrefixes('blog:post:')
   ->addTextField('title')
   ->addTextField('body');
$client->execute($create);

I hope that answer your question.

itmeicn commented 2 years ago

thanks,I will. But I need more operations, such as CRUD. or detailed function description

MacFJA commented 2 years ago

This library is a "low-level" implementation. It reflect operation from RediSearch as close as possible.

If you want more high level I can suggest macfja/redisearch-integration (git). This lib can offer method like persist (C and U of CRUD), remove (D of CRUD) and easier way to create a search (R of CRUD) on PHP object/class directly


But with only this library

C and U of CRUD

\MacFJA\RediSearch\Index::addDocumentFromArray will be the function to use

R of CRUD

\MacFJA\RediSearch\Redis\Command\Search will be the object to create and to use with a Client

D of CRUD

You will need to use other command or use \MacFJA\RediSearch\Redis\Client::executeRaw ($myClient->executeRaw('DEL', 'document_hash');)

itmeicn commented 2 years ago

Thanks a lot, I will already use 2.x.