Showing posts with label aggregations. Show all posts
Showing posts with label aggregations. Show all posts

Friday, June 17, 2016

trying out aggregations on mongodb

Today, we will again look into mongodb but on the specific topic of aggregation.

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.

Let's start with a sample aggregation from the zip code.Importing 29353 objects within second. Blazing fast, maybe because I'm using ssd, heh.

 user@localhost:~$ mongoimport --collection zipcodes < ~/Desktop/zips.json   
 connected to: 127.0.0.1  
 Tue Mar 8 20:21:37.950 check 9 29353  
 Tue Mar 8 20:21:38.099 imported 29353 objects  
   
 > db.zipcodes.aggregate( [ { $group: { _id: "$state", totalPop: { $sum: "$pop" } } }, { $match: { totalPop: { $gte: 10*1000*1000 } } } ] )  
 {  
    "result" : [  
       {  
          "_id" : "IL",  
          "totalPop" : 11427576  
       },  
       {  
          "_id" : "OH",  
          "totalPop" : 10846517  
       },  
       {  
          "_id" : "FL",  
          "totalPop" : 12686644  
       },  
       {  
          "_id" : "NY",  
          "totalPop" : 17990402  
       },  
       {  
          "_id" : "PA",  
          "totalPop" : 11881643  
       },  
       {  
          "_id" : "TX",  
          "totalPop" : 16984601  
       },  
       {  
          "_id" : "CA",  
          "totalPop" : 29754890  
       }  
    ],  
    "ok" : 1  
 }  
 > db.zipcodes.aggregate( [ { $group: { _id: { state: "$state", city: "$city" }, pop: { $sum: "$pop" } } }, { $group: { _id: "$_id.state", avgCityPop: { $avg: "$pop" } } } ] )  
 {  
    "result" : [  
       {  
          "_id" : "NH",  
          "avgCityPop" : 5232.320754716981  
       },  
       {  
          "_id" : "MA",  
          "avgCityPop" : 14855.37037037037  
       },  
       {  
          "_id" : "ME",  
          "avgCityPop" : 3006.4901960784314  
       },  
       {  
          "_id" : "NY",  
          "avgCityPop" : 13131.680291970803  
       },  
       {  
          "_id" : "VT",  
          "avgCityPop" : 2315.8765432098767  
       },  
       {  
          "_id" : "PA",  
          "avgCityPop" : 8679.067202337472  
       },  
       {  
          "_id" : "DE",  
          "avgCityPop" : 14481.91304347826  
       },  
       {  
          "_id" : "DC",  
          "avgCityPop" : 303450  
       },  
       {  
          "_id" : "VA",  
          "avgCityPop" : 8526.177931034483  
       },  
       {  
          "_id" : "SC",  
          "avgCityPop" : 11139.626198083068  
       },  
       {  
          "_id" : "FL",  
          "avgCityPop" : 27400.958963282937  
       },  
       {  
          "_id" : "AL",  
          "avgCityPop" : 7907.2152641878665  
       },  
       {  
          "_id" : "NJ",  
          "avgCityPop" : 15775.89387755102  
       },  
       {  
          "_id" : "WV",  
          "avgCityPop" : 2771.4775888717154  
       },  
       {  
          "_id" : "TN",  
          "avgCityPop" : 9656.350495049504  
       },  
       {  
          "_id" : "OH",  
          "avgCityPop" : 12700.839578454332  
       },  
       {  
          "_id" : "MD",  
          "avgCityPop" : 12615.775725593667  
       },  
       {  
          "_id" : "MN",  
          "avgCityPop" : 5372.21375921376  
       },  
       {  
          "_id" : "ND",  
          "avgCityPop" : 1645.0309278350514  
       },  
       {  
          "_id" : "NC",  
          "avgCityPop" : 10622.815705128205  
       },  
       {  
          "_id" : "MT",  
          "avgCityPop" : 2593.987012987013  
       },  
       {  
          "_id" : "IL",  
          "avgCityPop" : 9954.334494773519  
       },  
       {  
          "_id" : "MO",  
          "avgCityPop" : 5672.195338512764  
       },  
       {  
          "_id" : "KS",  
          "avgCityPop" : 3819.884259259259  
       },  
       {  
          "_id" : "LA",  
          "avgCityPop" : 10465.496277915632  
       },  
       {  
          "_id" : "AR",  
          "avgCityPop" : 4175.355239786856  
       },  
       {  
          "_id" : "CO",  
          "avgCityPop" : 9981.075757575758  
       },  
       {  
          "_id" : "IN",  
          "avgCityPop" : 9271.130434782608  
       },  
       {  
          "_id" : "KY",  
          "avgCityPop" : 4767.164721141375  
       },  
       {  
          "_id" : "OK",  
          "avgCityPop" : 6155.743639921722  
       },  
       {  
          "_id" : "ID",  
          "avgCityPop" : 4320.811158798283  
       },  
       {  
          "_id" : "WY",  
          "avgCityPop" : 3384.5373134328356  
       },  
       {  
          "_id" : "UT",  
          "avgCityPop" : 9518.508287292818  
       },  
       {  
          "_id" : "NV",  
          "avgCityPop" : 18209.590909090908  
       },  
       {  
          "_id" : "NE",  
          "avgCityPop" : 3034.882692307692  
       },  
       {  
          "_id" : "RI",  
          "avgCityPop" : 19292.653846153848  
       },  
       {  
          "_id" : "NM",  
          "avgCityPop" : 5872.360465116279  
       },  
       {  
          "_id" : "CA",  
          "avgCityPop" : 27756.42723880597  
       },  
       {  
          "_id" : "AZ",  
          "avgCityPop" : 20591.16853932584  
       },  
       {  
          "_id" : "HI",  
          "avgCityPop" : 15831.842857142858  
       },  
       {  
          "_id" : "IA",  
          "avgCityPop" : 3123.0821147356583  
       },  
       {  
          "_id" : "MS",  
          "avgCityPop" : 7524.023391812865  
       },  
       {  
          "_id" : "WI",  
          "avgCityPop" : 7323.00748502994  
       },  
       {  
          "_id" : "TX",  
          "avgCityPop" : 13775.02108678021  
       },  
       {  
          "_id" : "SD",  
          "avgCityPop" : 1839.6746031746031  
       },  
       {  
          "_id" : "MI",  
          "avgCityPop" : 12087.512353706112  
       },  
       {  
          "_id" : "GA",  
          "avgCityPop" : 11547.62210338681  
       },  
       {  
          "_id" : "OR",  
          "avgCityPop" : 8262.561046511628  
       },  
       {  
          "_id" : "CT",  
          "avgCityPop" : 14674.625  
       },  
       {  
          "_id" : "WA",  
          "avgCityPop" : 12258.670025188916  
       },  
       {  
          "_id" : "AK",  
          "avgCityPop" : 2976.4918032786886  
       }  
    ],  
    "ok" : 1  
 }  
 > db.zipcodes.aggregate( [ { $group: { _id: { state: "$state", city: "$city" }, pop: { $sum: "$pop" } } }, { $sort: { pop: 1 } }, { $group: { _id : "$_id.state", biggestCity: { $last: "$_id.city" }, biggestPop:  { $last: "$pop" }, smallestCity: { $first: "$_id.city" }, smallestPop: { $first: "$pop" } } }, { $project: { _id: 0, state: "$_id", biggestCity: { name: "$biggestCity", pop: "$biggestPop" }, smallestCity: { name: "$smallestCity", pop: "$smallestPop" } } } ] )  
 {  
    "result" : [  
       {  
          "biggestCity" : {  
             "name" : "NEWARK",  
             "pop" : 111674  
          },  
          "smallestCity" : {  
             "name" : "BETHEL",  
             "pop" : 108  
          },  
          "state" : "DE"  
       },  
       {  
          "biggestCity" : {  
             "name" : "SAINT LOUIS",  
             "pop" : 397802  
          },  
          "smallestCity" : {  
             "name" : "BENDAVIS",  
             "pop" : 44  
          },  
          "state" : "MO"  
       },  
       {  
          "biggestCity" : {  
             "name" : "CHICAGO",  
             "pop" : 2452177  
          },  
          "smallestCity" : {  
             "name" : "ANCONA",  
             "pop" : 38  
          },  
          "state" : "IL"  
       },  
       {  
          "biggestCity" : {  
             "name" : "CLEVELAND",  
             "pop" : 536759  
          },  
          "smallestCity" : {  
             "name" : "ISLE SAINT GEORG",  
             "pop" : 38  
          },  
          "state" : "OH"  
       },  
       {  
          "biggestCity" : {  
             "name" : "MANCHESTER",  
             "pop" : 106452  
          },  
          "smallestCity" : {  
             "name" : "WEST NOTTINGHAM",  
             "pop" : 27  
          },  
          "state" : "NH"  
       },  
       {  
          "biggestCity" : {  
             "name" : "WASHINGTON",  
             "pop" : 606879  
          },  
          "smallestCity" : {  
             "name" : "PENTAGON",  
             "pop" : 21  
          },  
          "state" : "DC"  
       },  
       {  
          "biggestCity" : {  
             "name" : "GRAND FORKS",  
             "pop" : 59527  
          },  
          "smallestCity" : {  
             "name" : "TROTTERS",  
             "pop" : 12  
          },  
          "state" : "ND"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BALTIMORE",  
             "pop" : 733081  
          },  
          "smallestCity" : {  
             "name" : "ANNAPOLIS JUNCTI",  
             "pop" : 32  
          },  
          "state" : "MD"  
       },  
       {  
          "biggestCity" : {  
             "name" : "MINNEAPOLIS",  
             "pop" : 344719  
          },  
          "smallestCity" : {  
             "name" : "JOHNSON",  
             "pop" : 12  
          },  
          "state" : "MN"  
       },  
       {  
          "biggestCity" : {  
             "name" : "SALT LAKE CITY",  
             "pop" : 186346  
          },  
          "smallestCity" : {  
             "name" : "MODENA",  
             "pop" : 9  
          },  
          "state" : "UT"  
       },  
       {  
          "biggestCity" : {  
             "name" : "CHEYENNE",  
             "pop" : 70185  
          },  
          "smallestCity" : {  
             "name" : "LOST SPRINGS",  
             "pop" : 6  
          },  
          "state" : "WY"  
       },  
       {  
          "biggestCity" : {  
             "name" : "PHOENIX",  
             "pop" : 890853  
          },  
          "smallestCity" : {  
             "name" : "HUALAPAI",  
             "pop" : 2  
          },  
          "state" : "AZ"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BRIDGEPORT",  
             "pop" : 141638  
          },  
          "smallestCity" : {  
             "name" : "EAST KILLINGLY",  
             "pop" : 25  
          },  
          "state" : "CT"  
       },  
       {  
          "biggestCity" : {  
             "name" : "SEATTLE",  
             "pop" : 520096  
          },  
          "smallestCity" : {  
             "name" : "BENGE",  
             "pop" : 2  
          },  
          "state" : "WA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BIRMINGHAM",  
             "pop" : 242606  
          },  
          "smallestCity" : {  
             "name" : "ALLEN",  
             "pop" : 0  
          },  
          "state" : "AL"  
       },  
       {  
          "biggestCity" : {  
             "name" : "LAS VEGAS",  
             "pop" : 597557  
          },  
          "smallestCity" : {  
             "name" : "TUSCARORA",  
             "pop" : 1  
          },  
          "state" : "NV"  
       },  
       {  
          "biggestCity" : {  
             "name" : "OMAHA",  
             "pop" : 358930  
          },  
          "smallestCity" : {  
             "name" : "LAKESIDE",  
             "pop" : 5  
          },  
          "state" : "NE"  
       },  
       {  
          "biggestCity" : {  
             "name" : "MIAMI",  
             "pop" : 825232  
          },  
          "smallestCity" : {  
             "name" : "CECIL FIELD NAS",  
             "pop" : 0  
          },  
          "state" : "FL"  
       },  
       {  
          "biggestCity" : {  
             "name" : "SIOUX FALLS",  
             "pop" : 102046  
          },  
          "smallestCity" : {  
             "name" : "ZEONA",  
             "pop" : 8  
          },  
          "state" : "SD"  
       },  
       {  
          "biggestCity" : {  
             "name" : "HOUSTON",  
             "pop" : 2095918  
          },  
          "smallestCity" : {  
             "name" : "FULTON",  
             "pop" : 0  
          },  
          "state" : "TX"  
       },  
       {  
          "biggestCity" : {  
             "name" : "MILWAUKEE",  
             "pop" : 597324  
          },  
          "smallestCity" : {  
             "name" : "CLAM LAKE",  
             "pop" : 2  
          },  
          "state" : "WI"  
       },  
       {  
          "biggestCity" : {  
             "name" : "JACKSON",  
             "pop" : 204788  
          },  
          "smallestCity" : {  
             "name" : "CHUNKY",  
             "pop" : 79  
          },  
          "state" : "MS"  
       },  
       {  
          "biggestCity" : {  
             "name" : "DES MOINES",  
             "pop" : 148155  
          },  
          "smallestCity" : {  
             "name" : "DOUDS",  
             "pop" : 15  
          },  
          "state" : "IA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "HONOLULU",  
             "pop" : 396643  
          },  
          "smallestCity" : {  
             "name" : "NINOLE",  
             "pop" : 0  
          },  
          "state" : "HI"  
       },  
       {  
          "biggestCity" : {  
             "name" : "CHARLOTTE",  
             "pop" : 465833  
          },  
          "smallestCity" : {  
             "name" : "GLOUCESTER",  
             "pop" : 0  
          },  
          "state" : "NC"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BILLINGS",  
             "pop" : 78805  
          },  
          "smallestCity" : {  
             "name" : "MOSBY",  
             "pop" : 7  
          },  
          "state" : "MT"  
       },  
       {  
          "biggestCity" : {  
             "name" : "TULSA",  
             "pop" : 389072  
          },  
          "smallestCity" : {  
             "name" : "SOUTHARD",  
             "pop" : 8  
          },  
          "state" : "OK"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BOISE",  
             "pop" : 165522  
          },  
          "smallestCity" : {  
             "name" : "KEUTERVILLE",  
             "pop" : 0  
          },  
          "state" : "ID"  
       },  
       {  
          "biggestCity" : {  
             "name" : "INDIANAPOLIS",  
             "pop" : 348868  
          },  
          "smallestCity" : {  
             "name" : "WESTPOINT",  
             "pop" : 145  
          },  
          "state" : "IN"  
       },  
       {  
          "biggestCity" : {  
             "name" : "LOUISVILLE",  
             "pop" : 288058  
          },  
          "smallestCity" : {  
             "name" : "BROWDER",  
             "pop" : 0  
          },  
          "state" : "KY"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BURLINGTON",  
             "pop" : 39127  
          },  
          "smallestCity" : {  
             "name" : "UNIV OF VERMONT",  
             "pop" : 0  
          },  
          "state" : "VT"  
       },  
       {  
          "biggestCity" : {  
             "name" : "PHILADELPHIA",  
             "pop" : 1610956  
          },  
          "smallestCity" : {  
             "name" : "HAMILTON",  
             "pop" : 0  
          },  
          "state" : "PA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "NEW ORLEANS",  
             "pop" : 496937  
          },  
          "smallestCity" : {  
             "name" : "FORDOCHE",  
             "pop" : 0  
          },  
          "state" : "LA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "COLUMBIA",  
             "pop" : 269521  
          },  
          "smallestCity" : {  
             "name" : "QUINBY",  
             "pop" : 0  
          },  
          "state" : "SC"  
       },  
       {  
          "biggestCity" : {  
             "name" : "WICHITA",  
             "pop" : 295115  
          },  
          "smallestCity" : {  
             "name" : "ARNOLD",  
             "pop" : 0  
          },  
          "state" : "KS"  
       },  
       {  
          "biggestCity" : {  
             "name" : "NEWARK",  
             "pop" : 275572  
          },  
          "smallestCity" : {  
             "name" : "IMLAYSTOWN",  
             "pop" : 17  
          },  
          "state" : "NJ"  
       },  
       {  
          "biggestCity" : {  
             "name" : "HUNTINGTON",  
             "pop" : 75343  
          },  
          "smallestCity" : {  
             "name" : "MOUNT CARBON",  
             "pop" : 0  
          },  
          "state" : "WV"  
       },  
       {  
          "biggestCity" : {  
             "name" : "MEMPHIS",  
             "pop" : 632837  
          },  
          "smallestCity" : {  
             "name" : "ALLRED",  
             "pop" : 2  
          },  
          "state" : "TN"  
       },  
       {  
          "biggestCity" : {  
             "name" : "VIRGINIA BEACH",  
             "pop" : 385080  
          },  
          "smallestCity" : {  
             "name" : "WALLOPS ISLAND",  
             "pop" : 0  
          },  
          "state" : "VA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "ANCHORAGE",  
             "pop" : 183987  
          },  
          "smallestCity" : {  
             "name" : "CHEVAK",  
             "pop" : 0  
          },  
          "state" : "AK"  
       },  
       {  
          "biggestCity" : {  
             "name" : "BROOKLYN",  
             "pop" : 2300504  
          },  
          "smallestCity" : {  
             "name" : "RAQUETTE LAKE",  
             "pop" : 0  
          },  
          "state" : "NY"  
       },  
       {  
          "biggestCity" : {  
             "name" : "DENVER",  
             "pop" : 451182  
          },  
          "smallestCity" : {  
             "name" : "CHEYENNE MTN AFB",  
             "pop" : 0  
          },  
          "state" : "CO"  
       },  
       {  
          "biggestCity" : {  
             "name" : "DETROIT",  
             "pop" : 963243  
          },  
          "smallestCity" : {  
             "name" : "LELAND",  
             "pop" : 0  
          },  
          "state" : "MI"  
       },  
       {  
          "biggestCity" : {  
             "name" : "PORTLAND",  
             "pop" : 518543  
          },  
          "smallestCity" : {  
             "name" : "KENT",  
             "pop" : 0  
          },  
          "state" : "OR"  
       },  
       {  
          "biggestCity" : {  
             "name" : "ATLANTA",  
             "pop" : 609591  
          },  
          "smallestCity" : {  
             "name" : "FORT STEWART",  
             "pop" : 0  
          },  
          "state" : "GA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "CRANSTON",  
             "pop" : 176404  
          },  
          "smallestCity" : {  
             "name" : "CLAYVILLE",  
             "pop" : 45  
          },  
          "state" : "RI"  
       },  
       {  
          "biggestCity" : {  
             "name" : "ALBUQUERQUE",  
             "pop" : 449584  
          },  
          "smallestCity" : {  
             "name" : "ALGODONES",  
             "pop" : 0  
          },  
          "state" : "NM"  
       },  
       {  
          "biggestCity" : {  
             "name" : "LOS ANGELES",  
             "pop" : 2102295  
          },  
          "smallestCity" : {  
             "name" : "TWIN BRIDGES",  
             "pop" : 0  
          },  
          "state" : "CA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "LITTLE ROCK",  
             "pop" : 192895  
          },  
          "smallestCity" : {  
             "name" : "TOMATO",  
             "pop" : 0  
          },  
          "state" : "AR"  
       },  
       {  
          "biggestCity" : {  
             "name" : "WORCESTER",  
             "pop" : 169856  
          },  
          "smallestCity" : {  
             "name" : "BUCKLAND",  
             "pop" : 16  
          },  
          "state" : "MA"  
       },  
       {  
          "biggestCity" : {  
             "name" : "PORTLAND",  
             "pop" : 63268  
          },  
          "smallestCity" : {  
             "name" : "BUSTINS ISLAND",  
             "pop" : 0  
          },  
          "state" : "ME"  
       }  
    ],  
    "ok" : 1  
 }  
 >   
   

All query in the examples works. It's amazing all three queries quickly bring results within second! Amazing. Whilst this is an short article to convince you to use aggregation on mongodb, and if you have been convince, you should really try on the following useful links too.


https://docs.mongodb.org/manual/core/map-reduce/

https://docs.mongodb.org/manual/reference/aggregation/

good luck!

Saturday, June 6, 2015

Learning aggregations in elasticsearch 1.5 - part2

This is second part of learning aggregations in elasticsearch 1.5.2. Please read part 1of this article before continue to try the remaining aggregations feature in this article.

If you are using script, then you can use this feature, scripted metric aggregation

Next, let's try global aggregation

 {  
  "took" : 3,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 0.8465736,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 0.8465736,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "all_products" : {  
    "doc_count" : 3,  
    "avg_price" : {  
     "value" : 3.7833333810170493  
    }  
   }  
  }  
 }  

Filter aggregation.

 {  
  "took" : 6,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "in_stock_products" : {  
    "doc_count" : 2,  
    "avg_price" : {  
     "value" : 4.180000066757202  
    }  
   }  
  }  
 }  

missing aggregation 

 {  
  "took" : 3,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "fruits_without_a_price" : {  
    "doc_count" : 0  
   }  
  }  
 }  

nested aggregation

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 0.8465736,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 0.8465736,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "suppliers" : {  
    "doc_count" : 7,  
    "near" : {  
     "value" : 1.68430081E8,  
     "value_as_string" : "10.10.10.1"  
    }  
   }  
  }  
 }  

reverse nested aggregation

 {  
  "took" : 13,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 0.8465736,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 0.8465736,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "suppliers" : {  
    "doc_count" : 7,  
    "top_suppliers" : {  
     "doc_count_error_upper_bound" : 0,  
     "sum_other_doc_count" : 0,  
     "buckets" : [ {  
      "key" : "company",  
      "doc_count" : 7,  
      "suppliers_to_issue" : {  
       "doc_count" : 3,  
       "top_tags_per_comment" : {  
        "doc_count_error_upper_bound" : 0,  
        "sum_other_doc_count" : 0,  
        "buckets" : [ {  
         "key" : "foods",  
         "doc_count" : 3  
        }, {  
         "key" : "fruits",  
         "doc_count" : 3  
        }, {  
         "key" : "red",  
         "doc_count" : 3  
        }, {  
         "key" : "large",  
         "doc_count" : 1  
        }, {  
         "key" : "medium",  
         "doc_count" : 1  
        }, {  
         "key" : "small",  
         "doc_count" : 1  
        } ]  
       }  
      }  
     }, {  
      "key" : "a",  
      "doc_count" : 3,  
      "suppliers_to_issue" : {  
       "doc_count" : 3,  
       "top_tags_per_comment" : {  
        "doc_count_error_upper_bound" : 0,  
        "sum_other_doc_count" : 0,  
        "buckets" : [ {  
         "key" : "foods",  
         "doc_count" : 3  
        }, {  
         "key" : "fruits",  
         "doc_count" : 3  
        }, {  
         "key" : "red",  
         "doc_count" : 3  
        }, {  
         "key" : "large",  
         "doc_count" : 1  
        }, {  
         "key" : "medium",  
         "doc_count" : 1  
        }, {  
         "key" : "small",  
         "doc_count" : 1  
        } ]  
       }  
      }  
     }, {  
      "key" : "b",  
      "doc_count" : 3,  
      "suppliers_to_issue" : {  
       "doc_count" : 3,  
       "top_tags_per_comment" : {  
        "doc_count_error_upper_bound" : 0,  
        "sum_other_doc_count" : 0,  
        "buckets" : [ {  
         "key" : "foods",  
         "doc_count" : 3  
        }, {  
         "key" : "fruits",  
         "doc_count" : 3  
        }, {  
         "key" : "red",  
         "doc_count" : 3  
        }, {  
         "key" : "large",  
         "doc_count" : 1  
        }, {  
         "key" : "medium",  
         "doc_count" : 1  
        }, {  
         "key" : "small",  
         "doc_count" : 1  
        } ]  
       }  
      }  
     }, {  
      "key" : "c",  
      "doc_count" : 1,  
      "suppliers_to_issue" : {  
       "doc_count" : 1,  
       "top_tags_per_comment" : {  
        "doc_count_error_upper_bound" : 0,  
        "sum_other_doc_count" : 0,  
        "buckets" : [ {  
         "key" : "foods",  
         "doc_count" : 1  
        }, {  
         "key" : "fruits",  
         "doc_count" : 1  
        }, {  
         "key" : "red",  
         "doc_count" : 1  
        }, {  
         "key" : "small",  
         "doc_count" : 1  
        } ]  
       }  
      }  
     } ]  
    }  
   }  
  }  
 }  

children aggregation. We don't have mapping for this, so we will skip this for now, maybe if you do it as homework, you can leave a comment here.

terms aggregation

 {  
  "took" : 4,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "tags" : {  
    "doc_count_error_upper_bound" : 0,  
    "sum_other_doc_count" : 0,  
    "buckets" : [ {  
     "key" : "foods",  
     "doc_count" : 3  
    }, {  
     "key" : "fruits",  
     "doc_count" : 3  
    }, {  
     "key" : "red",  
     "doc_count" : 3  
    }, {  
     "key" : "large",  
     "doc_count" : 1  
    }, {  
     "key" : "medium",  
     "doc_count" : 1  
    }, {  
     "key" : "small",  
     "doc_count" : 1  
    } ]  
   }  
  }  
 }  
   

significant terms aggregation

 {  
  "took" : 3,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 0.8465736,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 0.8465736,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "significantQuantityTypes" : {  
    "doc_count" : 3,  
    "buckets" : [ ]  
   }  
  }  
 }  

range aggregation

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "price_ranges" : {  
    "buckets" : [ {  
     "key" : "*-4.0",  
     "to" : 4.0,  
     "to_as_string" : "4.0",  
     "doc_count" : 2  
    }, {  
     "key" : "1.0-3.0",  
     "from" : 1.0,  
     "from_as_string" : "1.0",  
     "to" : 3.0,  
     "to_as_string" : "3.0",  
     "doc_count" : 1  
    }, {  
     "key" : "4.0-*",  
     "from" : 4.0,  
     "from_as_string" : "4.0",  
     "doc_count" : 1  
    } ]  
   }  
  }  
 }  

date range aggregation

 {  
  "took" : 9,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "range" : {  
    "buckets" : [ {  
     "key" : "*-2014-07-01T00:00:00.000Z",  
     "to" : 1.4041728E12,  
     "to_as_string" : "2014-07-01T00:00:00.000Z",  
     "doc_count" : 0  
    }, {  
     "key" : "2014-07-01T00:00:00.000Z-*",  
     "from" : 1.4041728E12,  
     "from_as_string" : "2014-07-01T00:00:00.000Z",  
     "doc_count" : 3  
    } ]  
   }  
  }  
 }  

IPv4 range aggregation

 {  
  "took" : 3,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "suppliers_ip_ranges" : {  
    "buckets" : [ {  
     "key" : "*-10.0.0.1",  
     "to" : 1.67772161E8,  
     "to_as_string" : "10.0.0.1",  
     "doc_count" : 0  
    }, {  
     "key" : "10.30.0.1-*",  
     "from" : 1.69738241E8,  
     "from_as_string" : "10.30.0.1",  
     "doc_count" : 0  
    } ]  
   }  
  }  
 }  

histogram aggregation

 {  
  "took" : 3,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "prices" : {  
    "buckets" : [ {  
     "key" : 2,  
     "doc_count" : 1  
    }, {  
     "key" : 3,  
     "doc_count" : 1  
    }, {  
     "key" : 4,  
     "doc_count" : 1  
    } ]  
   }  
  }  
 }  

date histogram aggregation

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "prices_over_time" : {  
    "buckets" : [ {  
     "key_as_string" : "2015-05-14T00:00:00.000Z",  
     "key" : 1431561600000,  
     "doc_count" : 2  
    }, {  
     "key_as_string" : "2015-05-15T00:00:00.000Z",  
     "key" : 1431648000000,  
     "doc_count" : 1  
    } ]  
   }  
  }  
 }  

geo distance aggregation

 {  
  "took" : 2,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "rings_around_x" : {  
    "buckets" : [ {  
     "key" : "*-100.0",  
     "from" : 0.0,  
     "to" : 100.0,  
     "doc_count" : 0  
    }, {  
     "key" : "100.0-300.0",  
     "from" : 100.0,  
     "to" : 300.0,  
     "doc_count" : 0  
    }, {  
     "key" : "300.0-*",  
     "from" : 300.0,  
     "doc_count" : 0  
    } ]  
   }  
  }  
 }  

GeoHash grid aggregation

 {  
  "took" : 2,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "myLarge-GrainGeoHashGrid" : {  
    "buckets" : [ ]  
   }  
  }  
 }  

That's it, elasticsearch aggregations has a lot of features in comparison to the previous facets. So this is a light learning on various aggregation going through in elasticsearch 1.5.2. If you interested in the certain aggreation, I suggest you spend more time to experiment on the aggregation. You can download all the index, data, and mapping here.

Friday, June 5, 2015

Learning aggregations in elasticsearch 1.5 - part1

In the last article, we learned elasticsearch facets and in this article, we will learn the newer aggregations framework from elasticsearch. It is recommended you read the previous article on facets as it give you some idea what was in the past and it assists you in this article learning.

Before we jump into elasticsearch aggregation, let's take a look of the taxonomy of the data first. Let's use an example that you and I use everyday, we all need food. With food, we have fruits within food. A wide variety of fruits can be available and we will use different type of fruits for queries later. So you may already guessed, the index is foods and the type is fruits. Unlike previous article, in this article, we will create mapping first. The reason is with each unique type to the fruit, we can do things like range, date, or geo query.

Okay, let's get started, the following script is to create an index and its mapping. You should also be able to retrieve it here.

1:  #!/bin/bash  
2:    
3:    
4:  curl -XPUT 'http://localhost:9200/foods/?pretty'  
5:    
6:  sleep 3  
7:    
8:  curl -XPUT 'http://localhost:9200/foods/_mapping/fruits' -d '  
9:  {  
10:    "fruits" : {  
11:      "properties" : {  
12:        "insert_date"   : { "type" : "date"},  
13:        "name"      : { "type" : "string" },  
14:        "grade"      : { "type" : "string" },  
15:        "price"      : { "type" : "float"},  
16:        "price_date"   : { "type" : "date"},  
17:        "staff_update"  : { "type" : "object", "properties" : { "staff" : { "type": "object", "properties" : { "id" : { "type" : "string"}, "name" : {"type": "string"} } } } },  
18:        "quantity"    : { "type" : "integer"},  
19:        "quantity_max"  : { "type" : "integer"},  
20:        "quantity_min"  : { "type" : "integer"},  
21:        "tags"      : { "type" : "string"},  
22:        "quantity_enough" : { "type" : "boolean"},  
23:        "suppliers"    : { "type" : "nested", "properties" : { "vendor_name" : {"type": "string"}, "vendor_ip": {"type": "ip"}, "vendor_coordinate": {"type": "string"} } }  
24:      }  
25:    }  
26:  }'  

now we check if the mapping are okay and health of the cluster.

1:  [user@localhost ~]$ curl 'localhost:9200/_cat/health?v'  
2:  epoch   timestamp cluster    status node.total node.data shards pri relo init unassign pending_tasks   
3:  1431691876 14:11:16 elasticsearch green      3     3   10  5  0  0    0       0   
4:  [user@localhost ~]$ curl -XGET 'http://localhost:9200/foods/_mapping/?pretty'  
5:  {  
6:   "foods" : {  
7:    "mappings" : {  
8:     "fruits" : {  
9:      "properties" : {  
10:       "grade" : {  
11:        "type" : "string"  
12:       },  
13:       "insert_date" : {  
14:        "type" : "date",  
15:        "format" : "dateOptionalTime"  
16:       },  
17:       "name" : {  
18:        "type" : "string"  
19:       },  
20:       "price" : {  
21:        "type" : "float"  
22:       },  
23:       "price_date" : {  
24:        "type" : "date",  
25:        "format" : "dateOptionalTime"  
26:       },  
27:       "quantity" : {  
28:        "type" : "integer"  
29:       },  
30:       "quantity_enough" : {  
31:        "type" : "boolean"  
32:       },  
33:       "quantity_max" : {  
34:        "type" : "integer"  
35:       },  
36:       "quantity_min" : {  
37:        "type" : "integer"  
38:       },  
39:       "staff_update" : {  
40:        "properties" : {  
41:         "staff" : {  
42:          "properties" : {  
43:           "id" : {  
44:            "type" : "string"  
45:           },  
46:           "name" : {  
47:            "type" : "string"  
48:           }  
49:          }  
50:         }  
51:        }  
52:       },  
53:       "suppliers" : {  
54:        "type" : "nested",  
55:        "properties" : {  
56:         "vendor_coordinate" : {  
57:          "type" : "string"  
58:         },  
59:         "vendor_ip" : {  
60:          "type" : "ip"  
61:         },  
62:         "vendor_name" : {  
63:          "type" : "string"  
64:         }  
65:        }  
66:       },  
67:       "tags" : {  
68:        "type" : "string"  
69:       }  
70:      }  
71:     }  
72:    }  
73:   }  
74:  }  
75:  [user@localhost ~]$   

Look good, we are ready to index some sample data. A sample below but you should be able to get more here.

 curl -XPOST "http://localhost:9200/foods/fruits/1?pretty" -d '  
 {  
   "insert_date"   : "2015-05-15 20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "b"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }'  

okay, let's get into the actual works, min aggregation.

 {  
  "took" : 131,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 1.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 1.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 0.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "min_price" : {  
    "value" : 0.9900000095367432  
   }  
  }  
 }  

So I have no idea why is the floating end with 95367432.  Let's see on the next example, max aggregation,

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 1.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 1.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 0.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "max_price" : {  
    "value" : 1.9800000190734863  
   }  
  }  
 }  

Next, sum aggregation.

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 1.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 1.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 0.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "sum_all_item_price" : {  
    "value" : 4.350000023841858  
   }  
  }  
 }  

the average

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 1.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 1.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 0.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "avg_grade" : {  
    "value" : 1.450000007947286  
   }  
  }  
 }  

Something different now, statistics aggreation. This one is cool as you can combine the above output into one.

 {  
  "took" : 5,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 1.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 1.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 0.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "prices_stats" : {  
    "count" : 3,  
    "min" : 0.9900000095367432,  
    "max" : 1.9800000190734863,  
    "avg" : 1.450000007947286,  
    "sum" : 4.350000023841858  
   }  
  }  
 }  

and if you want extra statistics exposure, try extended stats aggregation.

 {  
  "took" : 3,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 1.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 1.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 0.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "prices_stats" : {  
    "count" : 3,  
    "min" : 0.9900000095367432,  
    "max" : 1.9800000190734863,  
    "avg" : 1.450000007947286,  
    "sum" : 4.350000023841858,  
    "sum_of_squares" : 6.804900081253052,  
    "variance" : 0.16580000403722148,  
    "std_deviation" : 0.4071854663875191,  
    "std_deviation_bounds" : {  
     "upper" : 2.264370940722324,  
     "lower" : 0.6356290751722476  
    }  
   }  
  }  
 }  

value count aggregation.

 {  
  "took" : 9,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "prices_count" : {  
    "value" : 3  
   }  
  }  
 }  

percentile aggregation. This is cool to see your data distributions, like from 1% to 99%, where are the usual data distributed.

 {  
  "took" : 15,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "quantity_outlier" : {  
    "values" : {  
     "1.0" : 9.18,  
     "5.0" : 9.9,  
     "25.0" : 13.5,  
     "50.0" : 18.0,  
     "75.0" : 19.0,  
     "95.0" : 19.8,  
     "99.0" : 19.96  
    }  
   }  
  }  
 }  

percentile ranks aggregation.

 {  
  "took" : 4,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "quantity_outlier" : {  
    "values" : {  
     "15.0" : 0.0,  
     "30.0" : 100.0  
    }  
   }  
  }  
 }  

cardinality aggregation. Note that this is experimental, it may have been removed in the future.

 {  
  "took" : 21,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "grade_count" : {  
    "value" : 3  
   }  
  }  
 }  

geo bounds aggregation

 {  
  "took" : 4,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 0.8465736,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 0.8465736,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 0.70273256,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "viewport" : { }  
  }  
 }  

Top hits Aggregation

 {  
  "took" : 38,  
  "timed_out" : false,  
  "_shards" : {  
   "total" : 5,  
   "successful" : 5,  
   "failed" : 0  
  },  
  "hits" : {  
   "total" : 3,  
   "max_score" : 1.0,  
   "hits" : [ {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "1",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-15T20:18:50",  
   "name"      : "apple-a",  
   "grade"      : "A",  
   "price"      : 4.98,  
   "price_date"   : "2015-05-15",  
   "staff_update"  : {"staff" : {"id" : 9739, "name" : "John Smith"} },  
   "quantity"    : 20,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "large", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "2",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:50",  
   "name"      : "apple-b",  
   "grade"      : "B",  
   "price"      : 3.38,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 18,  
   "quantity_max"  : 30,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "medium", "red"],  
   "quantity_enough" : true,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}]  
 }  
   }, {  
    "_index" : "foods",  
    "_type" : "fruits",  
    "_id" : "3",  
    "_score" : 1.0,  
    "_source":  
 {  
   "insert_date"   : "2015-05-14T20:18:55",  
   "name"      : "apple-c",  
   "grade"      : "C",  
   "price"      : 2.99,  
   "price_date"   : "2015-05-14",  
   "staff_update"  : {"staff" : {"id" : 7795, "name" : "Tide Hunter"} },  
   "quantity"    : 9,  
   "quantity_max"  : 40,  
   "quantity_min"  : 10,  
   "tags"      : ["fruits", "foods", "small", "red"],  
   "quantity_enough" : false,  
   "suppliers"    : [{"vendor_name": "company-A", "vendor_ip": "10.10.10.1", "vendor_coordinate": "41.72,-10.35"}, {"vendor_name": "company-B", "vendor_ip": "10.20.10.1", "vendor_coordinate": "45.72,8.35"}, {"vendor_name": "company-C", "vendor_ip": "203.83.10.55", "vendor_coordinate": "11.72,18.72"}]  
 }  
   } ]  
  },  
  "aggregations" : {  
   "top-tags" : {  
    "doc_count_error_upper_bound" : 0,  
    "sum_other_doc_count" : 6,  
    "buckets" : [ {  
     "key" : "foods",  
     "doc_count" : 3,  
     "top_tag_hits" : {  
      "hits" : {  
       "total" : 3,  
       "max_score" : null,  
       "hits" : [ {  
        "_index" : "foods",  
        "_type" : "fruits",  
        "_id" : "1",  
        "_score" : null,  
        "_source":{"price":4.98},  
        "sort" : [ 1431721130000 ]  
       } ]  
      }  
     }  
    }, {  
     "key" : "fruits",  
     "doc_count" : 3,  
     "top_tag_hits" : {  
      "hits" : {  
       "total" : 3,  
       "max_score" : null,  
       "hits" : [ {  
        "_index" : "foods",  
        "_type" : "fruits",  
        "_id" : "1",  
        "_score" : null,  
        "_source":{"price":4.98},  
        "sort" : [ 1431721130000 ]  
       } ]  
      }  
     }  
    } ]  
   }  
  }  
 }  
   

Okay, we have covered a lot in this article for aggregations. But there are more to come in the next article. Hence, let's continue the rest of aggregation in the incoming article.