{"id":194681,"date":"2020-11-02T07:37:10","date_gmt":"2020-11-01T23:37:10","guid":{"rendered":"http:\/\/4563.org\/?p=194681"},"modified":"2020-11-02T07:37:10","modified_gmt":"2020-11-01T23:37:10","slug":"%e6%9c%89%e6%b2%a1%e6%9c%89-elasticsearch-%e7%86%9f%e6%82%89%e7%9a%84%e5%a4%a7%e4%bd%ac%ef%bc%8c%e5%88%86%e7%bb%84%e8%81%9a%e5%90%88%e5%88%86%e9%a1%b5%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=194681","title":{"rendered":"\u6709\u6ca1\u6709 elasticsearch \u719f\u6089\u7684\u5927\u4f6c\uff0c\u5206\u7ec4\u805a\u5408\u5206\u9875\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6709\u6ca1\u6709 elasticsearch \u719f\u6089\u7684\u5927\u4f6c\uff0c\u5206\u7ec4\u805a\u5408\u5206\u9875\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dyllen <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5b9e\u73b0\u7c7b\u4f3c SQL \u91cc\u9762 group by \u5206\u7ec4\u53bb\u91cd\u4e4b\u540e\u5206\u9875\u8f93\u51fa\u5217\u8868\u3002<\/p>\n<p>\u6211\u7684\u505a\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u9875\u7684\u6570\u636e\u5217\u8868<\/li>\n<\/ol>\n<p>\u7528\u7684\u662f<code>composite<\/code>\u6876\u805a\u5408\u83b7\u53d6\u4e00\u9875\u7684\u6570\u636e\uff0c\u4e0b\u4e00\u9875\u7528<code>after<\/code>\u53c2\u6570\u3002<\/p>\n<p>\u83b7\u53d6\u5230\u4e00\u9875\u6570\u636e\u5217\u8868\u4e4b\u540e\uff0c\u62ff\u5230 id \u5217\u8868\u518d\u53bb\u4e3b\u8868\u91cc\u9762\u67e5\u8be2\u5177\u4f53\u7684\u6570\u636e\u3002<\/p>\n<p>agg \u90e8\u5206\uff1a<\/p>\n<pre><code>'aggs' =&gt; [     'uid' =&gt; [         'composite' =&gt; [          'size' =&gt; 20,          'sources' =&gt; [                 [                     'uid' =&gt; [                         'terms' =&gt; [                         'field' =&gt; 'uid',                         'order' =&gt; 'desc',                     ],           ],          ],         ],     ], ] <\/code><\/pre>\n<p>\u7528 uid \u805a\u5408\u548c\u6392\u5e8f<\/p>\n<ol>\n<li>\u83b7\u53d6\u6570\u636e\u603b\u6570<\/li>\n<\/ol>\n<p>\u7528\u7684<code>cardinality<\/code>\u548c<code>sum_bucket<\/code>\u7ec4\u548c\u3002<\/p>\n<p>\u805a\u5408\u4e4b\u540e\u5728\u8ba1\u7b97\u6876\u7684\u603b\u6570\uff0csize \u8bbe\u7f6e\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u6570\u3002<\/p>\n<p>agg \u90e8\u5206\uff1a<\/p>\n<pre><code>'aggs' =&gt; [             'uid' =&gt; [                 'terms' =&gt; [                     'field' =&gt; 'uid',                     'size' =&gt; 5000,                 ],                 'aggs' =&gt; [                     'uid_count' =&gt; [                         'cardinality' =&gt; [                             'field' =&gt; 'uid',                         ],                     ],                 ],             ],             'sun_uid' =&gt; [                 'sum_bucket' =&gt; [                     'buckets_path' =&gt; 'uid&gt;uid_count',                 ],             ],         ] <\/code><\/pre>\n<p>\u65b9\u6cd5\u5bf9\u4e0d\u5bf9\u5440\uff1f\u603b\u611f\u89c9 es \u505a\u5206\u9875\u5217\u8868\u8f93\u51fa\u4e0d\u662f\u592a\u597d,\u53ea\u80fd\u83b7\u53d6\u4e0b\u4e00\u9875\u3002<\/p>\n<p>ui \u9700\u6c42\u5206\u9875\u9700\u6c42\u662f\u53ef\u4ee5\u8df3\u9875\uff0c\u968f\u4fbf\u70b9\u54ea\u9875\u7684\uff0c\u7528 es \u7684\u8bdd\uff0c\u4e00\u822c\u600e\u4e48\u6ee1\u8db3\u7684\uff1f\u8fd8\u662f\u8bf4\u6ee1\u8db3\u4e0d\u6765\uff0c\u53ea\u80fd\u6539\u9700\u6c42\u4e86\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4219957\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : smart9527 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 from, size                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4219958\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DavinBin23 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f7f\u7528 Terms Aggregation\uff0c\u6309\u7167\u67d0\u4e9b\u5b57\u6bb5\u5206\u7ec4\u805a\u5408\uff0c\u5f97\u5230 n \u4e2a\u6876\uff0c\u6876\u91cc\u9762\u88c5\u7684\u662f\u5206\u7ec4\u7684\u6570\u636e\uff0c\u518d\u7528 bucket_sort \u5bf9\u6876\u8fdb\u884c\u5206\u9875                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4219959\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : dyllen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @smart9527 \u8fd9\u4e2a\u6211\u77e5\u9053\uff0c\u805a\u5408\u7528\u4e0d\u4e86\uff0c\u5206\u9875\u6570\u636e\u6709\u9650\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4219960\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : dyllen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @DavinBin23 \u6211\u53bb\u8bd5\u4e86\u4e0b\uff0c\u6211\u7528\u67d0\u4e2a\u5b57\u6bb5\u5206\u7ec4\u805a\u5408\uff0c\u7528\u53e6\u4e00\u4e2a\u5b57\u6bb5\u6392\u5e8f\u662f\u4e0d\u662f\u5c31\u4e0d\u884c\u4e86\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4219961\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DavinBin23 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dyllen \u53ef\u4ee5\u7684\uff0cbucket_sort \u652f\u6301<br \/>&#8220;bucket_sort&#8221;: {<br \/> &#8220;sort&#8221;: {<br \/> &#8220;xxx&#8221;: {<br \/> &#8220;order&#8221;: &#8220;desc&#8221;<br \/> }<br \/> },<br \/> &#8220;from&#8221;: 10,<br \/> &#8220;size&#8221;: 10<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u6ca1\u6709 elasticsearch&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194681"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194681"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194681\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194681"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}