{"id":22422,"date":"2024-11-21T13:31:51","date_gmt":"2024-11-21T05:31:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22422\/"},"modified":"2024-11-21T13:31:51","modified_gmt":"2024-11-21T05:31:51","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8thinkphp%e6%a1%86%e6%9e%b6%e4%b8%ad%e5%ae%9e%e7%8e%b0%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2%e5%a4%9a%e4%b8%aa%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22422\/","title":{"rendered":"\u5982\u4f55\u5728thinkphp\u6846\u67b6\u4e2d\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2\u591a\u4e2a\u5b57\u6bb5"},"content":{"rendered":"<p>\u5728\u5f00\u53d1web\u5e94\u7528\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2\u6765\u8fdb\u884c\u641c\u7d22\u529f\u80fd\u7684\u5b9e\u73b0\u3002\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u65f6\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp\u6846\u67b6\u4e2d\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2\u591a\u4e2a\u5b57\u6bb5\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u6a21\u578b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u83b7\u53d6\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\u7684\u65b9\u6cd5\u3002\u5728\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528thinkphp\u81ea\u5e26\u7684\u6a21\u7cca\u67e5\u8be2\u53c2\u6570%\u6765\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public function getFuzzySearchResult($keyword){\n    $result = $this-&gt;where('title', 'like', '%'.$keyword.'%')  \/\/\u5728title\u5b57\u6bb5\u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\n                   -&gt;whereOr('content', 'like', '%'.$keyword.'%')  \/\/\u5728content\u5b57\u6bb5\u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\n                   -&gt;select();\n    return $result;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86where\u548cwhereOr\u4e24\u4e2a\u67e5\u8be2\u6761\u4ef6\u3002\u5176\u4e2d\uff0cwhere\u662f\u6307\u67e5\u8be2\u6307\u5b9a\u5b57\u6bb5\u4e0e\u6307\u5b9a\u503c\u76f8\u7b49\u7684\u7ed3\u679c\uff0c\u800cwhereOr\u662f\u6307\u67e5\u8be2\u6307\u5b9a\u5b57\u6bb5\u4e0e\u6307\u5b9a\u503c\u76f8\u7b49\u7684\u7ed3\u679c\u6216\u8005\u6307\u5b9a\u5b57\u6bb5\u4e0e\u6307\u5b9a\u503c\u76f8\u7b49\u7684\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728title\u4e0econtent\u4e24\u4e2a\u5b57\u6bb5\u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\uff0c\u5e76\u5c06\u7ed3\u679c\u96c6\u5408\u5e76\u8fd4\u56de\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u63a7\u5236\u5668\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u5e76\u5c06\u67e5\u8be2\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\u8fdb\u884c\u5c55\u793a\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public function fuzzySearch(){\n    $keyword = input('keyword');  \/\/\u83b7\u53d6\u641c\u7d22\u5173\u952e\u5b57\n    $model = new Article();  \/\/\u5b9e\u4f8b\u5316\u6a21\u578b\n    $result = $model-&gt;getFuzzySearchResult($keyword);  \/\/\u83b7\u53d6\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\n    $this-&gt;assign('result', $result);  \/\/\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\n    return $this-&gt;fetch('search_result');  \/\/\u8df3\u8f6c\u5230\u5c55\u793a\u9875\u9762\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u641c\u7d22\u5173\u952e\u5b57\uff0c\u5e76\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6a21\u578b\u3002\u7136\u540e\u8c03\u7528\u6a21\u578b\u4e2d\u5b9a\u4e49\u7684getFuzzySearchResult\u65b9\u6cd5\u83b7\u53d6\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\u3002\u6700\u540e\uff0c\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\uff0c\u5e76\u8df3\u8f6c\u5230\u5c55\u793a\u9875\u9762\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5728\u89c6\u56fe\u4e2d\u5c55\u793a\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>{if $result}\n    {foreach $result as $item}\n        &lt;div class=\"article-item\"&gt;\n            &lt;div class=\"title\"&gt;{$item.title}&lt;\/div&gt;\n            &lt;div class=\"content\"&gt;{$item.content}&lt;\/div&gt;\n        &lt;\/div&gt;\n    {\/foreach}\n{else}\n    &lt;div class=\"no-result\"&gt;\u6ca1\u6709\u641c\u7d22\u7ed3\u679c\u54e6~&lt;\/div&gt;\n{\/if}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5224\u65ad\u641c\u7d22\u7ed3\u679c\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u5219\u4f7f\u7528foreach\u5faa\u73af\u5c55\u793a\u6bcf\u4e00\u4e2a\u7ed3\u679c\uff1b\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u63d0\u793a\u7528\u6237\u6ca1\u6709\u641c\u7d22\u7ed3\u679c\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728thinkphp\u6846\u67b6\u4e2d\u5b9e\u73b0\u591a\u4e2a\u5b57\u6bb5\u7684\u6a21\u7cca\u67e5\u8be2\u529f\u80fd\u3002\u611f\u8c22\u5927\u5bb6\u7684\u9605\u8bfb\uff0c\u5e0c\u671b\u80fd\u5bf9\u5b9e\u9645\u5f00\u53d1\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728thinkphp\u6846\u67b6\u4e2d\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2\u591a\u4e2a\u5b57\u6bb5\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>\u5728\u5f00\u53d1web\u5e94\u7528\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2\u6765\u8fdb\u884c\u641c\u7d22\u529f\u80fd\u7684\u5b9e\u73b0\u3002\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u65f6\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp\u6846\u67b6\u4e2d\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2\u591a\u4e2a\u5b57\u6bb5\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u6a21\u578b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u83b7\u53d6\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\u7684\u65b9\u6cd5\u3002\u5728\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528thinkphp\u81ea\u5e26\u7684\u6a21\u7cca\u67e5\u8be2\u53c2\u6570%\u6765\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a public function getFuzzySearchResult($keyword){ $result = $this-&gt;where(&#8216;title&#8217;, &#8216;like&#8217;, &#8216;%&#8217;.$keyword.&#8217;%&#8217;) \/\/\u5728title\u5b57\u6bb5\u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2 -&gt;whereOr(&#8216;content&#8217;, &#8216;like&#8217;, &#8216;%&#8217;.$keyword.&#8217;%&#8217;) \/\/\u5728content\u5b57\u6bb5\u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2 -&gt;select(); return $result; } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86where\u548cwhereOr\u4e24\u4e2a\u67e5\u8be2\u6761\u4ef6\u3002\u5176\u4e2d\uff0cwhere\u662f\u6307\u67e5\u8be2\u6307\u5b9a\u5b57\u6bb5\u4e0e\u6307\u5b9a\u503c\u76f8\u7b49\u7684\u7ed3\u679c\uff0c\u800cwhereOr\u662f\u6307\u67e5\u8be2\u6307\u5b9a\u5b57\u6bb5\u4e0e\u6307\u5b9a\u503c\u76f8\u7b49\u7684\u7ed3\u679c\u6216\u8005\u6307\u5b9a\u5b57\u6bb5\u4e0e\u6307\u5b9a\u503c\u76f8\u7b49\u7684\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728title\u4e0econtent\u4e24\u4e2a\u5b57\u6bb5\u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\uff0c\u5e76\u5c06\u7ed3\u679c\u96c6\u5408\u5e76\u8fd4\u56de\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u63a7\u5236\u5668\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u5e76\u5c06\u67e5\u8be2\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\u8fdb\u884c\u5c55\u793a\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a public function fuzzySearch(){ $keyword = input(&#8216;keyword&#8217;); \/\/\u83b7\u53d6\u641c\u7d22\u5173\u952e\u5b57 $model = new Article(); \/\/\u5b9e\u4f8b\u5316\u6a21\u578b $result = $model-&gt;getFuzzySearchResult($keyword); \/\/\u83b7\u53d6\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c $this-&gt;assign(&#8216;result&#8217;, $result); \/\/\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe return $this-&gt;fetch(&#8216;search_result&#8217;); \/\/\u8df3\u8f6c\u5230\u5c55\u793a\u9875\u9762 } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u641c\u7d22\u5173\u952e\u5b57\uff0c\u5e76\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6a21\u578b\u3002\u7136\u540e\u8c03\u7528\u6a21\u578b\u4e2d\u5b9a\u4e49\u7684getFuzzySearchResult\u65b9\u6cd5\u83b7\u53d6\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\u3002\u6700\u540e\uff0c\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\uff0c\u5e76\u8df3\u8f6c\u5230\u5c55\u793a\u9875\u9762\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6700\u540e\uff0c\u6211\u4eec\u5728\u89c6\u56fe\u4e2d\u5c55\u793a\u6a21\u7cca\u67e5\u8be2\u7ed3\u679c\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a {if $result} {foreach $result as $item} [&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-22422","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22422","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=22422"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22422\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}