{"id":10006887,"date":"2022-11-22T06:06:21","date_gmt":"2022-11-21T22:06:21","guid":{"rendered":"https:\/\/ityet.com\/?p=10006887"},"modified":"2022-11-22T06:06:21","modified_gmt":"2022-11-21T22:06:21","slug":"elasticsearch-%e6%8c%89%e7%85%a7%e6%a0%87%e7%ad%be%e5%8c%b9%e9%85%8d%e4%b8%aa%e6%95%b0%e4%bc%98%e5%85%88%e6%8e%92%e5%ba%8f%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006887","title":{"rendered":"Elasticsearch \u6309\u7167\u6807\u7b7e\u5339\u914d\u4e2a\u6570\u4f18\u5148\u6392\u5e8f\u67e5\u8be2"},"content":{"rendered":"<p>\u6709\u4e00\u79cd\u76f8\u4f3c\u5ea6\u5339\u914d\u9700\u6c42\uff0c\u9700\u8981\u4ee5\u5339\u914d\u5230\u7684\u6807\u7b7e\u4e2a\u6570\u4f18\u5148\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u9700\u8981\u7528\u5230\u81ea\u5b9a\u4e49\u67e5\u8be2\u8bed\u53e5\u3002<\/p>\n<p>\u5148\u4e0a\u4ee3\u7801\uff0c\u8fd9\u91cc\u6211\u7528\u7684\u662fPHP\u7684\u6570\u7ec4\u7ed3\u6784\uff0c\u6700\u7ec8\u63d0\u4ea4\u7684\u65f6\u5019\u662f\u4f1a\u8f6c\u6210json\u683c\u5f0f\u7684\uff0c\u6682\u4e14\u4e0d\u8868\uff1a<\/p>\n<pre><code class=\"php\">'query' =&gt; [\n    'script_score' =&gt; [\n        &quot;query&quot;  =&gt; [],\n        'script' =&gt; [\n            'source' =&gt; '\n                        def matches = 0;\n                        for (t in params.tags) {\n                            if(doc[&quot;tags&quot;].contains(t)) {\n                                matches += 1;\n                            }\n                        }\n                        return _score * matches * matches;\n                        ',\n            &quot;params&quot; =&gt; [\n                &quot;tags&quot; =&gt; $tags,\n            ],\n        ]\n    ],\n]<\/code><\/pre>\n<p>\u9996\u5148\u6700\u5916\u5c42\u7684\u6570\u7ec4\u5c31\u662f\u6211\u4eec\u901a\u5e38\u5199\u7684<code>query<\/code>\u8bed\u53e5\uff0c\u653e\u5728<code>body<\/code>\u4e2d\u8fdb\u884c\u8bf7\u6c42\u7684\uff0c\u4e3b\u8981\u770b<code>query<\/code>\u91cc\u9762\u7684\u7ed3\u6784\uff0c\u8fd9\u79cd\u9700\u8981\u81ea\u5b9a\u4e49\u811a\u672c\u5904\u7406\u8bc4\u5206\u7684\uff0c<code>query<\/code>\u4e2d\u53ea\u653e\u4e86\u4e00\u4e2a<code>script_score<\/code>\uff1a<\/p>\n<p><code>script_score<\/code>\u5305\u542b\u4e86\u4e24\u4e2a\u90e8\u5206\uff0c\u4e00\u90e8\u5206\u662f<code>query<\/code>\uff0c\u53e6\u5916\u4e00\u90e8\u5206\u662f\u81ea\u5b9a\u4e49\u7684<code>script<\/code>\uff0c\u8fd9\u91cc\u9762\u7684<code>query<\/code>\uff0c\u5c31\u662f\u6b63\u5e38\u539f\u672c\u5199\u5728\u5916\u5c42\u7684<code>query<\/code>\u4e2d\u7684\u67e5\u8be2\u7ed3\u6784\uff0c\u539f\u672c\u600e\u4e48\u5199\u8fd8\u662f\u600e\u4e48\u5199\uff0c\u6bd4\u5982\u8fd9\u91cc\u9762\u53ef\u80fd\u8fd8\u4f1a\u9700\u8981\u4e00\u4e2a\u6807\u7b7e\u5339\u914d\u7684\uff0c\u90a3\u4e48\u7ee7\u7eed\u52a0\u4e00\u4e2a<code>terms<\/code>\u6765\u67e5\u8be2\u3002<\/p>\n<p>\u7136\u540e\u6765\u770b<code>script<\/code>\u90e8\u5206\uff0c\u8fd9\u91cc\u9762\u53c8\u5206\u4e3a\u4e24\u4e2a\u4e1c\u897f\uff1a<\/p>\n<ul>\n<li>source \u81ea\u5b9a\u4e49\u7684\u8bc4\u5206\u6392\u5e8f\u811a\u672c<\/li>\n<li>params \u81ea\u5b9a\u4e49\u7684\u8bc4\u5206\u811a\u672c\u53c2\u6570<\/li>\n<\/ul>\n<p>\u9996\u5148\u770b<code>params<\/code>\uff0c\u8fd9\u91cc\u6211\u4f20\u9012\u4e86<code>$tags<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u6807\u7b7e\u6570\u7ec4\uff0c\u6700\u7ec8\u4f1a\u5728<code>source<\/code>\u91cc\u8fdb\u884c\u8c03\u7528\uff0c<code>source<\/code>\u90e8\u5206\u4f1a\u653e\u5230<code>elasticsearch<\/code>\u4e2d\u8fdb\u884c\u7f16\u8bd1\uff0c\u5f62\u6210\u7c7b\u4f3c\u4e8e\u51fd\u6570\u7684\u4e1c\u897f\uff0c\u7136\u540e<code>params<\/code>\u5c31\u662f\u4e00\u4e2a\u53c2\u6570\uff0c\u5728<code>source<\/code>\u7684\u4ee3\u7801\u4e2d\u9700\u8981\u8c03\u7528<code>tags<\/code>\u8fd9\u4e2a\u53c2\u6570\uff0c\u91c7\u7528<code>params.tags<\/code>\u6765\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<p>\u7136\u540e\u518d\u770b<code>source<\/code>\u4ee3\u7801\u90e8\u5206\uff0c\u8fd9\u91cc\u9762\u4f1a\u628a\u641c\u7d22\u5230\u7684\u7ed3\u679c<code>doc<\/code>\u4e2d\u7684<code>tags<\/code>\u5b57\u6bb5\uff08\u8fd9doc\u4e2d\u7684<code>tags<\/code>\u4e5f\u662f\u4e00\u4e2a\u6570\u7ec4\uff09\u4e0e\u6240\u671f\u671b\u7684<code>tags<\/code>\u8fdb\u884c\u6821\u9a8c\u5bf9\u6bd4\uff0c\u6700\u540e\u5f97\u5230\u5b9e\u9645\u5339\u914d\u7684\u4e2a\u6570\uff1a<code>matches<\/code>\u3002<\/p>\n<p>\u6700\u540e\u770b<code>source<\/code>\u4e2d\u7684return\u90e8\u5206\uff0c\u8fd9\u91cc\u4f1a\u8fd4\u56de\u8bc4\u5206\u7ed3\u679c\uff0c\u8fd9\u91cc\u7684<code>_score<\/code>\u662f\u641c\u7d22\u672c\u8eab\u8ba1\u7b97\u7684\u76f8\u4f3c\u5ea6\u8bc4\u5206\uff0c\u7136\u540e\u8fd9\u4e2a\u5730\u65b9<code>_score * matches * matches<\/code>\uff0c\u8fd9\u91cc\u662f\u91c7\u7528\u6807\u7b7e\u4e2a\u6570\u5bf9\u8bc4\u5206\u7ed3\u679c\u8fdb\u884c\u653e\u5927\uff0c\u6807\u7b7e\u6570\u5339\u914d\u8d8a\u591a\uff0c\u81ea\u7136\u653e\u5927\u7ed3\u679c\u5c31\u8d8a\u5927\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5339\u914d\u6587\u6863<\/th>\n<th>_score<\/th>\n<th>\u6807\u7b7e\u4e2a\u6570<\/th>\n<th>\u7ed3\u679c\u5206<\/th>\n<th>\u6392\u5e8f\u4f4d\u7f6e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6587\u6863A<\/td>\n<td>40<\/td>\n<td>2<\/td>\n<td>160<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u6863B<\/td>\n<td>30<\/td>\n<td>3<\/td>\n<td>270<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ef\u89c1\u6700\u7ec8\u53d7\u5230\u6807\u7b7e\u5f71\u54cd\u66f4\u5927\uff0c\u5b9e\u73b0\u4e86\u6807\u7b7e\u7ed3\u679c\u6570\u4f18\u5148\u7684\u6392\u5e8f\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u81f3\u4e8e\u653e\u5927\u5f62\u5f0f\uff0c\u6bd4\u5982\u8bf4\uff0c\u8bc4\u5206\u4e00\u6837\uff0c\u4ee5\u6807\u7b7e\u6570\u4f18\u5148\uff0c\u90a3\u4e48\u4e0d\u7528\u8fdb\u884c<code>\u6807\u7b7e\u5e73\u65b9<\/code>\u5904\u7406\uff0c\u53e6\u5916\u6240\u671f\u671b\u7684\u653e\u5927\u7ed3\u679c\u6839\u636e\u5177\u4f53\u60c5\u51b5\u53ef\u4ee5\u81ea\u884c\u5904\u7406\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728\u6587\u7ae0\u672b\u5c3e\u5e16\u4e0a<code>elasticsearch<\/code>\u7684\u811a\u672c\u8bed\u6cd5\u94fe\u63a5\uff1a<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=YSmpv2hFVPoqQ7ncv5YTWg%3D%3D.bRHmO1%2FhnlzAjsX3rXs2tzNATL%2BqpaAFw5OXeUrvoLryK03P4P6WOIcfHBVTBR66xg07oQ%2FGpzX%2FGypT730Kl3PAvl2SC5tHCUfSgMc1XgCbPnXjax6jREBMkxxapVB%2F\" rel=\"nofollow\">https:\/\/www.elastic.co\/guide\/&#8230;<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042856795\">https:\/\/segmentfault.com\/a\/1190000042856795<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u4e00\u79cd\u76f8\u4f3c\u5ea6\u5339\u914d\u9700\u6c42\uff0c\u9700\u8981\u4ee5\u5339\u914d\u5230\u7684\u6807\u7b7e\u4e2a\u6570\u4f18\u5148\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u9700\u8981\u7528\u5230\u81ea\u5b9a\u4e49\u67e5\u8be2\u8bed\u53e5\u3002 \u5148\u4e0a\u4ee3\u7801\uff0c\u8fd9\u91cc\u6211\u7528\u7684\u662fP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006887","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006887"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006887\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}