{"id":21543,"date":"2024-01-02T11:12:27","date_gmt":"2024-01-02T11:12:27","guid":{"rendered":"https:\/\/server.hk\/blog\/21543\/"},"modified":"2024-11-14T10:31:00","modified_gmt":"2024-11-14T02:31:00","slug":"mongodb-glossary-text-index","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21543\/","title":{"rendered":"MongoDB \u8a5e\u5f59\u8868 &#8211; \u6587\u5b57\u7d22\u5f15"},"content":{"rendered":"<h1 id=\"mongodb-glossary-text-index-JCnuRGhuBF\">MongoDB \u8a5e\u5f59\u8868 &#8211; \u6587\u5b57\u7d22\u5f15<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7684\u4e16\u754c\u4e2d\uff0cMongoDB \u56e0\u5176\u9748\u6d3b\u6027\u3001\u53ef\u64f4\u5c55\u6027\u548c\u6613\u7528\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002MongoDB \u7684\u4e00\u500b\u95dc\u9375\u7279\u6027\u662f\u5176\u9ad8\u6548\u8655\u7406\u975e\u7d50\u69cb\u5316\u6578\u64da\uff08\u5982\u6587\u672c\uff09\u7684\u80fd\u529b\u3002\u70ba\u4e86\u5be6\u73fe\u9019\u4e00\u9ede\uff0cMongoDB \u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u7a31\u70ba\u6587\u5b57\u7d22\u5f15\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u4ec0\u9ebc\u662f\u6587\u5b57\u7d22\u5f15\u4ee5\u53ca\u5b83\u5982\u4f55\u589e\u5f37\u60a8\u7684 MongoDB \u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"understanding-text-indexing-JCnuRGhuBF\">\u7406\u89e3\u6587\u5b57\u7d22\u5f15<\/h2>\n<p>MongoDB \u4e2d\u7684\u6587\u5b57\u7d22\u5f15\u662f\u4e00\u7a2e\u6a5f\u5236\uff0c\u5141\u8a31\u60a8\u5c0d\u5b58\u5132\u5728\u96c6\u5408\u4e2d\u7684\u6587\u672c\u6578\u64da\u57f7\u884c\u5168\u6587\u641c\u7d22\u3002\u8207\u50b3\u7d71\u7684\u7d22\u5f15\u65b9\u6cd5\u5c08\u6ce8\u65bc\u7cbe\u78ba\u5339\u914d\u4e0d\u540c\uff0c\u6587\u5b57\u7d22\u5f15\u4f7f\u60a8\u80fd\u5920\u5728\u6587\u672c\u5b57\u6bb5\u4e2d\u641c\u7d22\u95dc\u9375\u5b57\u6216\u77ed\u8a9e\uff0c\u975e\u5e38\u9069\u5408\u9700\u8981\u57fa\u65bc\u6587\u672c\u641c\u7d22\u529f\u80fd\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u7576\u60a8\u5728\u7279\u5b9a\u5b57\u6bb5\u4e0a\u5275\u5efa\u6587\u5b57\u7d22\u5f15\u6642\uff0cMongoDB \u6703\u5206\u6790\u6587\u672c\u4e26\u751f\u6210\u4e00\u500b\u7d22\u5f15\uff0c\u5c07\u6bcf\u500b\u552f\u4e00\u7684\u55ae\u8a5e\u6216\u77ed\u8a9e\u6620\u5c04\u5230\u5305\u542b\u5b83\u5011\u7684\u6587\u6a94\u3002\u7136\u5f8c\uff0c\u8a72\u7d22\u5f15\u7528\u65bc\u5728\u57f7\u884c\u6587\u672c\u641c\u7d22\u6642\u9ad8\u6548\u5730\u6aa2\u7d22\u76f8\u95dc\u6587\u6a94\u3002<\/p>\n<h2 id=\"creating-a-text-index-JCnuRGhuBF\">\u5275\u5efa\u6587\u5b57\u7d22\u5f15<\/h2>\n<p>\u8981\u5728 MongoDB \u4e2d\u5275\u5efa\u6587\u5b57\u7d22\u5f15\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>createIndex()<\/code> \u65b9\u6cd5\uff0c\u4e26\u6307\u5b9a <code>\"text\"<\/code> \u7d22\u5f15\u985e\u578b\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba &#8220;articles&#8221; \u7684\u96c6\u5408\uff0c\u5176\u4e2d\u6709\u4e00\u500b\u540d\u70ba &#8220;content&#8221; \u7684\u5b57\u6bb5\uff0c\u7528\u65bc\u5b58\u5132\u6bcf\u7bc7\u6587\u7ae0\u7684\u6587\u672c\u5167\u5bb9\uff1a<\/p>\n<pre><code>db.articles.createIndex({ content: \"text\" })\r\n<\/code><\/pre>\n<p>\u4e00\u65e6\u5275\u5efa\u4e86\u6587\u5b57\u7d22\u5f15\uff0c\u60a8\u5c31\u53ef\u4ee5\u4f7f\u7528 <code>$text<\/code> \u64cd\u4f5c\u7b26\u5728 &#8220;content&#8221; \u5b57\u6bb5\u4e0a\u958b\u59cb\u57f7\u884c\u6587\u672c\u641c\u7d22\u3002\u4f8b\u5982\uff0c\u8981\u67e5\u627e\u6240\u6709\u5305\u542b\u55ae\u8a5e &#8220;MongoDB&#8221; \u7684\u6587\u7ae0\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>db.articles.find({ $text: { $search: \"MongoDB\" } })\r\n<\/code><\/pre>\n<p>\u6587\u5b57\u7d22\u5f15\u9084\u652f\u6301\u8a9e\u8a00\u7279\u5b9a\u7684\u8a5e\u5e79\u8655\u7406\u3001\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\u548c\u505c\u6b62\u8a5e\u7b49\u9ad8\u7d1a\u529f\u80fd\u3002\u901a\u904e\u5728\u5275\u5efa\u7d22\u5f15\u6642\u6307\u5b9a\u8a9e\u8a00\u9078\u9805\uff0cMongoDB \u53ef\u4ee5\u61c9\u7528\u8a9e\u8a00\u7279\u5b9a\u7684\u898f\u5247\uff0c\u4ee5\u63d0\u9ad8\u641c\u7d22\u7684\u6e96\u78ba\u6027\u548c\u76f8\u95dc\u6027\u3002<\/p>\n<h2 id=\"limitations-and-considerations-JCnuRGhuBF\">\u9650\u5236\u548c\u8003\u91cf<\/h2>\n<p>\u96d6\u7136\u6587\u5b57\u7d22\u5f15\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u641c\u7d22\u529f\u80fd\uff0c\u4f46\u4e86\u89e3\u5176\u9650\u5236\u548c\u8003\u91cf\u4e5f\u5f88\u91cd\u8981\uff1a<\/p>\n<ul>\n<li>\u6587\u5b57\u7d22\u5f15\u50c5\u9069\u7528\u65bc\u5305\u542b\u5b57\u7b26\u4e32\u503c\u7684\u5b57\u6bb5\uff0c\u4e0d\u80fd\u7528\u65bc\u6578\u5b57\u6216\u4e8c\u9032\u5236\u5b57\u6bb5\u3002<\/li>\n<li>\u6587\u672c\u641c\u7d22\u9ed8\u8a8d\u662f\u5340\u5206\u5927\u5c0f\u5beb\u7684\u3002\u7136\u800c\uff0c\u60a8\u53ef\u4ee5\u5728\u5275\u5efa\u7d22\u5f15\u6642\u901a\u904e\u6307\u5b9a <code>\"caseSensitive\"<\/code> \u9078\u9805\u4f86\u555f\u7528\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\u3002<\/li>\n<li>\u6587\u5b57\u7d22\u5f15\u53ef\u80fd\u6703\u6d88\u8017\u5927\u91cf\u7684\u5b58\u5132\u7a7a\u9593\uff0c\u7279\u5225\u662f\u5c0d\u65bc\u5927\u578b\u96c6\u5408\u3002\u78ba\u4fdd\u60a8\u6709\u8db3\u5920\u7684\u78c1\u789f\u7a7a\u9593\u4f86\u5bb9\u7d0d\u7d22\u5f15\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<h2 id=\"conclusion-JCnuRGhuBF\">\u7d50\u8ad6<\/h2>\n<p>\u6587\u5b57\u7d22\u5f15\u662f MongoDB \u63d0\u4f9b\u7684\u4e00\u500b\u6709\u50f9\u503c\u7684\u7279\u6027\uff0c\u5141\u8a31\u60a8\u5c0d\u6587\u672c\u6578\u64da\u57f7\u884c\u9ad8\u6548\u7684\u5168\u6587\u641c\u7d22\u3002\u901a\u904e\u5728\u6240\u9700\u5b57\u6bb5\u4e0a\u5275\u5efa\u6587\u5b57\u7d22\u5f15\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5728\u96c6\u5408\u4e2d\u641c\u7d22\u95dc\u9375\u5b57\u6216\u77ed\u8a9e\u3002\u7406\u89e3\u6587\u5b57\u7d22\u5f15\u7684\u80fd\u529b\u548c\u9650\u5236\u53ef\u4ee5\u5e6b\u52a9\u60a8\u5728 MongoDB \u61c9\u7528\u4e2d\u6709\u6548\u5730\u5229\u7528\u6b64\u529f\u80fd\u3002<\/p>\n<h2 id=\"summary-JCnuRGhuBF\">\u6458\u8981<\/h2>\n<p>\u7e3d\u4e4b\uff0cMongoDB \u4e2d\u7684\u6587\u5b57\u7d22\u5f15\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u6a5f\u5236\uff0c\u4f7f\u60a8\u80fd\u5920\u5c0d\u6587\u672c\u6578\u64da\u9032\u884c\u5168\u6587\u641c\u7d22\u3002\u901a\u904e\u5728\u7279\u5b9a\u5b57\u6bb5\u4e0a\u5275\u5efa\u6587\u5b57\u7d22\u5f15\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u5728\u96c6\u5408\u4e2d\u641c\u7d22\u95dc\u9375\u5b57\u6216\u77ed\u8a9e\u3002\u8981\u4e86\u89e3\u6709\u95dc MongoDB \u53ca\u5176\u529f\u80fd\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8acb\u8003\u616e\u63a2\u7d22 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\uff0c\u9019\u662f\u4e00\u5bb6\u63d0\u4f9b\u53ef\u9760\u548c\u53ef\u64f4\u5c55\u7684 MongoDB \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\u7684\u9818\u5148 VPS \u8a17\u7ba1\u516c\u53f8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u8a5e\u5f59\u8868 &#038;#82&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21543","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21543"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21543\/revisions"}],"predecessor-version":[{"id":23915,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21543\/revisions\/23915"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}