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.
No comments:
Post a Comment