{"id":23633,"date":"2024-11-21T12:15:03","date_gmt":"2024-11-21T04:15:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23633\/"},"modified":"2024-11-21T12:15:03","modified_gmt":"2024-11-21T04:15:03","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8thinkphp6%e5%ae%9e%e7%8e%b0%e5%88%86%e8%af%8d%e6%90%9c%e7%b4%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23633\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22"},"content":{"rendered":"<p>\u968f\u7740\u5927\u6570\u636e\u65f6\u4ee3\u7684\u5230\u6765\uff0c\u641c\u7d22\u5f15\u64ce\u7684\u91cd\u8981\u6027\u4e5f\u65e5\u76ca\u51f8\u663e\u3002\u800c\u5206\u8bcd\u641c\u7d22\uff0c\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u66f4\u662f\u88ab\u5e7f\u6cdb\u91c7\u7528\u3002\u90a3\u4e48\uff0c\u5728\u4f7f\u75286\u6846\u67b6\u4e0b\uff0c\u5982\u4f55\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u5462\uff1f\u63a5\u4e0b\u6765\uff0c\u672c\u6587\u5c06\u4e3a\u5927\u5bb6\u4e00\u4e00\u4ecb\u7ecd\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u5206\u8bcd\u641c\u7d22\uff1f<\/p>\n<p>\u5206\u8bcd\u641c\u7d22\u5373\u5c06\u7528\u6237\u952e\u5165\u7684\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u5904\u7406\uff0c\u5e76\u6839\u636e\u5206\u8bcd\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u641c\u7d22\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u201c\u7535\u89c6\u673a\u4ef7\u683c\u201d\u65f6\uff0c\u7cfb\u7edf\u5c06\u4f1a\u81ea\u52a8\u5c06\u5176\u5206\u4e3a\u201c\u7535\u89c6\u673a\u201d\u548c\u201c\u4ef7\u683c\u201d\u4e24\u4e2a\u5173\u952e\u8bcd\uff0c\u7136\u540e\u8fdb\u884c\u641c\u7d22\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u8f93\u5165\u7684\u5173\u952e\u8bcd\u4e0d\u5b8c\u6574\u6216\u9519\u8bef\uff0c\u7cfb\u7edf\u4e5f\u80fd\u667a\u80fd\u5339\u914d\u3002<\/p>\n<p>\u4e8c\u3001\u5982\u4f55\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\uff1f<\/p>\n<ol>\n<li>\u5b89\u88c5ThinkPHP6\u6846\u67b6<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u672c\u5730\u5b89\u88c5ThinkPHP6\u6846\u67b6\u3002\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u7167\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u5b89\u88c5\u5206\u8bcd\u5e93<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528jieba\u5206\u8bcd\u5e93\u6765\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\u3002jieba\u662f Python \u4e2d\u5e38\u7528\u7684\u4e2d\u6587\u5206\u8bcd\u5e93\uff0c\u652f\u6301\u4e09\u79cd\u5206\u8bcd\u6a21\u5f0f\uff1a\u7cbe\u786e\u6a21\u5f0f\u3001\u5168\u6a21\u5f0f\u548c\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PHP\u7684\u6269\u5c55\u5e93jieba-php\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u4e0b\u8f7djieba-php\u6269\u5c55\u5e93\uff0c\u5e76\u5c06\u5176\u653e\u7f6e\u5728ThinkPHP6\u6846\u67b6\u7684extend\u6587\u4ef6\u5939\u4e0b\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1\uff09\u5728GitHub\u4e0a\u4e0b\u8f7djieba-php\u6269\u5c55\u5e93<\/p>\n<p>2\uff09\u5c06\u4e0b\u8f7d\u7684jieba-php-master\u6587\u4ef6\u5939\u653e\u7f6e\u5728ThinkPHP6\u6846\u67b6\u7684extend\u6587\u4ef6\u5939\u4e0b<\/p>\n<ol>\n<li>\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u529f\u80fd<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u529f\u80fd\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u4e00\u4e2asearch\u65b9\u6cd5\uff0c\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u641c\u7d22\u8bcd\u3002<\/p>\n<pre>public function search()\n{\n    $keywords = input('keywords');\n    $result = [];\n    if ($keywords) {\n        \/\/ TODO: \u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\n    }\n    return json($result);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7input()\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u641c\u7d22\u8bcd\uff0c\u7136\u540e\u6839\u636e\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ee5JSON\u683c\u5f0f\u8fd4\u56de\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1\uff09\u5f15\u5165jieba\u5206\u8bcd\u5e93<\/p>\n<p>\u5728\u641c\u7d22\u63a7\u5236\u5668\u7684\u6587\u4ef6\u5934\u90e8\u5f15\u5165jieba-php\u6269\u5c55\u5e93\uff1a<\/p>\n<pre>use FukuballJiebaJieba;\nuse FukuballJiebaPosseg;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2\uff09\u5bf9\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd<\/p>\n<p>\u5728search\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528jieba\u5206\u8bcd\u5e93\u5bf9\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\uff0c\u5e76\u4fdd\u5b58\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>Jieba::init();\nPosseg::init();\n\n$seg_list = Posseg::cut($keywords);\n$search_arr = [];\nforeach ($seg_list as $seg) {\n    if (strlen($seg['word']) &amp;gt; 1) {\/\/\u5254\u9664\u5355\u5b57\u5173\u952e\u8bcd\n        array_push($search_arr, $seg['word']);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u4ee3\u7801\u4e2d\uff0c\u5148\u5bf9jieba\u5206\u8bcd\u5e93\u8fdb\u884c\u521d\u59cb\u5316\u3002\u7136\u540e\uff0c\u901a\u8fc7Posseg::cut()\u51fd\u6570\u5bf9\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728$seg_list\u6570\u7ec4\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u4e00\u4e2aforeach\u5faa\u73af\u5bf9\u5206\u8bcd\u7ed3\u679c\u8fdb\u884c\u7b5b\u9009\uff0c\u53ea\u4fdd\u7559\u957f\u5ea6\u5927\u4e8e1\u7684\u5173\u952e\u8bcd\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728$search_arr\u6570\u7ec4\u4e2d\u3002<\/p>\n<p>3\uff09\u8fdb\u884c\u641c\u7d22\u5339\u914d<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ThinkPHP6\u6846\u67b6\u63d0\u4f9b\u7684\u67e5\u8be2\u6784\u9020\u5668\u8fdb\u884c\u641c\u7d22\u5339\u914d\u64cd\u4f5c\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>$map[] = ['title', 'like', '%' . $keywords . '%'];\nif (!empty($search_arr)) {\n    foreach ($search_arr as $keyword) {\n        $map[] = ['title', 'like', '%' . $keyword . '%'];\n    }\n}\n$result = Db::table('article')-&amp;gt;whereOr($map)-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u4ee3\u7801\u9996\u5148\u901a\u8fc7$map\u6570\u7ec4\u5b9a\u4e49\u67e5\u8be2\u6761\u4ef6\uff0c\u5c06\u641c\u7d22\u8bcd\u4f5c\u4e3a\u6761\u4ef6\u8fdb\u884c\u7cbe\u786e\u5339\u914d\uff0c\u5982\u679c\u6709\u5206\u8bcd\u7ed3\u679c\uff0c\u5219\u5c06\u5206\u8bcd\u7ed3\u679c\u4e5f\u4f5c\u4e3a\u6761\u4ef6\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u3002\u6700\u540e\uff0c\u901a\u8fc7Db::table()-&gt;whereOr()\u51fd\u6570\u5c06\u67e5\u8be2\u6761\u4ef6\u8fdb\u884cOR\u6761\u4ef6\u62fc\u63a5\uff0c\u5e76\u8c03\u7528select()\u51fd\u6570\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\uff0c\u5c31\u662f\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u7684\u5168\u8fc7\u7a0b\u3002\u5f53\u7136\uff0c\u8fd8\u6709\u5f88\u591a\u7ec6\u8282\u9700\u8981\u6ce8\u610f\uff0c\u6bd4\u5982jieba\u5206\u8bcd\u5e93\u7684\u7248\u672c\u95ee\u9898\uff0c\u4ee5\u53ca\u641c\u7d22\u7ed3\u679c\u7684\u5448\u73b0\u65b9\u5f0f\u7b49\u7b49\uff0c\u5728\u4ee3\u7801\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u9700\u8981\u8ba4\u771f\u601d\u8003\u548c\u7814\u7a76\uff0c\u624d\u80fd\u591f\u5f97\u5230\u4e00\u4e2a\u7a33\u5b9a\u3001\u9ad8\u6548\u7684\u5206\u8bcd\u641c\u7d22\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\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\u5927\u6570\u636e\u65f6\u4ee3\u7684\u5230\u6765\uff0c\u641c\u7d22\u5f15\u64ce\u7684\u91cd\u8981\u6027\u4e5f\u65e5\u76ca\u51f8\u663e\u3002\u800c\u5206\u8bcd\u641c\u7d22\uff0c\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u66f4\u662f\u88ab\u5e7f\u6cdb\u91c7\u7528\u3002\u90a3\u4e48\uff0c\u5728\u4f7f\u75286\u6846\u67b6\u4e0b\uff0c\u5982\u4f55\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u5462\uff1f\u63a5\u4e0b\u6765\uff0c\u672c\u6587\u5c06\u4e3a\u5927\u5bb6\u4e00\u4e00\u4ecb\u7ecd\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u5206\u8bcd\u641c\u7d22\uff1f \u5206\u8bcd\u641c\u7d22\u5373\u5c06\u7528\u6237\u952e\u5165\u7684\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u5904\u7406\uff0c\u5e76\u6839\u636e\u5206\u8bcd\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u641c\u7d22\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u201c\u7535\u89c6\u673a\u4ef7\u683c\u201d\u65f6\uff0c\u7cfb\u7edf\u5c06\u4f1a\u81ea\u52a8\u5c06\u5176\u5206\u4e3a\u201c\u7535\u89c6\u673a\u201d\u548c\u201c\u4ef7\u683c\u201d\u4e24\u4e2a\u5173\u952e\u8bcd\uff0c\u7136\u540e\u8fdb\u884c\u641c\u7d22\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u8f93\u5165\u7684\u5173\u952e\u8bcd\u4e0d\u5b8c\u6574\u6216\u9519\u8bef\uff0c\u7cfb\u7edf\u4e5f\u80fd\u667a\u80fd\u5339\u914d\u3002 \u4e8c\u3001\u5982\u4f55\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\uff1f \u5b89\u88c5ThinkPHP6\u6846\u67b6 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u672c\u5730\u5b89\u88c5ThinkPHP6\u6846\u67b6\u3002\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u7167\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u5b89\u88c5\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5b89\u88c5\u5206\u8bcd\u5e93 \u5728ThinkPHP6\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528jieba\u5206\u8bcd\u5e93\u6765\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\u3002jieba\u662f Python \u4e2d\u5e38\u7528\u7684\u4e2d\u6587\u5206\u8bcd\u5e93\uff0c\u652f\u6301\u4e09\u79cd\u5206\u8bcd\u6a21\u5f0f\uff1a\u7cbe\u786e\u6a21\u5f0f\u3001\u5168\u6a21\u5f0f\u548c\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PHP\u7684\u6269\u5c55\u5e93jieba-php\u8fdb\u884c\u8c03\u7528\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u4e0b\u8f7djieba-php\u6269\u5c55\u5e93\uff0c\u5e76\u5c06\u5176\u653e\u7f6e\u5728ThinkPHP6\u6846\u67b6\u7684extend\u6587\u4ef6\u5939\u4e0b\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a 1\uff09\u5728GitHub\u4e0a\u4e0b\u8f7djieba-php\u6269\u5c55\u5e93 2\uff09\u5c06\u4e0b\u8f7d\u7684jieba-php-master\u6587\u4ef6\u5939\u653e\u7f6e\u5728ThinkPHP6\u6846\u67b6\u7684extend\u6587\u4ef6\u5939\u4e0b \u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u529f\u80fd \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u529f\u80fd\u3002 \u9996\u5148\uff0c\u5728\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u4e00\u4e2asearch\u65b9\u6cd5\uff0c\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u641c\u7d22\u8bcd\u3002 public function search() { $keywords = input(&#8216;keywords&#8217;); $result = []; if ($keywords) { \/\/ TODO: \u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c } return json($result); } \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7input()\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u641c\u7d22\u8bcd\uff0c\u7136\u540e\u6839\u636e\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ee5JSON\u683c\u5f0f\u8fd4\u56de\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u5206\u8bcd\u641c\u7d22\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a 1\uff09\u5f15\u5165jieba\u5206\u8bcd\u5e93 \u5728\u641c\u7d22\u63a7\u5236\u5668\u7684\u6587\u4ef6\u5934\u90e8\u5f15\u5165jieba-php\u6269\u5c55\u5e93\uff1a use FukuballJiebaJieba; use FukuballJiebaPosseg; \u767b\u5f55\u540e\u590d\u5236 2\uff09\u5bf9\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd \u5728search\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528jieba\u5206\u8bcd\u5e93\u5bf9\u641c\u7d22\u8bcd\u8fdb\u884c\u5206\u8bcd\u64cd\u4f5c\uff0c\u5e76\u4fdd\u5b58\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a Jieba::init(); Posseg::init(); $seg_list = Posseg::cut($keywords); $search_arr [&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-23633","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23633","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=23633"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23633\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}