Sunday, June 21, 2015

Learning JavaFX on eclipse luna

Today, we will learn JavaFX using eclipse luna as the IDE. It's a start learning journey to get acquainted with the basic of JavaFX in the eclipse development environment. Essentially it is a 'hello world' application. First, let's take a look what is JavaFX. From wikipedia,

JavaFX is a software platform for creating and delivering rich internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future.[3] JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linux, and Mac OS X.

Okay, so javaFX is a GUI related development arena. With that said, let's start with a simple hello world GUI application for JavaFX. This article assume your java project is using java 8 and eclipse luna and you have setup already. Below is a sample code.

1:  package play.learn.java.fx;  
2:    
3:  import javafx.application.Application;  
4:  import javafx.event.ActionEvent;  
5:  import javafx.event.EventHandler;  
6:  import javafx.scene.Scene;  
7:  import javafx.scene.control.Button;  
8:  import javafx.scene.layout.StackPane;  
9:  import javafx.stage.Stage;  
10:    
11:  public class HelloWorld extends Application {  
12:    
13:     @Override  
14:     public void start(Stage primaryStage) throws Exception {  
15:        Button btn = new Button();  
16:      btn.setText("Say 'Hello World'");  
17:      btn.setOnAction(new EventHandler<ActionEvent>() {  
18:     
19:        @Override  
20:        public void handle(ActionEvent event) {  
21:          System.out.println("Hello World!");  
22:        }  
23:      });  
24:        
25:      StackPane root = new StackPane();  
26:      root.getChildren().add(btn);  
27:        
28:      Scene scene = new Scene(root, 300, 250);  
29:    
30:      primaryStage.setTitle("Hello World!");  
31:      primaryStage.setScene(scene);  
32:      primaryStage.show();  
33:          
34:     }  
35:       
36:     public static void main(String[] args) {  
37:        launch(args);  
38:    
39:     }  
40:  }  


As you can see above, there is a warning about restrict access to the api. To summarize the warning short, it is because non java library is not import by default into the project. So in this situation, you will have to manually add it. It's simple, on the project, right click and then select Properties, then a window pop up and in the Java Build Path tree, click on the 'Add External JARs...' , now you will have to locate where is the java 8 installed, and then select a jar file name jfxrt.jar. It will be relative to where the JAVA_HOME install such that, <JAVA_HOME>/jre/lib/ext/




When that is done, the warning should be dissapear. Now run the application, a window should pop up and click on it, look at the eclipse console, you should see "Hello World!". A little remarks to understand the basic of this application.

Here are the important things to know about the basic structure of a JavaFX application:


  •     The main class for a JavaFX application extends the javafx.application.Application class. The start() method is the main entry point for all JavaFX applications.
  •     A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content. Example 3-1 creates the stage and scene and makes the scene visible in a given pixel size.
  •     In JavaFX, the content of the scene is represented as a hierarchical scene graph of nodes. In this example, the root node is a StackPane object, which is a resizable layout node. This means that the root node's size tracks the scene's size and changes when the stage is resized by a user.
  •     The root node contains one child node, a button control with text, plus an event handler to print a message when the button is pressed.
  •     The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.


The above are excerpt from official documentation. The code can also be found here. That's it, have fun to explore more of JavaFX.

Saturday, June 20, 2015

Fix corrupted ods file

If you have been working with spreadsheet, then one day, when you open up the file, for some unknown reason, it show gibberish text. You will like OH MY GAWD!! where is my file!!?? well afraid not, today, we will try to recover the file. To be exact, the spreadsheet is ods format from open office. You can find more information here.

So, a good normal working ods file start with PK. See example below.

 $ hexdump -C myfile.ods | head -1  
 00000000 50 4b 03 04 14 00 00 08 00 00 b7 71 c5 46 85 6c |PK.........q.F.l|  

The broken one does not start with PK and for my spreadsheet, it is something like the following. It may be different than you but that does not matter.

 $ hexdump -C myfile.ods | head -1  
 00000000 2c 75 73 65 72 2c 55 73 65 72 57 6f 72 6b 73 |,user,UserWorks|  

because openoffice file is compressed file, and then you can fix using the application zip. To fix it, you can run the command such as the one below.

 user@localhost:~$ zip --fixfix myfile.ods --out myfixfile.ods   
 Fix archive (-FF) - salvage what can  
  Found end record (EOCDR) - says expect single disk archive  
 Scanning for entries...  
  copying: Object 1/styles.xml (398 bytes)  
  copying: Object 1/content.xml (1892 bytes)  
  copying: Object 1/meta.xml (281 bytes)  
  copying: Object 2/content.xml (1999 bytes)  
  copying: Object 2/meta.xml (281 bytes)  
  copying: Object 2/styles.xml (483 bytes)  
  copying: Object 3/content.xml (2116 bytes)  
  copying: Object 3/meta.xml (281 bytes)  
  copying: Object 3/styles.xml (398 bytes)  
  copying: styles.xml (1999 bytes)  
  copying: Object 4/meta.xml (281 bytes)  
  copying: Object 4/content.xml (2405 bytes)  
  copying: Object 4/styles.xml (398 bytes)  
  copying: content.xml (17364 bytes)  
  copying: meta.xml (441 bytes)  
  copying: ObjectReplacements/Object 1 (2278 bytes)  
  copying: ObjectReplacements/Object 2 (3654 bytes)  
  copying: ObjectReplacements/Object 3 (1924 bytes)  
  copying: ObjectReplacements/Object 4 (2483 bytes)  
  copying: META-INF/manifest.xml (449 bytes)  
 Central Directory found...  
 no local entry: mimetype  
 no local entry: settings.xml  
 no local entry: manifest.rdf  
 no local entry: Configurations2/menubar/  
 no local entry: Configurations2/toolpanel/  
 no local entry: Configurations2/progressbar/  
 no local entry: Configurations2/accelerator/current.xml  
 no local entry: Configurations2/statusbar/  
 no local entry: Configurations2/images/Bitmaps/  
 no local entry: Configurations2/toolbar/  
 no local entry: Configurations2/floater/  
 no local entry: Configurations2/popupmenu/  
 no local entry: Thumbnails/thumbnail.png  
 EOCDR found ( 1 73809)...  

So the above command will try to salvage whatever it can. You might have guess it, the fix version file is the one specified by --out parameter.

This method works superb for my corrupted file. The fix version of the file contain all the data as before and I was happy. :) I hope it works for you too. That's it for today learning. Good luck to you!

Friday, June 19, 2015

Learn lucene term range query

Today, we are going to learn lucene term range query. But first, what actually is lucene term range query? From the official javadoc definition

A Query that matches documents within an range of terms.

This query matches the documents looking for terms that fall into the supplied range according to Byte.compareTo(Byte). It is not intended for numerical ranges; use NumericRangeQuery instead.

This query uses the MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT rewrite method.

So byte to byte comparison of between two ranges, because it is byte to byte comparison, the comparison is lexicographic. If you intend to find range between two numbers, this is not the class you should use. Okay, if this is not clear, let's go into the code, shall we?

As you know, lucene is about two parts, the first indexing (write) part and then search (query) part. So in this article, we are going to index and query using term range query. To give you an overall of this article, we have four class.

  • LuceneConstants - just a setting class for this application.
  • Indexer - the class that does the indexing. 
  • Searcher - a class that do the search.
  • LearnTermRangeQuery - our main entry class to bind the above three classes into one. 
We have create an object tester for this learning journey. We then create index by calling method createIndex and then the index using term range query.


1:  LearnTermRangeQuery tester;  
2:    
3:  try {  
4:     tester = new LearnTermRangeQuery();  
5:     tester.createIndex();  
6:     tester.searchUsingTermRangeQuery("record2.txt", "record6.txt");  
7:  } catch (Exception e) {  
8:       
9:  }  

In the method createIndex(), I have some lambda usage, which you can notice with the arrow symbol, so you need to have java8 installed. There are two variables, indexDir and dataDir. The variable, indexDir is there directory where the created index will reside whilst dataDir is the sample data to be index upon. In the class Indexer, method getDocument(), is essentially index all sample documents. Nothing fancy, just ordinary creating lucene document and three fields, filename, filepath and file content.

Back to the class LearnTermRangeQuery, method searchUsingTermRangeQuery(). Notice we search the range with two files as the border. We initialized a lucene directory object and pass to the object index searcher. Everything else for lucene index searcher is just standard. We construct the TermRangeQuery and passed to the searcher object. The results are then shown and eventually close.

Below are the sample output in eclipse output.

 record 21.txt  
 src/resources/samples.termrange/record 21.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record 21.txt  
 record 33 .txt  
 src/resources/samples.termrange/record 33 .txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record 33 .txt  
 record10.txt  
 src/resources/samples.termrange/record10.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record10.txt  
 record7.txt  
 src/resources/samples.termrange/record7.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record7.txt  
 record6.txt  
 src/resources/samples.termrange/record6.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record6.txt  
 record9.txt  
 src/resources/samples.termrange/record9.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record9.txt  
 record33.txt  
 src/resources/samples.termrange/record33.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record33.txt  
 record2.txt  
 src/resources/samples.termrange/record2.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record2.txt  
 record5.txt  
 src/resources/samples.termrange/record5.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record5.txt  
 record 33.txt  
 src/resources/samples.termrange/record 33.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record 33.txt  
 record3.txt  
 src/resources/samples.termrange/record3.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record3.txt  
 record8.txt  
 src/resources/samples.termrange/record8.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record8.txt  
 record2.1.txt  
 src/resources/samples.termrange/record2.1.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record2.1.txt  
 record1.txt  
 src/resources/samples.termrange/record1.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record1.txt  
 record4.txt  
 src/resources/samples.termrange/record4.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record4.txt  
 record22.txt  
 src/resources/samples.termrange/record22.txt  
 Indexing /home/user/eclipse/test/src/resources/samples.termrange/record22.txt  
 16 File indexed, time taken: 800 ms  
 6 documents found. Time :74ms  
 File : /home/user/eclipse/test/src/resources/samples.termrange/record33.txt  
 File : /home/user/eclipse/test/src/resources/samples.termrange/record2.txt  
 File : /home/user/eclipse/test/src/resources/samples.termrange/record5.txt  
 File : /home/user/eclipse/test/src/resources/samples.termrange/record3.txt  
 File : /home/user/eclipse/test/src/resources/samples.termrange/record4.txt  
 File : /home/user/eclipse/test/src/resources/samples.termrange/record22.txt  
   

As you can see above, the result are not correct if you consider numeric file name from record2.txt to record6.txt. So, always try experiment for few values before you implement. hehe, have fun! You can get the source for this codes at my github.

Sunday, June 7, 2015

code path learning on elasticsearch monitoring jvm logging

Today we are going to study the following info logging from elasticsearch 0.90.7.

1:  [2015-03-25 00:45:07,008][INFO ][monitor.jvm       ] [node03] [gc][ParNew][649825][391294] duration [829ms], collections [1]/[1.1s], total [829ms]/[2.8h], memory [8.4gb]->[8.5gb]/[14gb], all_pools {[Code Cache] [14.4mb]->[14.4mb]/[48mb]}{[Par Eden Space] [34.4mb]->[460.1kb]/[133.1mb]}{[Par Survivor Space] [16.6mb]->[16.6mb]/[16.6mb]}{[CMS Old Gen] [8.3gb]->[8.4gb]/[13.8gb]}{[CMS Perm Gen] [43.5mb]->[43.5mb]/[82mb]}  
2:  [2015-03-25 00:45:11,529][INFO ][monitor.jvm       ] [node03] [gc][ParNew][649829][391299] duration [921ms], collections [1]/[1.3s], total [921ms]/[2.8h], memory [8.7gb]->[8.8gb]/[14gb], all_pools {[Code Cache] [14.4mb]->[14.4mb]/[48mb]}{[Par Eden Space] [203.2kb]->[4.1mb]/[133.1mb]}{[Par Survivor Space] [16.6mb]->[16.6mb]/[16.6mb]}{[CMS Old Gen] [8.7gb]->[8.8gb]/[13.8gb]}{[CMS Perm Gen] [43.5mb]->[43.5mb]/[82mb]}  
3:  [2015-03-25 00:45:13,800][INFO ][monitor.jvm       ] [node03] [gc][ParNew][649831][391301] duration [744ms], collections [1]/[1.1s], total [744ms]/[2.8h], memory [8.9gb]->[9gb]/[14gb], all_pools {[Code Cache] [14.4mb]->[14.4mb]/[48mb]}{[Par Eden Space] [5.7mb]->[537.8kb]/[133.1mb]}{[Par Survivor Space] [16.6mb]->[16.6mb]/[16.6mb]}{[CMS Old Gen] [8.9gb]->[9gb]/[13.8gb]}{[CMS Perm Gen] [43.5mb]->[43.5mb]/[82mb]}  
4:  [2015-03-25 00:45:15,088][INFO ][monitor.jvm       ] [node03] [gc][ParNew][649832][391302] duration [891ms], collections [1]/[1.2s], total [891ms]/[2.8h], memory [9gb]->[9.1gb]/[14gb], all_pools {[Code Cache] [14.4mb]->[14.4mb]/[48mb]}{[Par Eden Space] [537.8kb]->[5.4mb]/[133.1mb]}{[Par Survivor Space] [16.6mb]->[16.6mb]/[16.6mb]}{[CMS Old Gen] [9gb]->[9.1gb]/[13.8gb]}{[CMS Perm Gen] [43.5mb]->[43.5mb]/[82mb]}  
5:  [2015-03-25 00:45:17,287][INFO ][monitor.jvm       ] [node03] [gc][ParNew][649834][391304] duration [770ms], collections [1]/[1.1s], total [770ms]/[2.8h], memory [9.2gb]->[9.3gb]/[14gb], all_pools {[Code Cache] [14.4mb]->[14.4mb]/[48mb]}{[Par Eden Space] [359.7kb]->[357.3kb]/[133.1mb]}{[Par Survivor Space] [16.6mb]->[16.6mb]/[16.6mb]}{[CMS Old Gen] [9.2gb]->[9.3gb]/[13.8gb]}{[CMS Perm Gen] [43.5mb]->[43.5mb]/[82mb]}  
6:  [2015-03-25 00:45:18,531][INFO ][monitor.jvm       ] [node03] [gc][ParNew][649835][391305] duration [713ms], collections [1]/[1.2s], total [713ms]/[2.8h], memory [9.3gb]->[9.4gb]/[14gb], all_pools {[Code Cache] [14.4mb]->[14.4mb]/[48mb]}{[Par Eden Space] [357.3kb]->[441.9kb]/[133.1mb]}{[Par Survivor Space] [16.6mb]->[16.6mb]/[16.6mb]}{[CMS Old Gen] [9.3gb]->[9.4gb]/[13.8gb]}{[CMS Perm Gen] [43.5mb]->[43.5mb]/[82mb]}  

Okay, before we go into this codes, let's analyze based on just the output above. We have info logging, nothing very seriously but as a sign we should already pay attention. Happened on one of the node, node03 with class file from package monitor.jvm. Let's take the first line in the log above and format nicely and so we can analyze further. Read below.

 [2015-03-25 00:45:07,008][INFO ][monitor.jvm       ] [node03] [gc]  
   
 [ParNew][649825][391294] duration [829ms],   
 collections [1]/[1.1s],   
 total [829ms]/[2.8h],   
 memory [8.4gb]->[8.5gb]/[14gb],   
 all_pools {[Code Cache]          [14.4mb]->[ 14.4mb ] / [   48mb]}  
           {[Par Eden Space]      [34.4mb]->[460.1kb ] / [133.1mb]}  
           {[Par Survivor Space]  [16.6mb]->[ 16.6mb ] / [ 16.6mb]}  
           {[CMS Old Gen]         [8.3gb] ->[  8.4gb ] / [ 13.8gb]}  
           {[CMS Perm Gen]        [43.5mb]->[ 43.5mb ] / [   82mb]}  

From just the above output, it become even clearer, the gc par new collection duration is 829milliseconds and one collection of 1.1seconds. The heap before was 8.4GB and after collected became 8.5GB with a total heap of 14GB. Then we have all the pools jvm statistics revealed. Pretty obvious, before and after with total memory heap assigned for the pool respectively.

Now, we will read into the code and verify if our assumption are valid. The class that log this is JvmMonitorService.java and we noticed it fall in the if else second evaluation clause.

Again, interpretation based on the code, it is an info logging with gc name is par new. Sequence of this gc is 649825. In this collection, total collection for par new is 391294 and its total collections time is 391294 milliseconds (6 minutes 31 seconds). Note that this collections time is derived with the difference in the last gc and this gc.

Next, the collections total counts between last count and this count. It is follow by the collections time in milliseconds. In this example, we have one collections performed in  1.1seconds.. hmm... Then we have the total time in milliseconds (829 milliseconds) with the gc collections time and in the output, it is showing 2.8 hours!

For the next jvm statistics, we have memory/heap statistics. So it is different than we interpret based on the log before, this statistics shown in the last jvm memory used and then current jvm used. The different is about one GB. The total memory/heap allocated for this jvm is 14GB.

The last statistics is the jvm pool statistics. Notice that the pool statistics is built such that the previous jvm collection run and this jvm collection run were built. The logic of output is such that the previous jvm pool used and then current jvm pool used with current pool max heap allocated. Let's take one from the pool for discussion, {[CMS Old Gen]        [8.3gb] ->[  8.4gb ] / [ 13.8gb]} . So cms old gen pool previous jvm usage was 8.3gb and in this jvm collection, the current usage is 8.4GB. This cms old gen max used is 13.8GB.

That's it and if you think this analysis is not correct, please leave your comment below and make the necessary correction.

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.