{"id":604,"date":"2020-11-06T11:31:43","date_gmt":"2020-11-06T03:31:43","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=604"},"modified":"2022-05-17T09:30:37","modified_gmt":"2022-05-17T01:30:37","slug":"elasticsearch","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=604","title":{"rendered":"Elasticsearch\u91cd\u5efa\u7d22\u5f15,\u6536\u96c6nginx\u65e5\u5fd7\uff0c\u4ee5request_time\u4e3a\u6307\u6807\u5206\u6790\u63a5\u53e3\u54cd\u5e94\u65f6\u95f4"},"content":{"rendered":"<h1 id=\"Elasticsearch\u91cd\u5efa\u7d22\u5f15-\u8d77\u56e0\">\u8d77\u56e0<\/h1>\n<p>filebeat\u91c7\u96c6nginx\u7684\u65e5\u5fd7\uff0c\u4ee5json\u683c\u5f0f\u89e3\u6790\u540e\u4f20\u5165elasticsearch\uff0c\u5168\u90e8\u5b57\u6bb5\u90fd\u662ftext\u683c\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u628arequest_time\u53d8\u6210double\u683c\u5f0f\u624d\u80fd\u4f7f\u7528\u805a\u5408\u641c\u7d22request_time\u7684\u6700\u5927\u503c.<br \/>\n\u4f46\u662felasticsearch\u7684index\u4e00\u65e6\u5efa\u7acb\u597d\u4e4b\u540e\uff0c\u5b57\u6bb5\u53ea\u80fd\u65b0\u589e\uff0c\u4e0d\u80fd\u4fee\u6539\uff0c\u6240\u4ee5\u8981\u4fee\u6539request_time\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ea\u80fd\u91cd\u5efa\u7d22\u5f15\u3002<br \/>\n\u6211\u4eec\u7684\u6b65\u9aa4\u662f\uff1a1.\u83b7\u5f97\u8001\u7d22\u5f15\u7684mapping\u4fe1\u606f\uff0c2.\u7528\u8fd9\u4e2amapping\u4fe1\u606f\u65b0\u5efa\u4e00\u4e2a\u7d22\u5f15 3.\u7528reindex\u65b9\u6cd5\uff0c\u628a\u8001\u7d22\u5f15\u7684\u6570\u636e\u8fc1\u79fb\u5230\u65b0\u7d22\u5f15 4.\u786e\u8ba4\u65b0\u7d22\u5f15\u6570\u636e\u8fc1\u79fb\u6210\u529f\uff0c5.\u5220\u9664\u8001\u7d22\u5f15 6.\u83b7\u5f97\u51fa\u65b0\u7d22\u5f15\u7684mapping\uff0c7.\u4f7f\u7528\u65b0\u7d22\u5f15\u7684mapping\u521b\u5efa\u8001\u7d22\u5f15\u30028.\u628a\u65b0\u7d22\u5f15\u7684\u6570\u636e\u5012\u56de\u8001\u7d22\u5f15 9.\u5220\u9664\u8001\u7d22\u5f15<br \/>\n\u5047\u8bbe\u8001\u7d22\u5f15\uff1aV1<br \/>\n\u4e34\u65f6\u7d22\u5f15\uff1aV2<br \/>\nnginx\u7edf\u8ba1\u63a5\u53e3\u8def\u5f84:path\u5b57\u6bb5<br \/>\nnginx\u7edf\u8ba1\u54cd\u5e94\u65f6\u95f4\uff1arequest_time\u5b57\u6bb5<\/p>\n<hr \/>\n<h1>\u6d41\u7a0b\u56fe\u4e0e\u8bf4\u660e<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-607 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-2-7.png\" alt=\"\" width=\"946\" height=\"853\" \/><\/p>\n<hr \/>\n<h1 id=\"Elasticsearch\u91cd\u5efa\u7d22\u5f15-7.\u6839\u636epath\uff0c\u805a\u5408\u67e5\u8be2\u51fa\u54cd\u5e94\u6700\u5927\u65f6\u95f4\u548c\u5e73\u5747\u65f6\u95f4\uff0c\u4fdd\u7559\u6700\u5927\u54cd\u5e94\u65f6\u95f4\u524d500\u4e2a\u5230csv\u6587\u4ef6\u91cc\">python\u4ee3\u7801<\/h1>\n<p>\u6839\u636epath\uff0c\u805a\u5408\u67e5\u8be2\u51fa\u54cd\u5e94\u6700\u5927\u65f6\u95f4\u548c\u5e73\u5747\u65f6\u95f4\uff0c\u4fdd\u7559\u6700\u5927\u54cd\u5e94\u65f6\u95f4\u524d500\u4e2a\u5230csv\u6587\u4ef6\u91cc<\/p>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#\n#  created by zhenwei.Li at 2020\/11\/3 17:50\n#\n#  filename : example4.py\n#  description :\nimport csv\nimport json\nimport requests\nif __name__ == '__main__':\n    send_json = {\n        \"query\": {\n            \"bool\": {\n                \"must\": [\n                    {\n                        \"range\": {\n                            \"@timestamp\": {\n                                \"gte\": 1533556800000,\n                                \"lte\": 1604470685934\n                            }\n                        }\n                    }\n                ]\n            }\n        },\n        \"size\": 0,\n        \"aggs\": {\n            \"Job_gender_stats\": {\n                \"terms\": {\n                    \"field\": \"path.keyword\",\n                    \"size\": 500,\n                    \"order\": {\n                        \"max_request_time\": \"desc\"\n                    }\n                },\n                \"aggs\": {\n                    \"max_request_time\": {\n                        \"max\": {\n                            \"field\": \"request_time\"\n                        }\n                    },\n                    \"avg_request_time\": {\n                        \"avg\": {\n                            \"field\": \"request_time\"\n                        }\n                    }\n                }\n            }\n        }\n    }\n    res = requests.post(url=\"http:\/\/192.168.0.174:32164\/192.168.0.67-eiop-frontend\/_search\", json=send_json)\n    print(json.dumps(res.json()['aggregations']['Job_gender_stats']['buckets'], sort_keys=True, indent=4))\n    buckets = res.json()['aggregations']['Job_gender_stats']['buckets']\n    file_handle = open('research.csv', 'w', encoding='utf-8', newline='' \"\")\n    # 2. \u57fa\u4e8e\u6587\u4ef6\u5bf9\u8c61\u6784\u5efa csv\u5199\u5165\u5bf9\u8c61\n    csv_writer = csv.writer(file_handle)\n    # 3. \u6784\u5efa\u5217\u8868\u5934\n    csv_writer.writerow([\"\u8def\u5f84\", \"\u51fa\u73b0\u6b21\u6570\", \"\u5e73\u5747\u54cd\u5e94\u65f6\u95f4(\u79d2)\", \"\u6700\u5927\u54cd\u5e94\u65f6\u95f4(\u79d2)\"])\n    for item in buckets:\n        csv_writer.writerow(\n            [item['key'], item['doc_count'], item['avg_request_time']['value'], item['max_request_time']['value']])\n    # 5. \u5173\u95ed\u6587\u4ef6\n    file_handle.close()<\/code><\/pre>\n<h1>\u6548\u679c\u56fe<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-609 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-1-4.png\" alt=\"\" width=\"457\" height=\"459\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8d77\u56e0 filebeat\u91c7\u96c6nginx\u7684\u65e5\u5fd7\uff0c\u4ee5json\u683c\u5f0f\u89e3\u6790\u540e\u4f20\u5165elasticsearch\uff0c\u5168\u90e8\u5b57\u6bb5\u90fd\u662ft [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-elasticsearch","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=604"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":871,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/604\/revisions\/871"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}