Alias for query fields containing formulas in ClusterPount Aggregate Requests

50 Views Asked by At

I am executing ClusterPoint aggregation query from my PHP application using php_api. Here is a code sample I'm using:

$query = "<query><Make>subaru</Make></query>
          <docs>20</docs>
          <offset>0</offset>
          <aggregate>SUM(Count), Type, Fuel GROUP BY Type, Fuel </aggregate>";

$request = new CPS_Request('search');
$request -> setExtraXmlParam($query);
$response = $cpsConn->sendRequest($request);

$aggregates = $response -> getAggregate(DOC_TYPE_ARRAY);
$type_fuel = array_pop($aggregates);

var_dump($type_fuel[0]);

ClusterPoint API returns data as Array as requested and computes "key" by normalizing the formula SUM(Count):

array (size=3)
  'SUM_Count' => string '2' (length=1)
  'Type' => string 'Lorry' (length=5)
  'Fuel' => string 'Petrol & gas' (length=12)

As I change query around, the "SUM_Count" often becomes something else, so I am looking for a way to "alias" the formula result and to have a more consistent key for the value of "2".

1

There are 1 best solutions below

0
On BEST ANSWER

You can make alias like you do in SQL using "as" construction:

<aggregate>SUM(Count) as sum_c, Type, Fuel GROUP BY Type, Fuel </aggregate>