MacFJA / php-redisearch

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

How to make Geo field sortable in MacFJA / php-redisearch library? #20

Closed mahendran-kcube closed 2 years ago

mahendran-kcube commented 2 years ago

How to make Geo field sortable in MacFJA / php-redisearch library?

MacFJA commented 2 years ago

With the version 2.0.0 (that was not possible with version 1.x of the lib):

use MacFJA\RediSearch\Index;
use MacFJA\RediSearch\IndexBuilder;
use MacFJA\RediSearch\Redis\Client\PredisClient;
use MacFJA\RediSearch\Redis\Command\CreateCommand\GeoFieldOption;
use MacFJA\RediSearch\Redis\Command\Search;
use Predis\Client;

$client = PredisClient::make(new Client(['scheme' => 'tcp', 'host' => 'localhost', 'port' => '6379', 'db' => 0]));
// Or whatever client you need/have
// Or $client = (new \MacFJA\RediSearch\Redis\Client\ClientFacade)->getClient($redisInstance);

$indexBuilder = new IndexBuilder();
$indexBuilder
    ->setIndex('place')
    ->addPrefixes('p-')
    ->addField(
        (new GeoFieldOption())
            ->setField('gps')
            ->setSortable(true)
    );
$indexBuilder->create($client);

$index = new Index('place', $client);
$index->addDocumentFromArray(['gps' => '-0.1277583,51.5073509'], 'p-london');
$index->addDocumentFromArray(['gps' => '2.3522219,48.856614'],  'p-paris');
$index->addDocumentFromArray(['gps' => '11.5819806,48.1351253'],  'p-munich');
$index->addDocumentFromArray(['gps' => '-74.005941,40.712784'],  'p-nyc');
$index->addDocumentFromArray(['gps' => '139.731992,35.709026'],  'p-tokyo');

$result = $client->execute(
    (new Search())
        ->setIndex('place')
        ->setQuery('*')
        ->setSortBy('gps', 'DESC')
);

var_dump($result);
mahendran-kcube commented 2 years ago

I am not able to install Version 2.0.0 of this library using the following command? composer require macfja/redisearch

MacFJA commented 2 years ago

The package is available on Packagist: https://packagist.org/packages/macfja/redisearch#2.0.0

You need to have: