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.