{"id":20778,"date":"2024-01-02T05:47:23","date_gmt":"2024-01-02T05:47:23","guid":{"rendered":"https:\/\/server.hk\/blog\/20778\/"},"modified":"2024-11-14T10:38:51","modified_gmt":"2024-11-14T02:38:51","slug":"postgresql-command-create-text-search-parser","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/20778\/","title":{"rendered":"PostgreSQL \u547d\u4ee4: CREATE TEXT SEARCH PARSER"},"content":{"rendered":"<h1 id=\"postgresql-command-create-text-search-parser-MiJwUhVYvf\">PostgreSQL \u547d\u4ee4: CREATE TEXT SEARCH PARSER<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u5ee3\u6cdb\u7684\u529f\u80fd\u548c\u7279\u6027\u3002\u5176\u4e2d\u4e00\u500b\u986f\u8457\u7684\u7279\u9ede\u662f\u652f\u6301\u5168\u6587\u6aa2\u7d22\uff0c\u8b93\u7528\u6236\u80fd\u6709\u6548\u5730\u5728\u5927\u91cf\u6587\u672c\u6578\u64da\u4e2d\u641c\u5c0b\u7279\u5b9a\u7684\u8a5e\u5f59\u6216\u77ed\u8a9e\u3002\u70ba\u4e86\u589e\u5f37\u641c\u7d22\u529f\u80fd\uff0cPostgreSQL \u63d0\u4f9b\u4e86 <code>CREATE TEXT SEARCH PARSER<\/code> \u547d\u4ee4\u3002<\/p>\n<h2 id=\"introduction-to-text-search-parser-MiJwUhVYvf\">\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\u7c21\u4ecb<\/h2>\n<p>\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\u662f PostgreSQL \u4e2d\u8ca0\u8cac\u5c07\u8f38\u5165\u6587\u672c\u5206\u89e3\u6210\u500b\u5225\u6a19\u8a18\uff08Token\uff09\u6216\u8a5e\u7d20\uff08Lexeme\uff09\u7684\u7d44\u4ef6\u3002\u9019\u4e9b\u6a19\u8a18\u6703\u88ab\u7528\u65bc\u7d22\u5f15\u548c\u641c\u5c0b\u64cd\u4f5c\u3002<code>CREATE TEXT SEARCH PARSER<\/code> \u547d\u4ee4\u5141\u8a31\u7528\u6236\u5b9a\u7fa9\u548c\u5275\u5efa\u81ea\u8a02\u7684\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\uff0c\u6839\u64da\u4ed6\u5011\u7684\u5177\u9ad4\u9700\u6c42\u9032\u884c\u8abf\u6574\u3002<\/p>\n<h2 id=\"creating-a-text-search-parser-MiJwUhVYvf\">\u5275\u5efa\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668<\/h2>\n<p>\u8981\u5275\u5efa\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u6240\u9700\u7684\u914d\u7f6e\u9078\u9805\u4f86\u57f7\u884c <code>CREATE TEXT SEARCH PARSER<\/code> \u547d\u4ee4\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>CREATE TEXT SEARCH PARSER parser_name (\r\n    [option_name = option_value]\r\n    [, ...]\r\n)\r\n<\/code><\/pre>\n<p><code>parser_name<\/code> \u662f\u60a8\u60f3\u5206\u914d\u7d66\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\u7684\u540d\u7a31\uff0c\u5b83\u61c9\u5728\u6578\u64da\u5eab\u4e2d\u662f\u552f\u4e00\u7684\u3002<code>option_name<\/code> \u548c <code>option_value<\/code> \u662f\u7528\u4f86\u6307\u5b9a\u89e3\u6790\u5668\u914d\u7f6e\u9078\u9805\u7684\u914d\u5c0d\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u5011\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684\u540d\u70ba &#8220;my_parser&#8221; \u7684\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\uff0c\u5b83\u6839\u64da\u7a7a\u767d\u5b57\u7b26\u5c07\u8f38\u5165\u6587\u672c\u5206\u5272\u6210\u6a19\u8a18\uff1a<\/p>\n<pre><code>CREATE TEXT SEARCH PARSER my_parser (\r\n    start = prsd_start,\r\n    gettoken = prsd_nexttoken,\r\n    end = prsd_end,\r\n    lextypes = prsd_lextype,\r\n    headline = prsd_headline\r\n);\r\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u6307\u5b9a\u4e86\u4ee5\u4e0b\u914d\u7f6e\u9078\u9805\uff1a<\/p>\n<ul>\n<li><code>start<\/code>: \u521d\u59cb\u5316\u89e3\u6790\u5668\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<li><code>gettoken<\/code>: \u7528\u4f86\u5f9e\u8f38\u5165\u6587\u672c\u4e2d\u7372\u53d6\u4e0b\u4e00\u500b\u6a19\u8a18\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<li><code>end<\/code>: \u8655\u7406\u7d50\u675f\u5f8c\u6e05\u7406\u89e3\u6790\u5668\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<li><code>lextypes<\/code>: \u7528\u4f86\u70ba\u6bcf\u500b\u6a19\u8a18\u5206\u914d\u6a19\u8a18\u985e\u578b\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<li><code>headline<\/code>: \u70ba\u641c\u7d22\u7d50\u679c\u751f\u6210\u6458\u8981\u6216\u6a19\u984c\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u4f7f\u7528 PostgreSQL \u652f\u6301\u7684\u4efb\u4f55\u7a0b\u5e8f\u8a9e\u8a00\u4f86\u5be6\u73fe\uff0c\u4f8b\u5982 PL\/pgSQL \u6216 PL\/Python\u3002<\/p>\n<h2 id=\"using-a-text-search-parser-MiJwUhVYvf\">\u4f7f\u7528\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668<\/h2>\n<p>\u5275\u5efa\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5728\u6d89\u53ca\u5168\u6587\u6aa2\u7d22\u7684\u5404\u7a2e PostgreSQL \u547d\u4ee4\u548c\u51fd\u6578\u4e2d\u4f7f\u7528\u5b83\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728 <code>to_tsvector<\/code> \u51fd\u6578\u4e2d\u4f7f\u7528\u5b83\uff0c\u5c07\u6587\u6a94\u8f49\u63db\u70ba <code>tsvector<\/code>\uff0c\u9019\u662f\u4e00\u7a2e\u70ba\u5168\u6587\u641c\u7d22\u512a\u5316\u7684\u6578\u64da\u985e\u578b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 &#8220;my_parser&#8221; \u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>SELECT to_tsvector('my_parser', 'This is a sample document');\r\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u67e5\u8a62\u5c07\u4f7f\u7528 &#8220;my_parser&#8221; \u89e3\u6790\u5668\u751f\u6210\u8f38\u5165\u6587\u672c\u7684 <code>tsvector<\/code> \u8868\u793a\u3002<\/p>\n<h2 id=\"summary-MiJwUhVYvf\">\u7e3d\u7d50<\/h2>\n<p>PostgreSQL \u4e2d\u7684 <code>CREATE TEXT SEARCH PARSER<\/code> \u547d\u4ee4\u5141\u8a31\u7528\u6236\u5275\u5efa\u81ea\u8a02\u7684\u6587\u672c\u641c\u7d22\u89e3\u6790\u5668\uff0c\u9019\u5c0d\u589e\u5f37\u6578\u64da\u5eab\u7684\u5168\u6587\u641c\u7d22\u529f\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u5b9a\u7fa9\u81ea\u5df1\u7684\u89e3\u6790\u5668\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u7279\u5b9a\u9700\u6c42\u81ea\u5b9a\u6a19\u8a18\u5316\u904e\u7a0b\uff0c\u5f9e\u800c\u5728\u5927\u91cf\u6587\u672c\u6578\u64da\u96c6\u4e2d\u9032\u884c\u66f4\u6e96\u78ba\u548c\u9ad8\u6548\u7684\u641c\u7d22\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6709\u8208\u8da3\u63a2\u7d22 PostgreSQL \u7684\u5168\u90e8\u6f5b\u529b\u53ca\u5176\u6587\u672c\u641c\u7d22\u529f\u80fd\uff0c\u8003\u616e\u4f7f\u7528\u50cf <a href=\"https:\/\/server.hk\">Server.HK<\/a> \u9019\u6a23\u53ef\u9760\u7684 VPS \u8a17\u7ba1\u670d\u52d9\u63d0\u4f9b\u5546\u3002\u4ed6\u5011\u63d0\u4f9b\u7684\u4e00\u6d41 VPS \u89e3\u6c7a\u65b9\u6848\u80fd\u78ba\u4fdd\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u7372\u5f97\u6700\u4f73\u6027\u80fd\u548c\u64f4\u5c55\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL \u547d\u4ee4: C&#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":[4924],"tags":[],"class_list":["post-20778","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20778","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=20778"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20778\/revisions"}],"predecessor-version":[{"id":24325,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20778\/revisions\/24325"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=20778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=20778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=20778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}