Data-driven applications have data pouring in continually and it is difficult to gather insights or retrieve analytics on such flowing data.


Appacitive keeps realtime computations for sum, average, min, max and count on your integer and decimal properties for connected data.


There are two kinds of aggregate properties you can have in Appacitive.


1. Relation aggregates 


For the purpose of this blog we will run with an example of a restaurant search app.

The restaurant schema has properties like name (string), description (text) and location (geography).

The review schema has properties review (text) and rating (integer between 0 and 5).


The restaurant schema is connected to review schema using a relation called restaurant_review and the user schema is connected to the review schema using a relation called user_review.


So now, users can add reviews for a restaurant and give it a rating based on their experience there.

For the purpose of keeping a count of total number of reviews added for each restaurant, I add a review_count relation aggregate property to the restaurant schema, which will maintain  a count of  reviews added for that restaurant.


Now whenever a new connection is added for relation restaurant_review, the review_count property for that restaurant will get incremented by one.

A get call on a restaurant has a new property for the count of connections of type restaurant_review.



{ "article" : { "$review_count" : { "all" : "2" },
      "__attributes" : {  },
      "__createdby" : "sushant athley",
      "__id" : "35361970997821857",
      "__lastmodifiedby" : "sushant athley",
      "__revision" : "1",
      "__schemaid" : "35357818250986164",
      "__schematype" : "restaurant",
      "__tags" : [ "pizzeria",
          "delivery",
          "organic"
        ],
      "__utcdatecreated" : "2013-08-26T07:43:23.0000000Z",
      "__utclastupdateddate" : "2013-08-26T07:43:23.0000000Z",
      "description" : "The best pizza place in downtown.",
      "location" : "37.774929500000000,-122.419415500000000",
      "name" : "Frank's pizzeria"
    },
  "status" : { "additionalmessages" : [  ],
      "code" : "200",
      "faulttype" : null,
      "message" : "Successful",
      "referenceid" : "846c5177-942a-4f17-aa6d-305471ae17ba",
      "version" : null
    }
}


This way you can add more aggregate properties on a schema for a particular relation. All other such relation aggregate properties will ask you to specify a integer or decimal property in the relation to do computation like sum, min, max and average.



2. Endpoint aggregates 


Now lets say you also want the average rating for every restaurant based on the reviews added for the restaurant. I add an endpoint aggregate property average_rating in the restaurant schema for maintaining the average on the rating (integer between 0 and 5) property in the review schema. This value gets updated every time a new connection is created between review and restaurant.

So now the restaurant objects contain a new property for their average rating.


{ "article" : { "$average_rating" : { "all" : "3.5000" },
      "$review_count" : { "all" : "2" },
      "__attributes" : {  },
      "__createdby" : "sushant athley",
      "__id" : "35361970997821857",
      "__lastmodifiedby" : "sushant athley",
      "__revision" : "1",
      "__schemaid" : "35357818250986164",
      "__schematype" : "restaurant",
      "__tags" : [ "pizzeria",
          "delivery",
          "organic"
        ],
      "__utcdatecreated" : "2013-08-26T07:43:23.0000000Z",
      "__utclastupdateddate" : "2013-08-26T07:43:23.0000000Z",
      "description" : "The best pizza place in downtown.",
      "location" : "37.774929500000000,-122.419415500000000",
      "name" : "Frank's pizzeria"
    },
  "status" : { "additionalmessages" : [  ],
      "code" : "200",
      "faulttype" : null,
      "message" : "Successful",
      "referenceid" : "846c5177-942a-4f17-aa6d-305471ae17ba",
      "version" : null
    }
}


All aggregate properties are prefixed with a '$'. You can perform search queries on aggregate properties by using the prefixed aggregate property name.