MacFJA / php-redisearch

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

Facing issue in usage of 'withApplies' in aggregate #21

Closed mahendran-kcube closed 2 years ago

mahendran-kcube commented 2 years ago

Aggregate Raw query

FT.AGGREGATE indexName "@geofield:[-83.81373 34.3755452645611 20 mi]" LOAD 1 @geofield APPLY "@cc_sum * ( 20 - (geodistance(@geofield, -83.81373, 34.3755452645611)) / 20)" as ccSumCalcValue APPLY 1 as test GROUPBY 1 @test REDUCE SUM 1 @ccSumCalcValue as weightedSum

Aggregate query using the "MacFJA/php-redisearch"

$query = '@geofield:[-83.81373 34.3755452645611 20 mi]';
 $resultAggregate = $aggregate
    ->withIndexName('indexName ')
    ->withLoad(['geofield', 'cc_sum', 'if_fat'])
    ->withQuery($query)
    ->withApplies(['@cc_sum * ( 20 - (geodistance(@geofield, -83.81373, 34.3755452645611)) / 20)' => 'ccSumCalcValue'])
    ->addGroupBy(['ccSumCalcValue'], [])
    ->execute();

I am not able to do the following command from raw aggregate query using this library

APPLY 1 as test GROUPBY 1 @test REDUCE SUM 1 @ccSumCalcValue as weightedSum

Can someone help me out to do this command using this library

MacFJA commented 2 years ago

Here the code to have the same output as your raw query (order of instructions are not in the same exact order):

$aggregate
    ->withIndexName('indexName ')
    ->withLoad(['@geofield'])
    ->withQuery($query)
    ->withApplies(['@cc_sum * ( 20 - (geodistance(@geofield, -83.81373, 34.3755452645611)) / 20)' => 'ccSumCalcValue', '1 ' => 'test'])
    ->addGroupBy(['test'], [MacFJA\RediSearch\Aggregate\Reducer::sum('ccSumCalcValue', 'weightedSum')]);

There is indeed a small issue with the APPLY 1 as test as the value of the applier (1) is not consider as a string, but with an extra space, the value is forced to be a string