{"id":23242,"date":"2024-11-21T08:14:39","date_gmt":"2024-11-21T00:14:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23242\/"},"modified":"2024-11-21T08:14:39","modified_gmt":"2024-11-21T00:14:39","slug":"thinkphp6%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%88%86%e8%af%8d%e6%90%9c%e7%b4%a2%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23242\/","title":{"rendered":"ThinkPHP6\u4e2d\u5982\u4f55\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u641c\u7d22\u5f15\u64ce\u4e5f\u6210\u4e3a\u4e86\u65e5\u5e38\u751f\u6d3b\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\uff0c\u800c\u5206\u8bcd\u641c\u7d22\u662f\u641c\u7d22\u5f15\u64ce\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u79cd\u641c\u7d22\u65b9\u5f0f\u3002\u5728\u4f7f\u75286\u6846\u67b6\u5f00\u53d1\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u5bf9\u5206\u8bcd\u641c\u7d22\u8fdb\u884c\u6df1\u5165\u4e86\u89e3\u548c\u5e94\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecdthinkphp6\u4e2d\u5982\u4f55\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\u3002<\/p>\n<p>\u4e00\u3001\u5206\u8bcd\u641c\u7d22\u7b80\u4ecb<\/p>\n<p>\u5206\u8bcd\u641c\u7d22\u662f\u5c06\u7528\u6237\u8f93\u5165\u7684\u5173\u952e\u8bcd\u8fdb\u884c\u5206\u5272\uff0c\u7136\u540e\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u6a21\u7cca\u641c\u7d22\uff0c\u627e\u5230\u76f8\u7b26\u5408\u7684\u8bb0\u5f55\u3002\u76f8\u8f83\u4e8e\u4f20\u7edf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5206\u8bcd\u641c\u7d22\u53ef\u4ee5\u66f4\u52a0\u7cbe\u51c6\u5730\u5339\u914d\u5230\u7528\u6237\u6240\u9700\u8981\u7684\u5185\u5bb9\uff0c\u63d0\u9ad8\u641c\u7d22\u7684\u51c6\u786e\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4e8c\u3001\u5206\u8bcd\u64cd\u4f5c\u5de5\u5177<\/p>\n<p>\u5728\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\u524d\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u5206\u8bcd\u64cd\u4f5c\u5de5\u5177\uff0c\u5e38\u7528\u7684\u6709jieba\u5206\u8bcd\u3001sphinx\u5168\u6587\u68c0\u7d22\u5de5\u5177\u7b49\u3002\u672c\u6587\u4ee5jieba\u5206\u8bcd\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u4e0b\u8f7djieba\u5206\u8bcd\u5e93<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u5728jieba\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u4e0b\u8f7d\u76f8\u5173\u7684\u5206\u8bcd\u5e93\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528composer\u76f4\u63a5\u5728\u9879\u76ee\u4e2d\u5f15\u5165\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528jieba\u5206\u8bcd<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528jieba\u5206\u8bcd\u524d\uff0c\u9700\u8981\u5148\u5f15\u5165\u76f8\u5e94\u7684\u7c7b\u5e93\uff1a<\/p>\n<pre>use     hinkFacadeConfig;\nuse     hinkacadeCache;\nuse xiaodiJiebaJieba;\n\nConfig::set('cache', ['type' =&amp;gt; 'File']);\nJieba::init();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u4ee3\u7801\u7247\u6bb5\u5b9e\u73b0\u4e86\u5bf9jieba\u5206\u8bcd\u5de5\u5177\u7684\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5e76\u8bbe\u7f6e\u4e86\u7f13\u5b58\u7c7b\u578b\u4e3a\u6587\u4ef6\u7f13\u5b58\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528jieba\u5206\u8bcd\u4e86\u3002<\/p>\n<pre>$seg_list = jiebaCutForSearch('\u5206\u8bcd\u641c\u7d22\u5de5\u5177');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u4ee3\u7801\u5b9e\u73b0\u4e86\u5bf9\u201c\u5206\u8bcd\u641c\u7d22\u5de5\u5177\u201d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5206\u8bcd\u7ed3\u679c\u7684\u6570\u7ec4$seg_list\u3002<\/p>\n<p>\u4e09\u3001ThinkPHP6\u4e2d\u5206\u8bcd\u641c\u7d22\u5b9e\u73b0<\/p>\n<p>\u5728\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bf9ThinkPHP6\u7684Query\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u6269\u5c55\u67e5\u8be2\uff1a<\/p>\n<pre>use     hinkdbQuery;\n\nQuery::macro('search', function ($keyword, $field) {\n    $seg_list = jiebaCutForSearch($keyword);\n    $where = [];\n    foreach ($seg_list as $seg) {\n        $where[] = [$field, 'like', '%' . $seg . '%'];\n    }\n    return $this-&amp;gt;whereOr($where);\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u4ee3\u7801\u5b9e\u73b0\u4e86Query\u5bf9\u8c61\u7684\u6269\u5c55\u64cd\u4f5c\uff0c\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a$keyword\u4e3a\u5173\u952e\u8bcd\uff0c$field\u4e3a\u5728\u54ea\u4e9b\u5b57\u6bb5\u4e2d\u8fdb\u884c\u641c\u7d22\u3002\u9996\u5148\u5c06\u5173\u952e\u8bcd\u8fdb\u884c\u5206\u8bcd\uff0c\u7136\u540e\u5c06\u5206\u8bcd\u540e\u7684\u7ed3\u679c\u4f5c\u4e3a\u6761\u4ef6\u8fdb\u884c\u641c\u7d22\uff0c\u6700\u540e\u8fd4\u56de\u641c\u7d22\u7ed3\u679c\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6a21\u578b\u8fdb\u884c\u6269\u5c55\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>use     hinkModel;\n\nclass Article extends Model\n{\n    \/\/ \u5b9a\u4e49\u641c\u7d22\u65b9\u6cd5\n    public static function search($keyword)\n    {\n        return (new static())\n            -&amp;gt;where('status', 1)\n            -&amp;gt;search($keyword, 'title')\n            -&amp;gt;order('create_time DESC');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5bf9Article\u6a21\u578b\u8fdb\u884c\u6269\u5c55\uff0c\u5b9a\u4e49\u4e86\u641c\u7d22\u65b9\u6cd5search\uff0c\u63a5\u6536\u4e00\u4e2a\u53c2\u6570$keyword\uff0c\u8c03\u7528Query\u5bf9\u8c61\u7684search\u65b9\u6cd5\u8fdb\u884c\u641c\u7d22\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u5206\u8bcd\u641c\u7d22\u4f5c\u4e3a\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u4e92\u8054\u7f51\u5e94\u7528\u4e2d\u3002\u5728\u4f7f\u7528ThinkPHP6\u5f00\u53d1\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u5206\u8bcd\u641c\u7d22\uff0c\u5bf9Query\u5bf9\u8c61\u8fdb\u884c\u6269\u5c55\uff0c\u63d0\u9ad8\u641c\u7d22\u7684\u51c6\u786e\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u4e2d\u5982\u4f55\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u641c\u7d22\u5f15\u64ce\u4e5f\u6210\u4e3a\u4e86\u65e5\u5e38\u751f\u6d3b\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\uff0c\u800c\u5206\u8bcd\u641c\u7d22\u662f\u641c\u7d22\u5f15\u64ce\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u79cd\u641c\u7d22\u65b9\u5f0f\u3002\u5728\u4f7f\u75286\u6846\u67b6\u5f00\u53d1\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u5bf9\u5206\u8bcd\u641c\u7d22\u8fdb\u884c\u6df1\u5165\u4e86\u89e3\u548c\u5e94\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecdthinkphp6\u4e2d\u5982\u4f55\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\u3002 \u4e00\u3001\u5206\u8bcd\u641c\u7d22\u7b80\u4ecb \u5206\u8bcd\u641c\u7d22\u662f\u5c06\u7528\u6237\u8f93\u5165\u7684\u5173\u952e\u8bcd\u8fdb\u884c\u5206\u5272\uff0c\u7136\u540e\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u6a21\u7cca\u641c\u7d22\uff0c\u627e\u5230\u76f8\u7b26\u5408\u7684\u8bb0\u5f55\u3002\u76f8\u8f83\u4e8e\u4f20\u7edf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5206\u8bcd\u641c\u7d22\u53ef\u4ee5\u66f4\u52a0\u7cbe\u51c6\u5730\u5339\u914d\u5230\u7528\u6237\u6240\u9700\u8981\u7684\u5185\u5bb9\uff0c\u63d0\u9ad8\u641c\u7d22\u7684\u51c6\u786e\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002 \u4e8c\u3001\u5206\u8bcd\u64cd\u4f5c\u5de5\u5177 \u5728\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\u524d\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u5206\u8bcd\u64cd\u4f5c\u5de5\u5177\uff0c\u5e38\u7528\u7684\u6709jieba\u5206\u8bcd\u3001sphinx\u5168\u6587\u68c0\u7d22\u5de5\u5177\u7b49\u3002\u672c\u6587\u4ee5jieba\u5206\u8bcd\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4e0b\u8f7djieba\u5206\u8bcd\u5e93 \u53ef\u4ee5\u5728jieba\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u4e0b\u8f7d\u76f8\u5173\u7684\u5206\u8bcd\u5e93\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528composer\u76f4\u63a5\u5728\u9879\u76ee\u4e2d\u5f15\u5165\u3002 \u4f7f\u7528jieba\u5206\u8bcd \u5728\u4f7f\u7528jieba\u5206\u8bcd\u524d\uff0c\u9700\u8981\u5148\u5f15\u5165\u76f8\u5e94\u7684\u7c7b\u5e93\uff1a use hinkFacadeConfig; use hinkacadeCache; use xiaodiJiebaJieba; Config::set(&#8216;cache&#8217;, [&#8216;type&#8217; =&amp;gt; &#8216;File&#8217;]); Jieba::init(); \u767b\u5f55\u540e\u590d\u5236 \u8be5\u4ee3\u7801\u7247\u6bb5\u5b9e\u73b0\u4e86\u5bf9jieba\u5206\u8bcd\u5de5\u5177\u7684\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5e76\u8bbe\u7f6e\u4e86\u7f13\u5b58\u7c7b\u578b\u4e3a\u6587\u4ef6\u7f13\u5b58\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528jieba\u5206\u8bcd\u4e86\u3002 $seg_list = jiebaCutForSearch(&#8216;\u5206\u8bcd\u641c\u7d22\u5de5\u5177&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8be5\u4ee3\u7801\u5b9e\u73b0\u4e86\u5bf9\u201c\u5206\u8bcd\u641c\u7d22\u5de5\u5177\u201d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5206\u8bcd\u7ed3\u679c\u7684\u6570\u7ec4$seg_list\u3002 \u4e09\u3001ThinkPHP6\u4e2d\u5206\u8bcd\u641c\u7d22\u5b9e\u73b0 \u5728\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bf9ThinkPHP6\u7684Query\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u6269\u5c55\u67e5\u8be2\uff1a use hinkdbQuery; Query::macro(&#8216;search&#8217;, function ($keyword, $field) { $seg_list = jiebaCutForSearch($keyword); $where = []; foreach ($seg_list as $seg) { $where[] = [$field, &#8216;like&#8217;, &#8216;%&#8217; . $seg [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23242","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23242"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23242\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}