{"id":7325,"date":"2024-04-07T12:01:01","date_gmt":"2024-04-07T04:01:01","guid":{"rendered":""},"modified":"2024-04-07T12:01:01","modified_gmt":"2024-04-07T04:01:01","slug":"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3 Elasticsearch \u4e2d\u7684 Bucket aggregation","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7325.html","title":{"rendered":"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3 Elasticsearch \u4e2d\u7684 Bucket aggregation"},"content":{"rendered":"

Elasticsearch \u9664\u4e86\u5728\u641c\u7d22\u65b9\u9762\u975e\u5e38\u4e4b\u5feb\uff0c\u5bf9\u6570\u636e\u5206\u6790\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u9762\u3002\u6b63\u786e\u7406\u89e3 Bucket aggregation \u5bf9\u6211\u4eec\u4f7f\u7528 Kibana \u975e\u5e38\u91cd\u8981\u3002Elasticsearch \u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684 aggregation \u53ef\u4ee5\u4f9b\u6211\u4eec\u4f7f\u7528\u3002\u5176\u4e2d Bucket aggregation \u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\u4e5f\u662f\u6bd4\u8f83\u4e0d\u5bb9\u6613\u7406\u89e3\u7684\u4e00\u4e2a\u3002\u5728\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u6765\u91cd\u70b9\u8bb2\u8ff0\u8fd9\u4e2a\u3002<\/p>\n

\u7b80\u5355\u5730\u8bf4\uff1a\u4e00\u4e2a\u6876\u4ee3\u8868\u4e00\u4e2a\u5177\u6709\u5171\u540c\u6807\u51c6\u7684\u6587\u6863\u96c6\u5408\u3002\u5b58\u50a8\u6876\uff08bucket\uff09\u662f\u805a\u5408\u7684\u5173\u952e\u8981\u7d20\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u60f3\u5206\u6790\u6bcf\u4e2a\u6708\u7684log\u6d41\u91cf\uff1a<\/p>\n

\"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

 <\/p>\n

\u5b58\u50a8\u6876\u805a\u5408\uff08bucket aggregation\uff09\u4e0d\u50cf\u6307\u6807\u805a\u5408\uff08Metric aggregation\uff09\u90a3\u6837\u8ba1\u7b97\u5b57\u6bb5\u7684\u6307\u6807\uff0c\u800c\u662f\u521b\u5efa\u6587\u6863\u5b58\u50a8\u6876\u3002 \u6bcf\u4e2a\u5b58\u50a8\u6876\u90fd\u4e0e\u4e00\u4e2a\u6807\u51c6\uff08\u53d6\u51b3\u4e8e\u805a\u5408\u7c7b\u578b\uff09\u76f8\u5173\u8054\uff0c\u8be5\u6807\u51c6\u786e\u5b9a\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u7684\u6587\u6863\u662f\u5426\u201c\u843d\u5165\u201d\u5176\u4e2d\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b58\u50a8\u6876\u6709\u6548\u5730\u5b9a\u4e49\u4e86\u6587\u6863\u96c6\u3002 \u9664\u4e86\u5b58\u50a8\u6876\u672c\u8eab\u4e4b\u5916\uff0c\u5b58\u50a8\u6876\u805a\u5408\u8fd8\u8ba1\u7b97\u5e76\u8fd4\u56de\u843d\u5165\u6bcf\u4e2a\u5b58\u50a8\u6876\u7684\u6587\u6863\u6570\u91cf\u3002<\/p>\n

\u4e0e\u6307\u6807\u805a\u5408\u76f8\u53cd\uff0c\u5b58\u50a8\u6876\u805a\u5408\u53ef\u4ee5\u4fdd\u5b58\u5b50\u805a\u5408\u3002 \u8fd9\u4e9b\u5b50\u805a\u5408\u5c06\u9488\u5bf9\u5176\u201c\u7236\u201d\u5b58\u50a8\u6876\u805a\u5408\u521b\u5efa\u7684\u5b58\u50a8\u6876\u8fdb\u884c\u805a\u5408\u3002<\/p>\n

\u6709\u4e0d\u540c\u7684\u5b58\u50a8\u6876\u805a\u5408\u5668\uff0c\u6bcf\u4e2a\u805a\u5408\u5668\u90fd\u6709\u4e0d\u540c\u7684\u201c\u5b58\u50a8\u6876\u201d\u7b56\u7565\u3002 \u4e00\u4e9b\u5b9a\u4e49\u5355\u4e2a\u5b58\u50a8\u6876\uff0c\u4e00\u4e9b\u5b9a\u4e49\u56fa\u5b9a\u6570\u91cf\u7684\u591a\u4e2a\u5b58\u50a8\u6876\uff0c\u53e6\u4e00\u4e9b\u5b9a\u4e49\u5728\u805a\u5408\u8fc7\u7a0b\u4e2d\u52a8\u6001\u521b\u5efa\u5b58\u50a8\u6876\u3002<\/p>\n

\u5c3d\u7ba1\u5b58\u50a8\u6876\u805a\u5408\u4e0d\u8ba1\u7b97\u6307\u6807\uff0c\u4f46\u5b83\u4eec\u53ef\u4ee5\u5305\u542b\u53ef\u4ee5\u4e3a\u5b58\u50a8\u6876\u805a\u5408\u751f\u6210\u7684\u6bcf\u4e2a\u5b58\u50a8\u6876\u8ba1\u7b97\u6307\u6807\u7684\u6307\u6807\u5b50\u805a\u5408\u3002 \u8fd9\u4f7f\u5b58\u50a8\u6876\u805a\u5408\u5bf9\u4e8e\u7c92\u5ea6\u8868\u793a\u548c\u5206\u6790 Elasticsearch \u7d22\u5f15\u975e\u5e38\u6709\u7528\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u76f4\u65b9\u56fe\uff08histogram\uff09\uff0c\u8303\u56f4\uff08range\uff09\uff0c\u8fc7\u6ee4\u5668\uff08filter\uff09\u548c\u672f\u8bed\uff08terms\uff09\u7b49\u5b58\u50a8\u6876\u805a\u5408\u3002 \u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/p>\n<\/p>\n

\u4ec0\u4e48\u662f\u6876\uff1f<\/h2>\n

\u9996\u5148\u4e3a\u4e86\u8bf4\u660e\u95ee\u9898\u7684\u65b9\u4fbf\uff0c\u6211\u4eec\u6765\u5c55\u793a\u4e00\u4e2a\u7b80\u5355\u7684 SpreadSheet \u8868\u683c\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n
id<\/th>\nuser<\/th>\nage<\/th>\ncountry<\/th>\ncategory<\/th>\n<\/tr>\n<\/thead>\n
1<\/td>\nBill<\/td>\n30<\/td>\nFR<\/td>\nA<\/td>\n<\/tr>\n
2<\/td>\nMarie<\/td>\n32<\/td>\nUS<\/td>\nA<\/td>\n<\/tr>\n
3<\/td>\nClarie<\/td>\n32<\/td>\nUS<\/td>\nA<\/td>\n<\/tr>\n
4<\/td>\nTom<\/td>\n44<\/td>\nDE<\/td>\nB<\/td>\n<\/tr>\n
5<\/td>\nJohn<\/td>\n40<\/td>\nUS<\/td>\nB<\/td>\n<\/tr>\n
6<\/td>\nEmma<\/td>\n26<\/td>\nUS<\/td>\nB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u4e0a\u9762\u7684\u8868\u683c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u5f88\u89c4\u6574\u7684\u5173\u4e8e\u7528\u6237\u7684\u540d\u5355\u3002\u6bcf\u4e2a\u7528\u6237\u5177\u6709 id\uff0cuser\uff0cage\uff0ccountry \u53ca category\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u88ab\u5b58\u4e8e\u5230 Elasticsearch \u4e2d\u540e\uff0c\u4f1a\u53d8\u6210\u4e00\u4e2a\u4e00\u4e2a\u7684\u6587\u6863\uff1a<\/p>\n

\"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

\u4e3a\u4e86\u628a\u8fd9\u4e9b\u6570\u636e\u5bfc\u5165\u5230 Elasticsearch \u4e4b\u4e2d\uff0c\u6211\u4eec\u4ee5\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u3002\u6211\u4eec\u5148\u521b\u5efa\u8fd9\u4e2a users \u7d22\u5f15\u3002<\/p>\n

PUT users\n{\n  \"mappings\": {\n    \"properties\": {\n      \"age\": {\n        \"type\": \"long\"\n      },\n      \"category\": {\n        \"type\": \"keyword\"\n      },\n      \"country\": {\n        \"type\": \"keyword\"\n      },\n      \"user\": {\n        \"type\": \"keyword\"\n      }\n    }\n  }\n}<\/code><\/pre>\n

\u4e0a\u9762\u521b\u5efa\u4e86\u4e00\u4e2a users \u7684\u7d22\u5f15\uff0c\u540c\u65f6\u4e5f\u5b9a\u4e49\u4e86\u5b83\u7684 mapping\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0bbulk API\u628a\u6570\u636e\u5bfc\u5165\uff1a<\/p>\n

POST _bulk\n{ \"index\" : { \"_index\" : \"users\", \"_id\": 1} }\n{\"user\":\"bill\", \"age\": 30, \"country\": \"FR\", \"category\": \"A\"}\n{ \"index\" : { \"_index\" : \"users\", \"_id\": 2} }\n{\"user\":\"Marie\", \"age\": 32, \"country\": \"US\", \"category\": \"A\"}\n{ \"index\" : { \"_index\" : \"users\", \"_id\": 3} }\n{\"user\":\"Clarie\", \"age\": 32, \"country\": \"US\", \"category\": \"A\"}\n{ \"index\" : { \"_index\" : \"users\", \"_id\": 4} }\n{\"user\":\"Tom\", \"age\": 44, \"country\": \"DE\", \"category\": \"B\"}\n{ \"index\" : { \"_index\" : \"users\", \"_id\": 5} }\n{\"user\":\"John\", \"age\": 40, \"country\": \"US\", \"category\": \"B\"}\n{ \"index\" : { \"_index\" : \"users\", \"_id\": 6} }\n{\"user\":\"Emma\", \"age\": 26, \"country\": \"US\", \"category\": \"B\"}<\/code><\/pre>\n

\u5047\u5982\u6211\u4eec\u60f3\u5f97\u5230\u8fd9\u4e9b\u4e2a\u7528\u6237\u7684\u5e73\u5747\u5e74\u9f84\uff0c\u6211\u4eec\u5f88\u5bb9\u6613\u901a\u8fc7 Elasticsearch \u7684 Avg aggregation \u6765\u5f97\u5230\u3002<\/p>\n

GET users\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"avg_of_age\": {\n      \"avg\": {\n        \"field\": \"age\"\n      }\n    }\n  }\n}<\/code><\/pre>\n

\"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

\u90a3\u4e48\u4ed6\u4eec\u7684\u5e73\u5747\u5e74\u9f84\u662f34\u5c81\u3002<\/p>\n

\u63a5\u4e0b\u6765\u6211\u4eec\u5f00\u59cb\u8c08\u6211\u4eec\u7684\u91cd\u70b9\u4e86\uff1aBucket aggregation\u3002<\/p>\n

\u7b80\u5355\u5730\u8bf4\uff1aBucket aggregation \u662f\u4e00\u79cd\u628a\u5177\u6709\u76f8\u540c\u6807\u51c6\u7684\u6570\u636e\u5206\u7ec4\u6570\u636e\u7684\u65b9\u6cd5\u3002\u521b\u5efa\u5b58\u50a8\u6876\uff1a<\/p>\n

    \n
  • \u6536\u96c6\u5177\u6709\u5171\u540c\u6807\u51c6\u7684\u6587\u4ef6<\/li>\n
  • \u53ef\u4ee5\u5177\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u4e0e\u5176\u5173\u8054\u7684\u6307\u6807<\/li>\n
  • bucket \u6bcf\u4e2a\u5b58\u50a8\u6876\u7684\u6587\u6863\u6570\uff08\u6587\u6863\u6570\uff09\u662f\u9ed8\u8ba4\u6307\u6807<\/li>\n<\/ul>\n

    \u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u7167 cetegory \u8fdb\u884c\u5206\u7c7b\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u6211\u4eec\u4ece\u4e0a\u9762\u7684\u8868\u683c\u53ef\u4ee5\u770b\u51fa\u6765 category A \u67093\u4e2a\u6587\u6863\uff0c\u800c category B \u67093\u4e2a\u6587\u6863\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u4e0a\u9762\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u6765\u67e5\u8be2\uff1a<\/p>\n

    GET users\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"categoeries\": {\n      \"terms\": {\n        \"field\": \"category\"\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u663e\u793a\u7ed3\u679c\u4e3a\uff1a<\/p>\n

      \"aggregations\" : {\n    \"categoeries\" : {\n      \"doc_count_error_upper_bound\" : 0,\n      \"sum_other_doc_count\" : 0,\n      \"buckets\" : [\n        {\n          \"key\" : \"A\",\n          \"doc_count\" : 3\n        },\n        {\n          \"key\" : \"B\",\n          \"doc_count\" : 3\n        }\n      ]\n    }<\/code><\/pre>\n

    \u6211\u4eec\u53ef\u4ee5\u5728 Kibana \u7684\u53ef\u89c6\u5316\u4e2d\u8fdb\u884c\u67e5\u770b\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u4e0a\u9762\u6211\u4eec\u5f97\u5230\u4e86\u6bcf\u4e2a category \u7684\u6570\u91cf\u662f\u6211\u4eec\u60f3\u8981\u7684\uff0c\u4f46\u662f\u5728\u5f88\u591a\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u66f4\u60f3\u5f97\u5230\u5728\u8fd9\u6bcf\u4e2a category \u4e0b\u7684\u4e00\u4e9b\u6307\u6807\uff0c\u6bd4\u5982\u6bcf\u4e2a category \u7684\u5e73\u5747\u5e74\u9f84\u662f\u591a\u5c11\uff1f\u8fd9\u6837\u6211\u5b9e\u9645\u4e0a\u662f\u5728\u4ee5 category \u4e3a key \u7684\u5b58\u50a8\u6876\u91cc\u6765\u6c42\u5e73\u5747\u503c\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u6cd5\u6765\u5f97\u5230\u8fd9\u4e2a\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u4e0a\u9762\u7684\u67e5\u8be2\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u4e2a category \u7684\u6876\u91cc\u6765\u505a\u5e73\u5747\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0 metrics \u6765\u5b9e\u9645\u3002<\/p>\n

    GET users\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"categoeries\": {\n      \"terms\": {\n        \"field\": \"category\"\n      },\n      \"aggs\": {\n        \"avg_age_per_category\": {\n          \"avg\": {\n            \"field\": \"age\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u4e0a\u9762\u7684\u663e\u793a\u7ed3\u679c\u4e3a\uff1a<\/p>\n

      \"aggregations\" : {\n    \"categoeries\" : {\n      \"doc_count_error_upper_bound\" : 0,\n      \"sum_other_doc_count\" : 0,\n      \"buckets\" : [\n        {\n          \"key\" : \"A\",\n          \"doc_count\" : 3,\n          \"avg_age_per_category\" : {\n            \"value\" : 31.333333333333332\n          }\n        },\n        {\n          \"key\" : \"B\",\n          \"doc_count\" : 3,\n          \"avg_age_per_category\" : {\n            \"value\" : 36.666666666666664\n          }\n        }\n      ]\n    }<\/code><\/pre>\n

    \u8fd9\u4e2a\u5728 Kibana \u4e2d\u53ef\u4ee5\u8868\u73b0\u4e3a\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u5728\u6bcf\u4e2a category \u4e0b\u7684\u5e73\u5747\u5e74\u9f84\u3002\u6211\u4eec\u53ef\u4ee5\u518d\u8fdb\u4e00\u6b65\u60f3\u5f97\u5230\u5728\u6bcf\u4e2a category \u4e0b\u7684\u6bcf\u4e2a\u56fd\u5bb6\u7684\u5e73\u5747\u5e74\u9f84\u3002\u663e\u7136\u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5230 country \u8fd9\u4e2a\u6876\uff0c\u8fd9\u6876\u662f\u5728 category \u6876\u4e0b\u9762\u7684\u53e6\u5916\u4e00\u4e2a\u6876\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u6dfb\u52a0\u5b50\u6876\u805a\u5408\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n<\/p>\n

    \u6700\u540e\u4e00\u6b65\u7684\u67e5\u8be2\uff1a<\/p>\n

    GET users\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"terms\": {\n        \"field\": \"category\"\n      },\n      \"aggs\": {\n        \"countries\": {\n          \"terms\": {\n            \"field\": \"country\"\n          },\n          \"aggs\": {\n            \"average_age\": {\n              \"avg\": {\n                \"field\": \"age\"\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u663e\u793a\u7ed3\u679c\uff1a<\/p>\n

      \"aggregations\" : {\n    \"categories\" : {\n      \"doc_count_error_upper_bound\" : 0,\n      \"sum_other_doc_count\" : 0,\n      \"buckets\" : [\n        {\n          \"key\" : \"A\",\n          \"doc_count\" : 3,\n          \"countries\" : {\n            \"doc_count_error_upper_bound\" : 0,\n            \"sum_other_doc_count\" : 0,\n            \"buckets\" : [\n              {\n                \"key\" : \"US\",\n                \"doc_count\" : 2,\n                \"average_age\" : {\n                  \"value\" : 32.0\n                }\n              },\n              {\n                \"key\" : \"FR\",\n                \"doc_count\" : 1,\n                \"average_age\" : {\n                  \"value\" : 30.0\n                }\n              }\n            ]\n          }\n        },\n        {\n          \"key\" : \"B\",\n          \"doc_count\" : 3,\n          \"countries\" : {\n            \"doc_count_error_upper_bound\" : 0,\n            \"sum_other_doc_count\" : 0,\n            \"buckets\" : [\n              {\n                \"key\" : \"US\",\n                \"doc_count\" : 2,\n                \"average_age\" : {\n                  \"value\" : 33.0\n                }\n              },\n              {\n                \"key\" : \"DE\",\n                \"doc_count\" : 1,\n                \"average_age\" : {\n                  \"value\" : 44.0\n                }\n              }\n            ]\n          }\n        }\n      ]\n    }<\/code><\/pre>\n

    \u5728 Kibana \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8868\u73b0\u4e3a\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3
    \u5728\u8fd9\u4e00\u8282\u91cc\uff0c\u6211\u4eec\u4e3b\u8981\u901a\u8fc7 terms aggregation \u6765\u5c55\u793a\u5b58\u50a8\u6876\u7684\u6982\u5ff5\u3002\u6211\u5728\u4e0b\u9762\u7528\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u8be6\u7ec6\u63cf\u8ff0\u66f4\u591a\u6876\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n

    \u51c6\u5907\u6570\u636e<\/h2>\n

    \u521b\u5efa\u7d22\u5f15<\/h3>\n

    \u4e3a\u4e86\u8bf4\u660e\u4ecb\u7ecd\u4e2d\u63d0\u5230\u7684\u5404\u79cd\u5b58\u50a8\u6876\u805a\u5408\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 \u201csports\u201d \u7d22\u5f15\uff0c\u8be5\u7d22\u5f15\u5b58\u50a8 \u201calthlete\u201d \u6587\u6863\u7684\u96c6\u5408\u3002 \u7d22\u5f15\u6620\u5c04\u5c06\u5305\u542b\u8bf8\u5982\u8fd0\u52a8\u5458\u7684\u4f4d\u7f6e\uff0c\u59d3\u540d\uff0c\u7b49\u7ea7\uff0c\u8fd0\u52a8\uff0c\u5e74\u9f84\uff0c\u8fdb\u7403\u6570\u548c\u573a\u4f4d\u7f6e\uff08\u4f8b\u5982\u9632\u5b88\u8005\uff09\u4e4b\u7c7b\u7684\u5b57\u6bb5\u3002 \u8ba9\u6211\u4eec\u521b\u5efa\u6620\u5c04\uff1a<\/p>\n

    PUT sports\n{\n  \"mappings\": {\n    \"properties\": {\n      \"birthdate\": {\n        \"type\": \"date\",\n        \"format\": \"dateOptionalTime\"\n      },\n      \"location\": {\n        \"type\": \"geo_point\"\n      },\n      \"name\": {\n        \"type\": \"keyword\"\n      },\n      \"rating\": {\n        \"type\": \"integer\"\n      },\n      \"sport\": {\n        \"type\": \"keyword\"\n      },\n      \"age\": {\n        \"type\": \"integer\"\n      },\n      \"goals\": {\n        \"type\": \"integer\"\n      },\n      \"role\": {\n        \"type\": \"keyword\"\n      },\n      \"score_weight\": {\n        \"type\": \"float\"\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u4e00\u65e6 mapping \u521b\u5efa\u6210\u529f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 Elasticsearch \u6240\u63d0\u4f9b\u7684 Bulk API \u6765\u628a\u6211\u4eec\u6570\u636e\u5bfc\u5165\u5230\u6211\u4eec\u7684\u7d22\u5f15\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a REST \u8c03\u7528\u5c31\u628a\u6240\u6709\u7684\u6570\u636e\u5bfc\u5165\u5230 Elasticsearch \u4e2d\u3002<\/p>\n

    POST sports\/_bulk\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Michael\",\"birthdate\":\"1989-10-1\",\"sport\":\"Football\",\"rating\":[\"5\",\"4\"],\"location\":\"46.22,-68.45\",\"age\":\"23\",\"goals\":\"43\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Bob\",\"birthdate\":\"1989-11-2\",\"sport\":\"Football\",\"rating\":[\"3\",\"4\"],\"location\":\"45.21,-68.35\",\"age\":\"33\",\"goals\":\"54\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Jim\",\"birthdate\":\"1988-10-3\",\"sport\":\"Football\",\"rating\":[\"3\",\"2\"],\"location\":\"45.16,-63.58\",\"age\":\"28\",\"goals\":\"73\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Joe\",\"birthdate\":\"1992-5-20\",\"sport\":\"Basketball\",\"rating\":[\"4\",\"3\"],\"location\":\"45.22,-68.53\",\"age\":\"18\",\"goals\":\"848\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Tim\",\"birthdate\":\"1992-2-28\",\"sport\":\"Basketball\",\"rating\":[\"3\",\"3\"],\"location\":\"46.22,-68.85\",\"age\":\"28\",\"goals\":\"942\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Alfred\",\"birthdate\":\"1990-9-9\",\"sport\":\"Football\",\"rating\":[\"2\",\"2\"],\"location\":\"45.12,-68.35\",\"age\":\"25\",\"goals\":\"53\",\"score_weight\":\"4\",\"role\":\"defender\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Jeff\",\"birthdate\":\"1990-4-1\",\"sport\":\"Hockey\",\"rating\":[\"2\",\"3\"],\"location\":\"46.12,-68.55\",\"age\":\"26\",\"goals\":\"93\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Will\",\"birthdate\":\"1988-3-1\",\"sport\":\"Hockey\",\"rating\":[\"4\",\"4\"],\"location\":\"46.25,-84.25\",\"age\":\"27\",\"goals\":\"124\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Mick\",\"birthdate\":\"1989-10-1\",\"sport\":\"Football\",\"rating\":[\"3\",\"4\"],\"location\":\"46.22,-68.45\",\"age\":\"35\",\"goals\":\"56\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Pong\",\"birthdate\":\"1989-11-2\",\"sport\":\"Basketball\",\"rating\":[\"1\",\"3\"],\"location\":\"45.21,-68.35\",\"age\":\"34\",\"goals\":\"1483\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Ray\",\"birthdate\":\"1988-10-3\",\"sport\":\"Football\",\"rating\":[\"2\",\"2\"],\"location\":\"45.16,-63.58\",\"age\":\"31\",\"goals\":\"84\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Ping\",\"birthdate\":\"1992-5-20\",\"sport\":\"Basketball\",\"rating\":[\"4\",\"3\"],\"location\":\"45.22,-68.53\",\"age\":\"27\",\"goals\":\"1328\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Duke\",\"birthdate\":\"1992-2-28\",\"sport\":\"Hockey\",\"rating\":[\"5\",\"2\"],\"location\":\"46.22,-68.85\",\"age\":\"41\",\"goals\":\"218\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Hal\",\"birthdate\":\"1990-9-9\",\"sport\":\"Hockey\",\"rating\":[\"4\",\"2\"],\"location\":\"45.12,-68.35\",\"age\":\"18\",\"goals\":\"148\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Charge\",\"birthdate\":\"1990-4-1\",\"sport\":\"Football\",\"rating\":[\"3\",\"2\"],\"location\":\"44.19,-82.55\",\"age\":\"19\",\"goals\":\"34\",\"score_weight\":\"4\",\"role\":\"defender\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Barry\",\"birthdate\":\"1988-3-1\",\"sport\":\"Football\",\"rating\":[\"5\",\"2\"],\"location\":\"36.45,-79.15\",\"age\":\"20\",\"goals\":\"48\",\"score_weight\":\"4\",\"role\":\"defender\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Bank\",\"birthdate\":\"1988-3-1\",\"sport\":\"Handball\",\"rating\":[\"6\",\"4\"],\"location\":\"46.25,-54.53\",\"age\":\"25\",\"goals\":\"150\",\"score_weight\":\"4\",\"role\":\"defender\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Bingo\",\"birthdate\":\"1988-3-1\",\"sport\":\"Handball\",\"rating\":[\"10\",\"7\"],\"location\":\"46.25,-68.55\",\"age\":\"29\",\"goals\":\"143\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"James\",\"birthdate\":\"1988-3-1\",\"sport\":\"Basketball\",\"rating\":[\"10\",\"8\"],\"location\":\"41.25,-69.55\",\"age\":\"36\",\"goals\":\"1284\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Wayne\",\"birthdate\":\"1988-3-1\",\"sport\":\"Hockey\",\"rating\":[\"10\",\"10\"],\"location\":\"46.21,-68.55\",\"age\":\"25\",\"goals\":\"113\",\"score_weight\":\"3\",\"role\":\"midfielder\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Brady\",\"birthdate\":\"1988-3-1\",\"sport\":\"Handball\",\"rating\":[\"10\",\"10\"],\"location\":\"63.24,-84.55\",\"age\":\"29\",\"goals\":\"443\",\"score_weight\":\"2\",\"role\":\"forward\"}\n{\"index\":{\"_index\":\"sports\"}}\n{\"name\":\"Lewis\",\"birthdate\":\"1988-3-1\",\"sport\":\"Football\",\"rating\":[\"10\",\"10\"],\"location\":\"56.25,-74.55\",\"age\":\"24\",\"goals\":\"49\",\"score_weight\":\"3\",\"role\":\"midfielder\"}<\/code><\/pre>\n

    \u8fd9\u6837\u6211\u4eec\u7684\u6570\u636e\u5c31\u5f55\u5165\u5230 Elasticsearch \u4e2d\u4e86\u3002\u5728\u4e0b\u9762\uff0c\u6211\u4eec\u5c31\u7528\u4e0d\u540c\u7684\u5b58\u50a8\u6876\u6765\u5bf9\u6211\u4eec\u7684\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<\/p>\n

    Filter(s) Aggregations<\/h2>\n

    \u6876\u805a\u5408\u652f\u6301\u5355\u8fc7\u6ee4\u5668\u805a\u5408\u548c\u591a\u8fc7\u6ee4\u5668\u805a\u5408\u3002<\/p>\n

    \u5355\u4e2a\u8fc7\u6ee4\u5668\u805a\u5408\u6839\u636e\u4e0e\u8fc7\u6ee4\u5668\u5b9a\u4e49\u4e2d\u6307\u5b9a\u7684\u67e5\u8be2\u6216\u5b57\u6bb5\u503c\u5339\u914d\u7684\u6240\u6709\u6587\u6863\u6784\u9020\u5355\u4e2a\u5b58\u50a8\u6876\u3002 \u5f53\u4f60\u8981\u6807\u8bc6\u4e00\u7ec4\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u6587\u6863\u65f6\uff0c\u5355\u8fc7\u6ee4\u5668\u805a\u5408\u5f88\u6709\u7528\u3002<\/p>\n

    \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5355\u8fc7\u6ee4\u5668\u805a\u5408\u6765\u67e5\u627e\u6240\u6709\u5177\u6709 \u201cdefender\u201d \u89d2\u8272\u7684\u8fd0\u52a8\u5458\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u8fc7\u6ee4\u6876\u7684\u5e73\u5747\u76ee\u6807\u3002 \u8fc7\u6ee4\u5668\u914d\u7f6e\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    POST sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"defender_filter\": {\n      \"filter\": {\n        \"term\": {\n          \"role\": \"defender\"\n        }\n      },\n      \"aggs\": {\n        \"avg_goals\": {\n          \"avg\": {\n            \"field\": \"goals\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u6211\u4eec\u770b\u5230\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n

      \"aggregations\" : {\n    \"defender_filter\" : {\n      \"doc_count\" : 4,\n      \"avg_goals\" : {\n        \"value\" : 71.25\n      }\n    }\n  }<\/code><\/pre>\n

    \u5982\u4f60\u6240\u89c1\uff0cfilter \u805a\u5408\u5305\u542b\u4e00\u4e2a \u201cterm\u201d \u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u6307\u5b9a\u6587\u6863\u4e2d\u7684\u5b57\u6bb5\u4ee5\u641c\u7d22\u7279\u5b9a\u503c\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a \u201cdefender\u201d\uff09\u3002 Elasticsearch \u5c06\u904d\u5386\u6240\u6709\u6587\u6863\uff0c\u5e76\u68c0\u67e5 \u201crole\u201d \u5b57\u6bb5\u4e2d\u662f\u5426\u5305\u542b \u201cdefender\u201d\u3002 \u7136\u540e\u5c06\u4e0e\u8be5\u503c\u5339\u914d\u7684\u6587\u6863\u6dfb\u52a0\u5230\u805a\u5408\u751f\u6210\u7684\u5355\u4e2a\u5b58\u50a8\u6876\u4e2d\u3002\u6b64\u8f93\u51fa\u8868\u660e\u6211\u4eec\u96c6\u5408\u4e2d\u6240\u6709\u540e\u536b\u7684\u5e73\u5747\u8fdb\u7403\u6570\u4e3a 71.25\u3002<\/p>\n

    \u8fd9\u662f\u5355\u8fc7\u6ee4\u5668\u805a\u5408\u7684\u793a\u4f8b\u3002 \u4f46\u662f\uff0c\u5728 Elasticsearch \u4e2d\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528 filter \u805a\u5408\u6307\u5b9a\u591a\u4e2a\u8fc7\u6ee4\u5668\u3002 \u8fd9\u662f\u4e00\u4e2a\u591a\u503c\u805a\u5408\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b58\u50a8\u6876\u90fd\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u8fc7\u6ee4\u5668\u3002 \u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u4e0a\u9762\u7684\u793a\u4f8b\u4ee5\u8fc7\u6ee4 defender \u548c forward\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0, \n  \"aggs\": {\n    \"athletes\": {\n      \"filters\": {\n        \"filters\": {\n          \"defenders\": {\n            \"term\": {\n              \"role\": \"defender\"\n            }\n          },\n          \"forwards\": {\n            \"term\": {\n              \"role\": \"forward\"\n            }\n          }\n        }\n      },\n      \"aggs\": {\n        \"avg_goals\": {\n          \"avg\": {\n            \"field\": \"goals\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u663e\u793a\u7ed3\u679c\u4e3a\uff1a<\/p>\n

      \"aggregations\" : {\n    \"athletes\" : {\n      \"buckets\" : {\n        \"defenders\" : {\n          \"doc_count\" : 4,\n          \"avg_goals\" : {\n            \"value\" : 71.25\n          }\n        },\n        \"forwards\" : {\n          \"doc_count\" : 9,\n          \"avg_goals\" : {\n            \"value\" : 661.0\n          }\n        }\n      }\n    }\n  }<\/code><\/pre>\n

    \u6211\u4eec\u53ef\u4ee5\u770b\u51fa\u6765\u6211\u4eec\u540c\u65f6\u6709\u4e24\u4e2a\u6876\uff0c\u5206\u522b\u5bf9\u5e94\u6211\u4eec\u7684\u4e24\u4e2a filter\u3002\u6bcf\u4e00\u4e2a filter \u90fd\u68c0\u67e5 role \u7684\u503c\u4e3a defender \u6216\u8005 forward\u3002<\/p>\n

    \u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5728 Kibana \u4e2d\u5c55\u793a\u8fd9\u4e24\u4e2a\u6876\u7684\u6570\u636e\u3002\u4e3a\u4e86\u80fd\u591f\u5728 Kibana \u4e2d\u4f7f\u7528\u6211\u4eec\u7684\u6570\u636e\uff0c\u6211\u4eec\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a index pattern\u3002\u5982\u679c\u4f60\u8fd8\u4e0d\u4e86\u89e3\u8fd9\u4e2a\uff0c\u8bf7\u53c2\u9605\u6211\u4e4b\u524d\u7684\u6587\u7ae0 \u201cKibana: \u5982\u4f55\u4f7f\u7528Search Bar\u201d\u3002\u5728\u6211\u4eec\u5bfc\u5165\u6570\u636e\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9009\u62e9 birthdate \u5b57\u6bb5\u4f5c\u4e3a\u65f6\u95f4\u7cfb\u5217\u7684 timestamp\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u201cgoals\u201d \u5b57\u6bb5\u4e0a\u7684\u5e73\u5747\u5b50\u805a\u5408\u662f\u5728Y\u8f74\u4e0a\u5b9a\u4e49\u7684\u3002 \u5728 X \u8f74\u4e0a\uff0c\u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u8fc7\u6ee4\u5668\uff0c\u5e76\u4e3a\u5b83\u4eec\u6307\u5b9a \u201cdefender\u201d \u548c \u201cforward\u201d \u503c\u3002 \u7531\u4e8e\u5e73\u5747\u6307\u6807\u662f\u8fc7\u6ee4\u5668\u805a\u5408\u7684\u5b50\u805a\u5408\uff0c\u56e0\u6b64 Elasticsearch \u5c06\u521b\u5efa\u7684\u8fc7\u6ee4\u5668\u5e94\u7528\u4e8e \u201cgoals\u201d \u5b57\u6bb5\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u9700\u660e\u786e\u6307\u5b9a\u8be5\u5b57\u6bb5\u3002<\/p>\n<\/p>\n

    Terms Aggregation<\/h2>\n

    \u4e5f\u8bb8\u6211\u4eec\u5bf9 Terms aggregation \u5e76\u4e0d\u964c\u751f\u3002\u6211\u4eec\u521a\u624d\u5728\u4e00\u5f00\u59cb\u5df2\u7ecf\u4f7f\u7528\u4e86 terms aggregation\u3002<\/p>\n

    \u672f\u8bed\u805a\u5408\u4f1a\u5728\u6587\u6863\u7684\u6307\u5b9a\u5b57\u6bb5\u4e2d\u641c\u7d22\u552f\u4e00\u503c\uff0c\u5e76\u4e3a\u627e\u5230\u7684\u6bcf\u4e2a\u552f\u4e00\u503c\u6784\u5efa\u5b58\u50a8\u6876\u3002 \u4e0e\u8fc7\u6ee4\u5668\u805a\u5408\u4e0d\u540c\uff0c\u672f\u8bed\u805a\u5408\u7684\u4efb\u52a1\u4e0d\u662f\u5c06\u7ed3\u679c\u9650\u5236\u4e3a\u7279\u5b9a\u503c\uff0c\u800c\u662f\u67e5\u627e\u6587\u6863\u4e2d\u7ed9\u5b9a\u5b57\u6bb5\u7684\u6240\u6709\u552f\u4e00\u503c\u3002<\/p>\n

    \u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u8bd5\u56fe\u4e3a \u201csport\u201d \u5b57\u6bb5\u4e2d\u627e\u5230\u7684\u6bcf\u4e2a\u552f\u4e00\u503c\u521b\u5efa\u4e00\u4e2a\u5b58\u50a8\u6876\u3002 \u8fd9\u9879\u64cd\u4f5c\u7684\u7ed3\u679c\u662f\uff0c\u6211\u4eec\u5c06\u4e3a\u7d22\u5f15\u4e2d\u7684\u6bcf\u79cd\u8fd0\u52a8\u63d0\u4f9b\u56db\u4e2a\u72ec\u7279\u7684\u5b58\u50a8\u6876\uff1aFootball\uff0cHandball\uff0cHockey \u548c Basketbalk\u3002 \u7136\u540e\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5e73\u5747\u5b50\u805a\u5408\u8ba1\u7b97\u6bcf\u79cd\u8fd0\u52a8\u7684\u5e73\u5747\u76ee\u6807\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"sports\": {\n      \"terms\": {\n        \"field\": \"sport\"\n      },\n      \"aggs\": {\n        \"avg_scoring\": {\n          \"avg\": {\n            \"field\": \"goals\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u8fd4\u56de\u6570\u636e\u4e3a\uff1a<\/p>\n

      \"aggregations\" : {\n    \"sports\" : {\n      \"doc_count_error_upper_bound\" : 0,\n      \"sum_other_doc_count\" : 0,\n      \"buckets\" : [\n        {\n          \"key\" : \"Football\",\n          \"doc_count\" : 9,\n          \"avg_scoring\" : {\n            \"value\" : 54.888888888888886\n          }\n        },\n        {\n          \"key\" : \"Basketball\",\n          \"doc_count\" : 5,\n          \"avg_scoring\" : {\n            \"value\" : 1177.0\n          }\n        },\n        {\n          \"key\" : \"Hockey\",\n          \"doc_count\" : 5,\n          \"avg_scoring\" : {\n            \"value\" : 139.2\n          }\n        },\n        {\n          \"key\" : \"Handball\",\n          \"doc_count\" : 3,\n          \"avg_scoring\" : {\n            \"value\" : 245.33333333333334\n          }\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u672f\u8bed\u805a\u5408\u4e3a\u7d22\u5f15\u4e2d\u7684\u6bcf\u79cd\u8fd0\u52a8\u7c7b\u578b\u6784\u9020\u4e86\u56db\u4e2a\u5b58\u50a8\u6876\u3002 \u6bcf\u4e2a\u5b58\u50a8\u6876\u5305\u542b doc_count\uff08\u5c5e\u4e8e\u5b58\u50a8\u6876\u7684\u6587\u6863\u6570\uff09\u548c\u6bcf\u4e2a\u8fd0\u52a8\u7684\u5e73\u5747\u5b50\u805a\u5408\u3002<\/p>\n

    \u8ba9\u6211\u4eec\u5728 Kibana \u4e2d\u53ef\u89c6\u5316\u8fd9\u4e9b\u7ed3\u679c\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n<\/p>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u5728Y\u8f74\u4e0a\uff0c\u6211\u4eec\u5728 \u201cgoals\u201d \u5b57\u6bb5\u4e0a\u4f7f\u7528\u5e73\u5747\u5b50\u805a\u5408\uff0c\u5728X\u8f74\u4e0a\uff0c\u6211\u4eec\u5728 \u201csport\u201d \u5b57\u6bb5\u4e0a\u5b9a\u4e49\u4e86\u672f\u8bed\u6876\u805a\u5408\u3002<\/p>\n

    <\/h2>\n

    Histogram Aggregation<\/h2>\n

    \u76f4\u65b9\u56fe\u805a\u5408\u4f7f\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6784\u9020\u5b58\u50a8\u6876\u3002 \u5c5e\u4e8e\u6bcf\u4e2a\u95f4\u9694\u7684\u503c\u5c06\u5f62\u6210\u4e00\u4e2a\u95f4\u9694\u5b58\u50a8\u6876\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u4f7f\u75285\u5e74\u95f4\u9694\u5c06\u76f4\u65b9\u56fe\u805a\u5408\u5e94\u7528\u4e8e \u201cage\u201d \u5b57\u6bb5\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u76f4\u65b9\u56fe\u805a\u5408\u5c06\u5728\u6211\u4eec\u7684\u6587\u6863\u96c6\u4e2d\u627e\u5230\u6700\u5c0f\u548c\u6700\u5927\u5e74\u9f84\uff0c\u5e76\u5c06\u6bcf\u4e2a\u6587\u6863\u4e0e\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u76f8\u5173\u8054\u3002 \u6bcf\u4e2a\u6587\u6863\u7684 \u201cage\u201d \u5b57\u6bb5\u5c06\u5411\u4e0b\u820d\u5165\u5230\u6700\u63a5\u8fd1\u7684\u65f6\u95f4\u95f4\u9694\u5b58\u50a8\u6876\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7684\u65f6\u95f4\u95f4\u9694\u503c\u4e3a5\uff0c\u5b58\u50a8\u5206\u533a\u5927\u5c0f\u4e3a6\uff0c\u5219\u5e74\u9f8432\u4f1a\u56db\u820d\u4e94\u5165\u4e3a30\u3002<\/p>\n

    \u76f4\u65b9\u56fe\u805a\u5408\u7684\u516c\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    bucket_key = Math.floor((value - offset) \/ interval) * interval + offset<\/code><\/pre>\n

    \u8bf7\u6ce8\u610f\uff0c\u65f6\u95f4\u95f4\u9694\u5fc5\u987b\u4e3a\u6b63\u5341\u8fdb\u5236\u6570\uff0c\u800c\u504f\u79fb\u91cf\u5fc5\u987b\u4e3a [0\uff0coffset] \u8303\u56f4\u5185\u7684\u5341\u8fdb\u5236\u3002<\/p>\n

    \u8ba9\u6211\u4eec\u4f7f\u7528\u76f4\u65b9\u56fe\u805a\u5408\u6765\u751f\u6210\u7bee\u7403\u4e2d\u76ee\u6807\u95f4\u9694\u4e3a200\u7684\u5b58\u50a8\u6876\u3002<\/p>\n

    POST sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"baskketball_filter\": {\n      \"filter\": {\n        \"term\": {\n          \"sport\": \"Basketball\"\n        }\n      },\n      \"aggs\": {\n        \"goals_histogram\": {\n          \"histogram\": {\n            \"field\": \"goals\",\n            \"interval\": 200\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u8fd4\u56de\u7ed3\u679c\uff1a<\/p>\n

      \"aggregations\" : {\n    \"baskketball_filter\" : {\n      \"doc_count\" : 5,\n      \"goals_histogram\" : {\n        \"buckets\" : [\n          {\n            \"key\" : 800.0,\n            \"doc_count\" : 2\n          },\n          {\n            \"key\" : 1000.0,\n            \"doc_count\" : 0\n          },\n          {\n            \"key\" : 1200.0,\n            \"doc_count\" : 2\n          },\n          {\n            \"key\" : 1400.0,\n            \"doc_count\" : 1\n          }\n        ]\n      }\n    }\n  }<\/code><\/pre>\n

    \u4e0a\u9762\u7684\u56de\u7b54\u8868\u660e\uff0c\u6ca1\u6709\u76ee\u6807\u843d\u57280-200\u3001200-400\u3001400-600\u548c600-800\u533a\u95f4\u5185\u3002 \u56e0\u6b64\uff0c\u7b2c\u4e00\u4e2a\u5b58\u50a8\u533a\u4ece800-1000\u95f4\u9694\u5f00\u59cb\u3002 \u56e0\u6b64\uff0c\u503c\u6700\u5c0f\u7684\u6587\u6863\u5c06\u786e\u5b9a\u6700\u5c0f\u5b58\u50a8\u6876\uff08\u6700\u5c0fkey\u7684\u5b58\u50a8\u6876\uff09\u3002 \u76f8\u5e94\u5730\uff0c\u5177\u6709\u6700\u9ad8\u503c\u7684\u6587\u6863\u5c06\u786e\u5b9a\u6700\u5927\u5b58\u50a8\u6876\uff08\u5177\u6709\u6700\u9ad8key\u7684\u5b58\u50a8\u6876\uff09\u3002<\/p>\n

    \u6b64\u5916\uff0c\u8be5\u54cd\u5e94\u8fd8\u663e\u793a\u6709\u96f6\u4e2a\u6587\u6863\u843d\u5728[1000\uff0c1200\uff09\u8303\u56f4\u5185\u3002 \u8fd9\u610f\u5473\u7740\u6ca1\u6709\u8fd0\u52a8\u5458\u5f97\u5206\u57281000\u52301200\u4e2a\u76ee\u6807\u4e4b\u95f4\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cElasticsearch \u7528\u7a7a\u5b58\u50a8\u6876\u586b\u5145\u6b64\u7c7b\u7a7a\u767d\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528 min_doc_count \u8bbe\u7f6e\u901a\u8fc7\u8bf7\u6c42\u6700\u5c0f\u8ba1\u6570\u4e0d\u4e3a\u96f6\u7684\u5b58\u50a8\u6876\u6765\u66f4\u6539\u6b64\u884c\u4e3a\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5c06 min_doc_count \u7684\u503c\u8bbe\u7f6e\u4e3a1\uff0c\u5219\u76f4\u65b9\u56fe\u5c06\u4ec5\u9488\u5bf9\u5176\u4e2d\u5305\u542b\u4e0d\u5c11\u4e8e1\u4e2a\u6587\u6863\u7684\u95f4\u9694\u6784\u9020\u5b58\u50a8\u6876\u3002 \u8ba9\u6211\u4eec\u4fee\u6539\u67e5\u8be2\uff0c\u5c06 min_doc_count \u8bbe\u7f6e\u4e3a1\u3002<\/p>\n

    POST sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"baskketball_filter\": {\n      \"filter\": {\n        \"term\": {\n          \"sport\": \"Basketball\"\n        }\n      },\n      \"aggs\": {\n        \"goals_histogram\": {\n          \"histogram\": {\n            \"field\": \"goals\",\n            \"interval\": 200,\n            \"min_doc_count\": 1\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u90a3\u4e48\u7ecf\u8fc7\u8fd9\u6837\u4fee\u6539\u540e\u7684\u8fd4\u56de\u7ed3\u679c\u662f\uff1a<\/p>\n

      \"aggregations\" : {\n    \"baskketball_filter\" : {\n      \"doc_count\" : 5,\n      \"goals_histogram\" : {\n        \"buckets\" : [\n          {\n            \"key\" : 800.0,\n            \"doc_count\" : 2\n          },\n          {\n            \"key\" : 1200.0,\n            \"doc_count\" : 2\n          },\n          {\n            \"key\" : 1400.0,\n            \"doc_count\" : 1\n          }\n        ]\n      }\n    }\n  }<\/code><\/pre>\n

    \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 extended_bounds \u8bbe\u7f6e\u6765\u5f3a\u5236\u76f4\u65b9\u56fe\u805a\u5408\uff0c\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u6700\u5c0f\u503c\u5f00\u59cb\u6784\u5efa\u5176\u5b58\u50a8\u6876\uff0c\u5e76\u7ee7\u7eed\u6784\u5efa\u5b58\u50a8\u6876\u76f4\u81f3\u8fbe\u5230\u6700\u5927\u503c\uff08\u5373\u4f7f\u4e0d\u518d\u6709\u6587\u6863\uff09\u3002 \u4ec5\u5f53\u5c06 min_doc_count \u8bbe\u7f6e\u4e3a0\u65f6\uff0c\u624d\u4f7f\u7528 extended_bounds\u3002\uff08\u5982\u679c min_doc_count \u5927\u4e8e0\uff0c\u5219\u4e0d\u4f1a\u8fd4\u56de\u7a7a\u5b58\u50a8\u6876\uff09\u3002 \u770b\u4e00\u4e0b\u8fd9\u4e2a\u67e5\u8be2\uff1a<\/p>\n

    POST sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"baskketball_filter\": {\n      \"filter\": {\n        \"term\": {\n          \"sport\": \"Basketball\"\n        }\n      },\n      \"aggs\": {\n        \"goals_histogram\": {\n          \"histogram\": {\n            \"field\": \"goals\",\n            \"interval\": 200,\n            \"min_doc_count\": 0,\n            \"extended_bounds\": {\n              \"min\": 0,\n              \"max\": 1600\n            }\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4e3a\u5b58\u50a8\u6876\u6307\u5b9a\u4e860\uff08\u6700\u5c0f\u503c\uff09\u548c1600\uff08\u6700\u5927\u503c\uff09\u3002 \u56e0\u6b64\uff0c\u54cd\u5e94\u5e94\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

      \"aggregations\" : {\n    \"baskketball_filter\" : {\n      \"doc_count\" : 5,\n      \"goals_histogram\" : {\n        \"buckets\" : [\n          {\n            \"key\" : 0.0,\n            \"doc_count\" : 0\n          },\n          {\n            \"key\" : 200.0,\n            \"doc_count\" : 0\n          },\n          {\n            \"key\" : 400.0,\n            \"doc_count\" : 0\n          },\n          {\n            \"key\" : 600.0,\n            \"doc_count\" : 0\n          },\n          {\n            \"key\" : 800.0,\n            \"doc_count\" : 2\n          },\n          {\n            \"key\" : 1000.0,\n            \"doc_count\" : 0\n          },\n          {\n            \"key\" : 1200.0,\n            \"doc_count\" : 2\n          },\n          {\n            \"key\" : 1400.0,\n            \"doc_count\" : 1\n          },\n          {\n            \"key\" : 1600.0,\n            \"doc_count\" : 0\n          }\n        ]\n      }\n    }\n  }<\/code><\/pre>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u5373\u4f7f\u7b2c\u4e00\u4e2a\u5b58\u50a8\u6876\u548c\u6700\u540e\u4e00\u4e2a\u5b58\u50a8\u6876\u6839\u672c\u6ca1\u6709\u4efb\u4f55\u503c\uff0c\u4e5f\u4f1a\u751f\u6210\u4ece0\u5230\u7ed3\u675f1600\u7684\u6240\u6709\u5b58\u50a8\u6876\u3002<\/p>\n<\/p>\n

    Date histogram aggregation<\/h2>\n

    \u8fd9\u4e2a\u805a\u5408\u7c7b\u4f3c\u4e8e\u6b63\u5e38\u7684\u76f4\u65b9\u56fe\uff0c\u4f46\u53ea\u80fd\u4e0e\u65e5\u671f\u6216\u65e5\u671f\u8303\u56f4\u503c\u4e00\u8d77\u4f7f\u7528\u3002 \u7531\u4e8e\u65e5\u671f\u5728 Elasticsearch \u4e2d\u5185\u90e8\u4ee5\u957f\u503c\u8868\u793a\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4f46\u4e0d\u51c6\u786e\u5730\u5bf9\u65e5\u671f\u4f7f\u7528\u6b63\u5e38\u7684\u76f4\u65b9\u56fe\u3002 \u8fd9\u4e24\u4e2a API \u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e5\u671f\/\u65f6\u95f4\u8868\u8fbe\u5f0f\u6307\u5b9a\u95f4\u9694\u3002 \u57fa\u4e8e\u65f6\u95f4\u7684\u6570\u636e\u9700\u8981\u7279\u6b8a\u7684\u652f\u6301\uff0c\u56e0\u4e3a\u57fa\u4e8e\u65f6\u95f4\u7684\u95f4\u9694\u5e76\u4e0d\u603b\u662f\u56fa\u5b9a\u7684\u957f\u5ea6\u3002<\/p>\n

    \u5728\u6211\u4eec\u7684\u6570\u636e\u4e2d\u6709\u4e00\u4e2a\u53eb\u505a birthdate \u7684\u5b57\u6bb5\u3002\u5728\u5efa\u7acb\u6211\u4eec\u7684 index pattern \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u8fd9\u4e2a\u5b57\u6bb5\u4f5c\u4e3a time series \u7684timestamp\u3002\u4e0b\u9762\u662f\u6211\u4eec\u7684\u6570\u636e\u7684\u65f6\u5e8f\u56fe\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 date histogram aggregation \u6765\u7edf\u8ba1\u6211\u4eec\u7684\u8fd0\u52a8\u5458\u51fa\u751f\u5e74\u6708\u5206\u522b\u60c5\u51b5\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"birthdays\": {\n      \"date_histogram\": {\n        \"field\": \"birthdate\",\n        \"interval\": \"year\"\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u8fd4\u56de\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n

      \"aggregations\" : {\n    \"birthdays\" : {\n      \"buckets\" : [\n        {\n          \"key_as_string\" : \"1988-01-01T00:00:00.000Z\",\n          \"key\" : 567993600000,\n          \"doc_count\" : 10\n        },\n        {\n          \"key_as_string\" : \"1989-01-01T00:00:00.000Z\",\n          \"key\" : 599616000000,\n          \"doc_count\" : 4\n        },\n        {\n          \"key_as_string\" : \"1990-01-01T00:00:00.000Z\",\n          \"key\" : 631152000000,\n          \"doc_count\" : 4\n        },\n        {\n          \"key_as_string\" : \"1991-01-01T00:00:00.000Z\",\n          \"key\" : 662688000000,\n          \"doc_count\" : 0\n        },\n        {\n          \"key_as_string\" : \"1992-01-01T00:00:00.000Z\",\n          \"key\" : 694224000000,\n          \"doc_count\" : 4\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u4e0a\u9762\u7684\u7ed3\u679c\u663e\u793a\u57281988\u5e74\u52301998\u5e74\u4e4b\u95f4\u7684\u8fd0\u52a8\u5458\u670910\u4e2a\uff0c\u57281989\u548c1990\u5e74\u4e4b\u95f4\u7684\u67094\u4f4d\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728 Kibana \u4e2d\u8868\u793a\u51fa\u6765\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    \u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u9488\u5bf9\u6bcf\u4e2a\u5e74\u9f84\u6bb5\u7684 goals \u7684\u5e73\u5747\u503c\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"birthdays\": {\n      \"date_histogram\": {\n        \"field\": \"birthdate\",\n        \"interval\": \"year\"\n      },\n      \"aggs\": {\n        \"average_goals\": {\n          \"avg\": {\n            \"field\": \"goals\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u8fd4\u56de\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n

      \"aggregations\" : {\n    \"birthdays\" : {\n      \"buckets\" : [\n        {\n          \"key_as_string\" : \"1988-01-01T00:00:00.000Z\",\n          \"key\" : 567993600000,\n          \"doc_count\" : 10,\n          \"average_goals\" : {\n            \"value\" : 251.1\n          }\n        },\n        {\n          \"key_as_string\" : \"1989-01-01T00:00:00.000Z\",\n          \"key\" : 599616000000,\n          \"doc_count\" : 4,\n          \"average_goals\" : {\n            \"value\" : 409.0\n          }\n        },\n        {\n          \"key_as_string\" : \"1990-01-01T00:00:00.000Z\",\n          \"key\" : 631152000000,\n          \"doc_count\" : 4,\n          \"average_goals\" : {\n            \"value\" : 82.0\n          }\n        },\n        {\n          \"key_as_string\" : \"1991-01-01T00:00:00.000Z\",\n          \"key\" : 662688000000,\n          \"doc_count\" : 0,\n          \"average_goals\" : {\n            \"value\" : null\n          }\n        },\n        {\n          \"key_as_string\" : \"1992-01-01T00:00:00.000Z\",\n          \"key\" : 694224000000,\n          \"doc_count\" : 4,\n          \"average_goals\" : {\n            \"value\" : 834.0\n          }\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u57281992\u5e74\u7684\u8fd9\u4e2a\u5e74\u9f84\u6bb5\u7684 average_goals \u662f\u6700\u9ad8\u7684\uff0c\u8fbe\u5230834\u3002\u4e5f\u8bb8\u662f\u540e\u751f\u53ef\u754f\u554a\uff01\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728 Kibana \u4e2d\u8868\u793a\u51fa\u6765\uff1a<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n<\/p>\n

    Range Aggregation<\/h2>\n

    \u901a\u8fc7\u6b64\u5b58\u50a8\u6876\u805a\u5408\uff0c\u53ef\u4ee5\u8f7b\u677e\u6839\u636e\u7528\u6237\u5b9a\u4e49\u7684\u8303\u56f4\u6784\u5efa\u5b58\u50a8\u6876\u3002 Elasticsearch \u5c06\u68c0\u67e5\u4ece\u4f60\u6307\u5b9a\u7684\u6570\u5b57\u5b57\u6bb5\u4e2d\u63d0\u53d6\u7684\u6bcf\u4e2a\u503c\uff0c\u5e76\u5c06\u5176\u4e0e\u8303\u56f4\u8fdb\u884c\u6bd4\u8f83\uff0c\u7136\u540e\u5c06\u8be5\u503c\u653e\u5165\u76f8\u5e94\u7684\u8303\u56f4\u3002 \u8bf7\u6ce8\u610f\uff0c\u6b64\u805a\u5408\u5305\u62ec\u8d77\u59cb\u503c\uff0c\u4f46\u4e0d\u5305\u62ec\u6bcf\u4e2a\u8303\u56f4\u7684\u8d77\u59cb\u503c\u3002<\/p>\n

    \u8ba9\u6211\u4eec\u4e3a\u8fd0\u52a8\u7d22\u5f15\u4e2d\u7684 \u201cage\u201d \u5b57\u6bb5\u521b\u5efa\u4e00\u4e2a\u8303\u56f4\u6c47\u603b\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"goal_ranges\": {\n      \"range\": {\n        \"field\": \"age\",\n        \"ranges\": [\n          {\n            \"to\": 20\n          },\n          {\n            \"from\": 20,\n            \"to\": 30\n          },\n          {\n            \"from\": 30\n          }\n        ]\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u6211\u4eec\u4e3a\u67e5\u8be2\u6307\u5b9a\u4e86\u4e09\u4e2a\u8303\u56f4\u3002 \u8fd9\u610f\u5473\u7740 Elasticsearch \u5c06\u521b\u5efa\u4e0e\u6bcf\u4e2a\u8303\u56f4\u76f8\u5bf9\u5e94\u7684\u4e09\u4e2a\u5b58\u50a8\u6876\u3002 \u4e0a\u9762\u7684\u67e5\u8be2\u5e94\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

      \"aggregations\" : {\n    \"goal_ranges\" : {\n      \"buckets\" : [\n        {\n          \"key\" : \"*-20.0\",\n          \"to\" : 20.0,\n          \"doc_count\" : 3\n        },\n        {\n          \"key\" : \"20.0-30.0\",\n          \"from\" : 20.0,\n          \"to\" : 30.0,\n          \"doc_count\" : 13\n        },\n        {\n          \"key\" : \"30.0-*\",\n          \"from\" : 30.0,\n          \"doc_count\" : 6\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u5982\u8f93\u51fa\u6240\u793a\uff0c\u6211\u4eec\u6307\u6570\u4e2d\u6700\u591a\u7684\u8fd0\u52a8\u5458\u5e74\u9f84\u572820\u81f330\u5c81\u4e4b\u95f4\u3002<\/p>\n

    \u4e3a\u4e86\u4f7f\u8303\u56f4\u66f4\u6613\u7406\u89e3\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u8303\u56f4\u81ea\u5b9a\u4e49\u952e\u540d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"goal_ranges\": {\n      \"range\": {\n        \"field\": \"age\",\n        \"ranges\": [\n          {\n            \"key\": \"start-of-career\",\n            \"to\": 20\n          },\n          {\n            \"key\": \"mid-of-career\",\n            \"from\": 20,\n            \"to\": 30\n          },\n          {\n            \"key\": \"end-of-cereer\",\n            \"from\": 30\n          }\n        ]\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u8fd9\u6837\u4ea7\u751f\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n

      \"aggregations\" : {\n    \"goal_ranges\" : {\n      \"buckets\" : [\n        {\n          \"key\" : \"start-of-career\",\n          \"to\" : 20.0,\n          \"doc_count\" : 3\n        },\n        {\n          \"key\" : \"mid-of-career\",\n          \"from\" : 20.0,\n          \"to\" : 30.0,\n          \"doc_count\" : 13\n        },\n        {\n          \"key\" : \"end-of-cereer\",\n          \"from\" : 30.0,\n          \"doc_count\" : 6\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7edf\u8ba1\u5b50\u805a\u5408\u5c06\u66f4\u591a\u4fe1\u606f\u6dfb\u52a0\u5230\u8303\u56f4\u3002 \u6b64\u6c47\u603b\u5c06\u4e3a\u6bcf\u4e2a\u8303\u56f4\u63d0\u4f9b\u6700\u5c0f\u503c\uff0c\u6700\u5927\u503c\uff0c\u5e73\u5747\u503c\u548c\u603b\u548c\u3002 \u8ba9\u6211\u4eec\u6765\u770b\u770b\uff1a<\/p>\n

    GET sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"goal_ranges\": {\n      \"range\": {\n        \"field\": \"age\",\n        \"ranges\": [\n          {\n            \"key\": \"start-of-career\",\n            \"to\": 20\n          },\n          {\n            \"key\": \"mid-of-career\",\n            \"from\": 20,\n            \"to\": 30\n          },\n          {\n            \"key\": \"end-of-cereer\",\n            \"from\": 30\n          }\n        ]\n      },\n      \"aggs\": {\n        \"age_stats\": {\n          \"stats\": {\n            \"field\": \"age\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u67e5\u8be2\u7ed3\u679c\u662f\uff1a<\/p>\n

      \"aggregations\" : {\n    \"goal_ranges\" : {\n      \"buckets\" : [\n        {\n          \"key\" : \"start-of-career\",\n          \"to\" : 20.0,\n          \"doc_count\" : 3,\n          \"age_stats\" : {\n            \"count\" : 3,\n            \"min\" : 18.0,\n            \"max\" : 19.0,\n            \"avg\" : 18.333333333333332,\n            \"sum\" : 55.0\n          }\n        },\n        {\n          \"key\" : \"mid-of-career\",\n          \"from\" : 20.0,\n          \"to\" : 30.0,\n          \"doc_count\" : 13,\n          \"age_stats\" : {\n            \"count\" : 13,\n            \"min\" : 20.0,\n            \"max\" : 29.0,\n            \"avg\" : 25.846153846153847,\n            \"sum\" : 336.0\n          }\n        },\n        {\n          \"key\" : \"end-of-cereer\",\n          \"from\" : 30.0,\n          \"doc_count\" : 6,\n          \"age_stats\" : {\n            \"count\" : 6,\n            \"min\" : 31.0,\n            \"max\" : 41.0,\n            \"avg\" : 35.0,\n            \"sum\" : 210.0\n          }\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u5728 Kibana \u4e2d\u53ef\u89c6\u5316\u8303\u56f4\u975e\u5e38\u7b80\u5355\u3002 \u6211\u4eec\u5c06\u4e3a\u6b64\u4f7f\u7528\u997c\u56fe\u3002 \u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5207\u7247\u5927\u5c0f count \u805a\u5408\u5b9a\u4e49\u3002 \u5728\u5b58\u50a8\u6876\u90e8\u5206\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6570\u636e\u521b\u5efa\u4e09\u4e2a\u8303\u56f4\u3002 \u8fd9\u4e9b\u8303\u56f4\u5c06\u662f\u997c\u56fe\u7684\u5206\u5272\u90e8\u5206\u3002<\/p>\n

    \"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3<\/p>\n

    <\/h2>\n

    Geo Distance Aggregation<\/h2>\n

    \u4f7f\u7528\u5730\u7406\u8ddd\u79bb\u805a\u5408\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u539f\u70b9\u548c\u5230\u8be5\u70b9\u7684\u4e00\u7ec4\u8ddd\u79bb\u8303\u56f4\u3002\u7136\u540e\uff0c\u805a\u5408\u5c06\u8bc4\u4f30\u6bcf\u4e2a geo_point \u503c\u5230\u539f\u70b9\u7684\u8ddd\u79bb\uff0c\u5e76\u786e\u5b9a\u6587\u6863\u5c5e\u4e8e\u54ea\u4e2a\u8303\u56f4\u3002\u5982\u679c\u6587\u6863\u7684 geo_point \u503c\u4e0e\u539f\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u843d\u5165\u8be5\u5b58\u50a8\u6876\u7684\u8ddd\u79bb\u8303\u56f4\u5185\uff0c\u5219\u8be5\u6587\u6863\u88ab\u89c6\u4e3a\u5c5e\u4e8e\u8be5\u5b58\u50a8\u6876\u3002<\/p>\n

    \u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7684\u539f\u70b9\u7684\u7eac\u5ea6\u503c\u4e3a 46.22\uff0c\u7ecf\u5ea6\u503c\u4e3a -68.85\u3002\u6211\u4eec\u4f7f\u7528\u539f\u70b9 46.22\uff0c-68.85 \u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u503c\u5b9a\u4e49\u7eac\u5ea6\uff0c\u7b2c\u4e8c\u4e2a\u503c\u5b9a\u4e49\u7ecf\u5ea6\u3002\u6216\u8005\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8c61\u683c\u5f0f -{\"lat\"\uff1a46.22\uff0c\"lon\"\uff1a-68.85} \u6216\u6570\u7ec4\u683c\u5f0f\uff1a[-68.85\uff0c46.22] \u57fa\u4e8e GeoJson \u6807\u51c6\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u6570\u5b57\u662f lon \u548c\u7b2c\u4e8c\u4e2a\u662f lat\u3002<\/p>\n

    \u53e6\u5916\uff0c\u6211\u4eec\u4ee5 km \u503c\u521b\u5efa\u4e09\u4e2a\u8303\u56f4\u3002\u9ed8\u8ba4\u8ddd\u79bb\u5355\u4f4d\u662f m\uff08\u7c73\uff09\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728 \u201cunit\u201d\uff08\u5355\u4f4d\uff09\u5b57\u6bb5\u4e2d\u660e\u786e\u8bbe\u7f6e km\u3002\u5176\u4ed6\u53d7\u652f\u6301\u7684\u8ddd\u79bb\u5355\u4f4d\u662f mi\uff08\u82f1\u91cc\uff09\uff0cin\uff08\u82f1\u5bf8\uff09\uff0cyd\uff08\u7801\uff09\uff0ccm\uff08\u5398\u7c73\uff09\u548cmm\uff08\u6beb\u7c73\uff09\u3002<\/p>\n

    POST sports\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"althlete_location\": {\n      \"geo_distance\": {\n        \"field\": \"location\",\n        \"origin\": {\n          \"lat\": 46.22,\n          \"lon\": -68.85\n        },\n        \"ranges\": [\n          {\n            \"to\": 200\n          },\n          {\n            \"from\": 200, \"to\": 400\n          },\n          {\n            \"from\": 400\n          }\n        ]\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u67e5\u8be2\u7ed3\u679c\uff1a<\/p>\n

      \"aggregations\" : {\n    \"althlete_location\" : {\n      \"buckets\" : [\n        {\n          \"key\" : \"*-200.0\",\n          \"from\" : 0.0,\n          \"to\" : 200.0,\n          \"doc_count\" : 2\n        },\n        {\n          \"key\" : \"200.0-400.0\",\n          \"from\" : 200.0,\n          \"to\" : 400.0,\n          \"doc_count\" : 0\n        },\n        {\n          \"key\" : \"400.0-*\",\n          \"from\" : 400.0,\n          \"doc_count\" : 20\n        }\n      ]\n    }\n  }<\/code><\/pre>\n

    \u7ed3\u679c\u8868\u660e\uff0c\u5c45\u4f4f\u5728\u8ddd\u8d77\u70b9\u4e0d\u8d85\u8fc7200 km\u7684\u8fd0\u52a8\u5458\u67092\u540d\uff0c\u5c45\u4f4f\u5728\u8ddd\u8d77\u70b9\u8d85\u8fc7400 km\u7684\u8fd0\u52a8\u5458\u670920\u540d\u3002<\/p>\n

    <\/h4>\n

    IP Range Aggregation<\/h2>\n

    Elasticsearch \u8fd8\u5177\u6709\u5bf9 IP \u8303\u56f4\u7684\u5185\u7f6e\u652f\u6301\u3002 IP \u805a\u5408\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u5176\u4ed6\u8303\u56f4\u805a\u5408\u7c7b\u4f3c\u3002 \u8ba9\u6211\u4eec\u4e3a IP \u5730\u5740\u521b\u5efa\u7d22\u5f15\u6620\u5c04\uff0c\u4ee5\u8bf4\u660e\u6b64\u805a\u5408\u7684\u5de5\u4f5c\u65b9\u5f0f\uff1a<\/p>\n

    PUT ips\n{\n  \"mappings\": {\n    \"properties\": {\n      \"ip_addr\": {\n        \"type\": \"ip\"\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u8ba9\u6211\u4eec\u5c06\u4e00\u4e9b\u4e13\u7528\u7f51\u7edc IP \u653e\u5165\u7d22\u5f15\u4e2d\u3002<\/p>\n

    POST ips\/_bulk\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.0\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.1\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.2\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.3\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.4\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.5\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.6\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.7\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.8\" }\n{\"index\":{\"_index\":\"ips\"}}\n{ \"ip_addr\": \"172.16.0.9\" }<\/code><\/pre>\n

    \u73b0\u5728\uff0c\u5f53\u7d22\u5f15\u4e2d\u5305\u542b\u4e00\u4e9b\u6570\u636e\u65f6\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a IP \u8303\u56f4\u805a\u5408\uff1a<\/p>\n

    GET ips\/_search\n{\n  \"size\": 0,\n  \"aggs\": {\n    \"ip_ranges\": {\n      \"ip_range\": {\n        \"field\": \"ip_addr\",\n        \"ranges\": [\n          {\n            \"to\": \"172.16.0.4\"\n          },\n          {\n            \"from\": \"172.16.0.4\"\n          }\n        ]\n      }\n    }\n  }\n}<\/code><\/pre>\n

    \u53c2\u8003\uff1a<\/p>\n

    \u30101\u3011Bucket aggregations | Elasticsearch Guide [7.16] | Elastic<\/p>\n","protected":false},"excerpt":{"rendered":"Elasticsearch\uff1a\u900f\u5f7b\u7406\u89e3 Elasticsearch \u4e2d\u7684 Bucket aggregationElasticsearch\u9664\u4e86\u5728\u641c\u7d22\u65b9\u9762\u975e\u5e38\u4e4b\u5feb\uff0c\u5bf9\u6570\u636e\u5206\u6790\u4e5f\u662f\u975e\u5e38...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7325"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7325"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7325\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}