{"id":6914,"date":"2026-05-08T07:11:30","date_gmt":"2026-05-07T23:11:30","guid":{"rendered":"https:\/\/www.babel.cc\/blog\/?p=6914"},"modified":"2026-05-08T07:11:30","modified_gmt":"2026-05-07T23:11:30","slug":"%e4%bc%81%e4%b8%9a%e4%ba%91%e7%9b%98%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2%e6%8a%80%e6%9c%af%e9%80%89%e5%9e%8b%ef%bc%9a%e4%bb%8eelasticsearch%e5%88%b0meilisearch%e5%88%b0typesense","status":"publish","type":"post","link":"https:\/\/www.babel.cc\/blog\/?p=6914","title":{"rendered":"\u4f01\u4e1a\u4e91\u76d8\u5168\u6587\u68c0\u7d22\u6280\u672f\u9009\u578b\uff1a\u4eceElasticsearch\u5230MeiliSearch\u5230Typesense"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u4f01\u4e1a\u4e91\u76d8\u7684\u6838\u5fc3\u4ef7\u503c\u4e4b\u4e00\u662f<strong>\u8ba9\u6587\u4ef6\u4e0d\u518d\u6c89\u7761\u5728\u76ee\u5f55\u6df1\u5904<\/strong>\u3002\u5f53\u5458\u5de5\u9700\u8981\u627e\u4e00\u4efd\u534a\u5e74\u524d\u7684\u5408\u540c\u3001\u4e00\u4efd\u4e0a\u5468\u7684\u65b9\u6848\u4fee\u8ba2\u7248\u65f6\uff0c\u68c0\u7d22\u4f53\u9a8c\u76f4\u63a5\u51b3\u5b9a\u4e86\u4e91\u76d8\u662f&#8221;\u795e\u5668&#8221;\u8fd8\u662f&#8221;\u9e21\u808b&#8221;\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4f01\u4e1a\u4e91\u76d8\u7684\u68c0\u7d22\u80fd\u529b\u957f\u671f\u662f\u91cd\u707e\u533a\u3002\u591a\u6570\u4ea7\u54c1\u5728\u5ba3\u4f20\u65f6\u53f7\u79f0&#8221;\u652f\u6301\u5168\u6587\u68c0\u7d22&#8221;\uff0c\u4f46\u5b9e\u9645\u4f7f\u7528\u4e2d\uff1a<br \/>\n&#8211; \u641c\u4e2d\u6587\u5e38\u5e38\u641c\u4e0d\u5230\uff08\u5206\u8bcd\u95ee\u9898\uff09<br \/>\n&#8211; \u641c\u5feb\u4e86\u7ecf\u5e38\u5361\u987f\uff08\u6027\u80fd\u95ee\u9898\uff09<br \/>\n&#8211; \u8bed\u4e49\u76f8\u8fd1\u7684\u8bcd\u641c\u4e0d\u5230\uff08\u540c\u4e49\u8bcd\u3001\u8fd1\u4e49\u8bcd\uff09<br \/>\n&#8211; \u641c\u7d22\u7ed3\u679c\u6392\u5e8f\u4e0d\u5408\u7406\uff08\u76f8\u5173\u6027\u7b97\u6cd5\u95ee\u9898\uff09<\/p>\n<p>\u672c\u6587\u4ece\u6280\u672f\u67b6\u6784\u5c42\u9762\uff0c\u7cfb\u7edf\u5206\u6790\u4f01\u4e1a\u4e91\u76d8\u5168\u6587\u68c0\u7d22\u7684\u6280\u672f\u9009\u578b\uff0c\u6db5\u76d6Elasticsearch\u3001MeiliSearch\u3001Typesense\u4e09\u79cd\u4e3b\u6d41\u65b9\u6848\uff0c\u4ece\u539f\u7406\u5230\u5b9e\u6218\uff0c\u4ece\u6027\u80fd\u5230\u6210\u672c\uff0c\u5e2e\u52a9\u6280\u672f\u8d1f\u8d23\u4eba\u505a\u51fa\u6b63\u786e\u51b3\u7b56\u3002<\/p>\n<hr>\n<h2>\u4e00\u3001\u4e3a\u4ec0\u4e48\u4f01\u4e1a\u4e91\u76d8\u9700\u8981\u4e13\u95e8\u7684\u5168\u6587\u68c0\u7d22\u5f15\u64ce<\/h2>\n<h3>1.1 \u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u5168\u6587\u68c0\u7d22\u5c40\u9650<\/h3>\n<p>\u5f88\u591a\u4f01\u4e1a\u4e91\u76d8\u65e9\u671f\u4f7f\u7528MySQL\/PostgreSQL\u7684LIKE\u67e5\u8be2\u6216\u57fa\u7840FULLTEXT\u7d22\u5f15\u6765\u6ee1\u8db3\u68c0\u7d22\u9700\u6c42\u3002\u8fd9\u79cd\u65b9\u6848\u5728\u5c0f\u89c4\u6a21\uff08\u6587\u4ef6\u91cf<10\u4e07\uff09\u65f6\u52c9\u5f3a\u53ef\u7528\uff0c\u4f46\u968f\u7740\u6570\u636e\u91cf\u589e\u957f\uff0c\u95ee\u9898\u5f88\u5feb\u66b4\u9732\uff1a<\/p>\n<p><strong>MySQL LIKE\u67e5\u8be2\u7684\u95ee\u9898\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">-- \u6027\u80fd\u6781\u5dee\uff0c\u5168\u8868\u626b\u63cf\r\nSELECT <em> FROM files WHERE name LIKE '%\u5408\u540c%' OR content LIKE '%\u5408\u540c%';\r\n<p>-- MySQL FULLTEXT\u5728\u4e2d\u6587\u573a\u666f\u4e0b\u6548\u679c\u4e0d\u4f73\r\n-- \u9700\u8981\u624b\u52a8\u914d\u7f6engram\u5206\u8bcd\u5668\uff0c\u914d\u7f6e\u590d\u6742\r\n<\/code><\/pre>\n<\/p>\n<p><strong>PostgreSQL tsvector\u7684\u95ee\u9898\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">-- \u9700\u624b\u52a8\u7ef4\u62a4tsvector\u5217\uff0c\u589e\u52a0\u5f00\u53d1\u590d\u6742\u5ea6\r\nALTER TABLE files ADD COLUMN search_vector tsvector;\r\nCREATE INDEX idx_search ON files USING GIN(search_vector);\r\n<p>-- \u5bf9\u4e2d\u6587\u5206\u8bcd\u652f\u6301\u540c\u6837\u9700\u8981\u989d\u5916\u914d\u7f6ezhparser\u63d2\u4ef6\r\n<\/code><\/pre>\n<\/p>\n<p>\u4e00\u65e6\u6587\u4ef6\u91cf\u8d85\u8fc750\u4e07\uff0c\u8fd9\u7c7b\u65b9\u6848\u7684\u67e5\u8be2\u5ef6\u8fdf\u4f1a\u4ece\u6beb\u79d2\u7ea7\u8df3\u5347\u5230\u79d2\u7ea7\uff0c\u76f4\u63a5\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<h3>1.2 \u4e13\u7528\u68c0\u7d22\u5f15\u64ce\u7684\u6838\u5fc3\u4ef7\u503c<\/h3>\n<p>\u4e13\u7528\u5168\u6587\u68c0\u7d22\u5f15\u64ce\uff08\u5982Elasticsearch\u3001MeiliSearch\u3001Typesense\uff09\u4ece\u67b6\u6784\u5c42\u9762\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\uff1a<\/p>\n<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n<tr>\n<th>\u80fd\u529b<\/th>\n<th>\u5173\u7cfb\u6570\u636e\u5e93<\/th>\n<th>\u4e13\u7528\u68c0\u7d22\u5f15\u64ce<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u5206\u8bcd\u7c92\u5ea6<\/td>\n<td>\u7b80\u5355\/\u9700\u63d2\u4ef6<\/td>\n<td>\u4e2d\u6587\u7cbe\u51c6\u5206\u8bcd\uff08\u7ed3\u5df4\/IK\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u5012\u6392\u7d22\u5f15<\/td>\n<td>\u65e0\/\u5f31<\/td>\n<td>\u5b8c\u6574\u5012\u6392\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td>\u76f8\u5173\u6027\u6392\u5e8f<\/td>\n<td>\u57fa\u7840<\/td>\n<td>BM25\/\u5411\u91cf\u76f8\u4f3c\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u7cca\u5339\u914d<\/td>\n<td>\u5dee<\/td>\n<td>\u652f\u6301\u524d\u7f00\/\u5bb9\u9519\u641c\u7d22<\/td>\n<\/tr>\n<tr>\n<td>\u8bed\u4e49\u68c0\u7d22<\/td>\n<td>\u65e0<\/td>\n<td>\u652f\u6301\uff08\u540c\u4e49\u8bcd\/\u8fd1\u4e49\u8bcd\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd\uff08\u767e\u4e07\u7ea7\uff09<\/td>\n<td>\u79d2\u7ea7<\/td>\n<td>\u6beb\u79d2\u7ea7<\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5c55\u6027<\/td>\n<td>\u5782\u76f4<\/td>\n<td>\u6c34\u5e73\u6269\u5c55<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>1.3 \u4f01\u4e1a\u4e91\u76d8\u68c0\u7d22\u7684\u7279\u6b8a\u6027<\/h3>\n<p>\u4f01\u4e1a\u4e91\u76d8\u7684\u68c0\u7d22\u573a\u666f\u6709\u51e0\u4e2a\u72ec\u7279\u6311\u6218\uff0c\u662f\u901a\u7528\u68c0\u7d22\u5f15\u64ce\u9700\u8981\u989d\u5916\u9002\u914d\u7684\uff1a<\/p>\n<p><strong>\u6311\u6218\u4e00\uff1a\u591a\u8bed\u8a00\u6df7\u5408\u5185\u5bb9<\/strong><br \/>\n&#8211; \u6587\u4ef6\u540d\u53ef\u80fd\u662f\u4e2d\u6587\u3001\u82f1\u6587\u3001\u7f29\u5199\u6df7\u5408<br \/>\n&#8211; \u6587\u6863\u5185\u5bb9\u53ef\u80fd\u662f\u4e2d\u82f1\u6df7\u5408<br \/>\n&#8211; \u9700\u8981\u540c\u65f6\u5904\u7406PPT\/PDF\/Word\/Excel\u7b49\u4e0d\u540c\u683c\u5f0f<br \/>\n<strong>\u6311\u6218\u4e8c\uff1a\u5143\u6570\u636e\u68c0\u7d22\u9700\u6c42<\/strong><br \/>\n&#8211; \u6309\u90e8\u95e8\u3001\u6309\u65f6\u95f4\u3001\u6309\u6587\u4ef6\u7c7b\u578b\u3001\u6309\u4e0a\u4f20\u8005\u68c0\u7d22<br \/>\n&#8211; \u7ed3\u6784\u5316\u67e5\u8be2+\u5168\u6587\u68c0\u7d22\u7684\u6df7\u5408\u9700\u6c42<br \/>\n<strong>\u6311\u6218\u4e09\uff1a\u6743\u9650\u8fc7\u6ee4<\/strong><br \/>\n&#8211; \u7528\u6237\u53ea\u80fd\u641c\u5230\u81ea\u5df1\u6709\u6743\u9650\u7684\u6587\u4ef6<br \/>\n&#8211; \u68c0\u7d22\u7ed3\u679c\u9700\u8981\u9884\u5148\u8fc7\u6ee4\uff0c\u4e0d\u662f\u4e8b\u540e\u8fc7\u6ee4<br \/>\n<strong>\u6311\u6218\u56db\uff1a\u5b9e\u65f6\u6027\u8981\u6c42<\/strong><br \/>\n&#8211; \u6587\u4ef6\u4e0a\u4f20\u540e\u9700\u8981\u5c3d\u5feb\u80fd\u88ab\u641c\u5230\uff08\u4e0d\u662fT+1\u6279\u91cf\u7d22\u5f15\uff09<br \/>\n&#8211; \u6587\u4ef6\u6539\u540d\/\u79fb\u52a8\u540e\uff0c\u641c\u7d22\u7ed3\u679c\u9700\u8981\u540c\u6b65\u66f4\u65b0<\/p>\n<hr>\n<h2>\u4e8c\u3001\u6280\u672f\u65b9\u6848\u6a2a\u8bc4\uff1aElasticsearch vs MeiliSearch vs Typesense<\/h2>\n<h3>2.1 Elasticsearch\uff1a\u529f\u80fd\u6700\u5f3a\uff0c\u590d\u6742\u5ea6\u6700\u9ad8<\/h3>\n<p><strong>\u67b6\u6784\u539f\u7406<\/strong><\/p>\n<p>Elasticsearch\u662f\u57fa\u4e8eLucene\u7684\u5206\u5e03\u5f0f\u641c\u7d22\u548c\u5206\u6790\u5f15\u64ce\u3002\u5176\u6838\u5fc3\u662f\u5012\u6392\u7d22\u5f15\uff08Inverted Index\uff09\uff1a\u5c06\u6bcf\u4e2a\u6587\u6863\u5206\u8bcd\u540e\uff0c\u5efa\u7acb&#8221;\u8bcd\u2192\u6587\u6863&#8221;\u7684\u6620\u5c04\u8868\u3002<\/p>\n<pre><code class=\"language-\">\u6587\u6863: \"\u5df4\u522b\u9e1f\u4f01\u4e1a\u4e91\u76d8\u652f\u6301\u5168\u6587\u68c0\u7d22\"\r\n\u5206\u8bcd: [\"\u5df4\u522b\u9e1f\", \"\u4f01\u4e1a\", \"\u4e91\u76d8\", \"\u652f\u6301\", \"\u5168\u6587\", \"\u68c0\u7d22\"]\r\n\u5012\u6392\u7d22\u5f15:\r\n  \u5df4\u522b\u9e1f \u2192 [doc_1, doc_5, doc_9]\r\n  \u4f01\u4e1a \u2192 [doc_1, doc_3, doc_7, doc_11]\r\n  \u5168\u6587 \u2192 [doc_1, doc_2]\r\n  ...\r\n<\/code><\/pre>\n<p><strong>\u4f01\u4e1a\u4e91\u76d8\u573a\u666f\u7684\u914d\u7f6e\u793a\u4f8b<\/strong><\/p>\n<pre><code class=\"language-yaml\"># elasticsearch.yml - \u4f01\u4e1a\u4e91\u76d8\u7d22\u5f15\u914d\u7f6e\r\nindex:\r\n  number_of_shards: 3\r\n  number_of_replicas: 1\r\n<p>analysis:\r\n  analyzer:\r\n    chinese_analyzer:\r\n      type: custom\r\n      tokenizer: ik_max_word\r\n      filter:\r\n        - traditional_chinese_convert  # \u7e41\u7b80\u8f6c\u6362\r\n        - synonym_filter               # \u540c\u4e49\u8bcd<\/p>\r\n<h1>\u6587\u4ef6\u5143\u6570\u636emapping<\/h1>\r\nmappings:\r\n  properties:\r\n    file_id: { type: keyword }\r\n    file_name: { \r\n      type: text, \r\n      analyzer: chinese_analyzer,\r\n      fields:\r\n        keyword: { type: keyword }\r\n    }\r\n    content: { type: text, analyzer: chinese_analyzer }\r\n    department: { type: keyword }\r\n    tags: { type: keyword }\r\n    uploaded_by: { type: keyword }\r\n    uploaded_at: { type: date }\r\n    file_size: { type: long }\r\n    permissions: { type: keyword }  # \u6743\u9650\u5b57\u6bb5\u7528\u4e8e\u8fc7\u6ee4\r\n<\/code><\/pre>\n<p><strong>\u4f18\u7f3a\u70b9\u5206\u6790<\/strong><\/p>\n<p>\u4f18\u70b9\uff1a<br \/>\n&#8211; \u529f\u80fd\u6700\u5168\uff1a\u652f\u6301\u590d\u6742\u7684\u805a\u5408\u67e5\u8be2\u3001\u5411\u91cf\u68c0\u7d22\uff08\u901a\u8fc7\u63d2\u4ef6\uff09\u3001\u81ea\u5b9a\u4e49\u8bc4\u5206\u811a\u672c<br \/>\n&#8211; \u751f\u6001\u6210\u719f\uff1a\u6709Kibana\u53ef\u89c6\u5316\u3001Cerebro\u76d1\u63a7\u3001\u4e30\u5bcc\u7684\u5ba2\u6237\u7aef\u5e93<br \/>\n&#8211; \u6269\u5c55\u6027\u5f3a\uff1a\u5929\u7136\u652f\u6301\u96c6\u7fa4\uff0c\u6570\u636e\u91cf\u4eceGB\u5230TB\u8f7b\u677e\u6269\u5c55<br \/>\n&#8211; \u4e2d\u6587\u652f\u6301\u597d\uff1aIK\u5206\u8bcd\u5668\u662f\u4e2d\u6587\u68c0\u7d22\u7684\u4e8b\u5b9e\u6807\u51c6<\/p>\n<p>\u7f3a\u70b9\uff1a<br \/>\n&#8211; <strong>\u90e8\u7f72\u8fd0\u7ef4\u590d\u6742<\/strong>\uff1a\u9700\u8981\u4e13\u4e1a\u7684\u8fd0\u7ef4\u56e2\u961f\uff0cES\u96c6\u7fa4\u8c03\u4f18\u662f\u4e2a\u4e13\u4e1a\u6d3b<br \/>\n&#8211; <strong>\u8d44\u6e90\u6d88\u8017\u5927<\/strong>\uff1aJVM\u5806\u5185\u5b58\u9ed8\u8ba41GB\u8d77\u6b65\uff0c\u5c0f\u89c4\u6a21\u90e8\u7f72\u6210\u672c\u9ad8<br \/>\n&#8211; <strong>\u914d\u7f6e\u95e8\u69db\u9ad8<\/strong>\uff1a\u5206\u8bcd\u5668\u3001mapping\u3001\u67e5\u8be2DSL\u9700\u8981\u6df1\u5165\u5b66\u4e60<br \/>\n&#8211; <strong>\u5199\u5165\u6027\u80fd\u76f8\u5bf9\u5f31<\/strong>\uff1a\u8fd1\u5b9e\u65f6\uff08NRT\uff09\u5ef6\u8fdf\u7ea61\u79d2\uff0c\u4e0d\u53caMeiliSearch<\/p>\n<p><strong>\u9002\u5408\u573a\u666f<\/strong><br \/>\n&#8211; \u6587\u4ef6\u91cf>500\u4e07\u6761<br \/>\n&#8211; \u9700\u8981\u590d\u6742\u67e5\u8be2\uff08\u805a\u5408\u3001\u5206\u6876\u3001\u591a\u6761\u4ef6\u7ec4\u5408\uff09<br \/>\n&#8211; \u6709\u4e13\u804c\u641c\u7d22\u56e2\u961f\u6216\u8fd0\u7ef4\u56e2\u961f<br \/>\n&#8211; \u9884\u7b97\u5145\u8db3\uff08\u81f3\u5c113\u53f08C16G\u670d\u52a1\u5668\uff09<br \/>\n<strong>\u5178\u578b\u8e29\u5751\u8bb0\u5f55<\/strong><\/p>\n<p>\u8e29\u57511\uff1aIK\u5206\u8bcd\u5668\u7248\u672c\u4e0d\u5339\u914d\u5bfc\u81f4\u7d22\u5f15\u635f\u574f<\/p>\n<pre><code class=\"language-\">\u95ee\u9898\uff1a\u5347\u7ea7ES\u540e\uff0cIK\u5206\u8bcd\u5668\u7248\u672c\u4e0d\u517c\u5bb9\uff0c\u7d22\u5f15\u65e0\u6cd5\u6253\u5f00\r\n\u89e3\u51b3\uff1a\u59cb\u7ec8\u4fdd\u8bc1IK\u7248\u672c\u4e0eES\u5927\u7248\u672c\u4e00\u81f4\uff0c\u5347\u7ea7\u524d\u5148\u5728\u6d4b\u8bd5\u73af\u5883\u9a8c\u8bc1\r\n<\/code><\/pre>\n<\/p>\n<p>\u8e29\u57512\uff1a\u8111\u88c2\u95ee\u9898\uff08Split-Brain\uff09<\/p>\n<pre><code class=\"language-\">\u95ee\u9898\uff1a3\u8282\u70b9\u96c6\u7fa4\u7f51\u7edc\u6296\u52a8\u540e\uff0c2\u4e2a\u8282\u70b9\u4e92\u76f8\u8ba4\u4e3a\u5bf9\u65b9\u6302\u4e86\uff0c\u5404\u81ea\u79f0master\r\n\u89e3\u51b3\uff1a\u5408\u7406\u8bbe\u7f6ediscovery.zen.minimum_master_nodes = (nodes\/2)+1\r\n\u63a8\u8350\u4f7f\u7528 dedicated master nodes \u5206\u79bb\u89d2\u8272\r\n<\/code><\/pre>\n<\/p>\n<p>\u8e29\u57513\uff1amapping\u7206\u70b8<\/p>\n<pre><code class=\"language-\">\u95ee\u9898\uff1a\u5b57\u6bb5\u7c7b\u578b\u8bbe\u7f6e\u4e0d\u5f53\uff0cnested\u7c7b\u578b\u5d4c\u5957\u8fc7\u6df1\r\n\u89e3\u51b3\uff1a\u8bbe\u7f6e index.mapping.total_fields.limit: 2000\r\n<\/code><\/pre>\n<\/p>\n<hr>\n<h3>2.2 MeiliSearch\uff1a\u7b80\u5355\u5230\u4ee4\u4eba\u53d1\u6307\uff0c\u6027\u80fd\u5374\u51fa\u4e4e\u610f\u6599<\/h3>\n<p><strong>\u67b6\u6784\u539f\u7406<\/strong><\/p>\n<p>MeiliSearch\u662fRust\u5b9e\u73b0\u7684\u8f7b\u91cf\u7ea7\u5168\u6587\u68c0\u7d22\u5f15\u64ce\uff0c\u6838\u5fc3\u8bbe\u8ba1\u7406\u5ff5\u662f&#8221;\u5f00\u7bb1\u5373\u7528\u7684\u597d\u4f53\u9a8c&#8221;\u3002\u5b83\u5185\u7f6e\u4e86\uff1a<br \/>\n&#8211; \u5168\u6587\u5206\u8bcd\uff08\u57fa\u4e8earroy\u4ed3\u5e93\u7684stemmer\uff0c\u652f\u6301\u4e2d\u6587\uff09<br \/>\n&#8211; \u76f8\u5173\u6027\u6392\u5e8f\uff08\u57fa\u4e8eBM25\uff0c\u53c2\u6570\u53ef\u8c03\uff09<br \/>\n&#8211; \u9519\u5b57\u5bb9\u5fcd\uff08Typo tolerance\uff0c\u81ea\u52a8\u7ea0\u9519\uff09<br \/>\n&#8211; \u524d\u7f00\u641c\u7d22\uff08\u8f93\u5165&#8221;\u5408\u540c&#8221;\u81ea\u52a8\u641c\u7d22&#8221;\u5408\u540c\u4fee\u8ba2\u7248&#8221;\uff09<\/p>\n<p><strong>\u4f01\u4e1a\u4e91\u76d8\u573a\u666f\u7684\u914d\u7f6e\u793a\u4f8b<\/strong><\/p>\n<pre><code class=\"language-rust\">\/\/ MeiliSearch\u7d22\u5f15\u914d\u7f6e\uff08\u901a\u8fc7HTTP API\uff09\r\nPOST \/indexes\/files\/settings\r\n<p>{\r\n  \"searchableAttributes\": [\r\n    \"file_name\",      \/\/ \u6587\u4ef6\u540d\u6743\u91cd\u6700\u9ad8\r\n    \"content\",        \/\/ \u6b63\u6587\u5185\u5bb9\r\n    \"tags\",           \/\/ \u6807\u7b7e\r\n    \"department\"      \/\/ \u90e8\u95e8\uff08\u53ef\u641c\u7d22\u4f46\u6743\u91cd\u4f4e\uff09\r\n  ],\r\n  \"filterableAttributes\": [\r\n    \"department\",      \/\/ \u652f\u6301\u6309\u90e8\u95e8\u8fc7\u6ee4\r\n    \"file_type\",      \/\/ \u652f\u6301\u6309\u7c7b\u578b\u8fc7\u6ee4\r\n    \"uploaded_by\",    \/\/ \u652f\u6301\u6309\u4e0a\u4f20\u8005\u8fc7\u6ee4\r\n    \"permissions\",    \/\/ \u6743\u9650\u8fc7\u6ee4\r\n    \"uploaded_at\"     \/\/ \u652f\u6301\u6309\u65f6\u95f4\u8fc7\u6ee4\r\n  ],\r\n  \"sortableAttributes\": [\r\n    \"uploaded_at\",     \/\/ \u652f\u6301\u6309\u65f6\u95f4\u6392\u5e8f\r\n    \"file_size\"       \/\/ \u652f\u6301\u6309\u5927\u5c0f\u6392\u5e8f\r\n  ],\r\n  \"typoTolerance\": {\r\n    \"enabled\": true,\r\n    \"minWordSizeForTypos\": {\r\n      \"oneTypo\": 4,    \/\/ 4\u5b57\u4ee5\u4e0a\u5141\u8bb81\u4e2a\u9519\u5b57\r\n      \"twoTypos\": 8    \/\/ 8\u5b57\u4ee5\u4e0a\u5141\u8bb82\u4e2a\u9519\u5b57\r\n    }\r\n  },\r\n  \"pagination\": {\r\n    \"maxTotalHits\": 1000000  \/\/ \u652f\u6301\u767e\u4e07\u7ea7\u7ed3\u679c\u5206\u9875\r\n  }\r\n}\r\n<\/code><\/pre>\n<\/p>\n<pre><code class=\"language-python\"># Python\u5ba2\u6237\u7aef\u793a\u4f8b\uff1a\u4f01\u4e1a\u4e91\u76d8\u68c0\u7d22\r\nimport meilisearch\r\n<p>client = meilisearch.Client('http:\/\/localhost:7700', 'master_key')<\/p>\r\n<p>def search_files(user_id: str, query: str, department: str = None):\r\n    # \u6784\u5efa\u6743\u9650\u8fc7\u6ee4\u6761\u4ef6\r\n    filters = [f\"permissions CONTAINS '{user_id}'\"]\r\n    if department:\r\n        filters.append(f\"department = '{department}'\")\r\n    \r\n    result = client.index('files').search(query, {\r\n        'attributesToHighlight': ['file_name', 'content'],\r\n        'attributesToCrop': ['content'],\r\n        'cropLength': 200,\r\n        'filter': ' AND '.join(filters),\r\n        'limit': 20,\r\n        'sort': ['uploaded_at:desc']\r\n    })\r\n    return result<\/p>\r\n<h1>\u4f7f\u7528\u793a\u4f8b<\/h1>\r\nresults = search_files(\r\n    user_id='user_12345',\r\n    query='\u5408\u540c\u4fee\u8ba2\u7248',\r\n    department='\u5546\u52a1'\r\n)\r\nfor hit in results['hits']:\r\n    print(f\"{hit['file_name']} - \u5339\u914d\u7247\u6bb5: {hit['_formatted']['content']}\")\r\n<\/code><\/pre>\n<p><strong>\u4f18\u7f3a\u70b9\u5206\u6790<\/strong><\/p>\n<p>\u4f18\u70b9\uff1a<br \/>\n&#8211; <strong>\u90e8\u7f72\u6781\u7b80<\/strong>\uff1a\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c<code>.\/meilisearch<\/code>\u5373\u53ef\u542f\u52a8<br \/>\n&#8211; <strong>\u5f00\u7bb1\u5373\u7528\u597d<\/strong>\uff1a\u65e0\u9700\u590d\u6742\u7684\u5206\u8bcd\u914d\u7f6e\uff0c\u9ed8\u8ba4\u4e2d\u6587\u652f\u6301\u53ef\u7528<br \/>\n&#8211; <strong>\u5199\u5165\u6027\u80fd\u5f3a<\/strong>\uff1a\u5b98\u65b9\u5ba3\u4f20<50ms\u7684\u7d22\u5f15\u5ef6\u8fdf\uff08\u5b9e\u6d4b\u7ea630-80ms\uff09\n- <strong>\u5185\u5b58\u5360\u7528\u5c0f<\/strong>\uff1aRust\u5b9e\u73b0\uff0c64MB\u5806\u5185\u5b58\u5373\u53ef\u8fd0\u884c\uff08\u751f\u4ea7\u63a8\u8350256MB+\uff09<br \/>\n&#8211; <strong>\u9519\u5b57\u5bb9\u5fcd<\/strong>\uff1a\u7528\u6237\u8f93\u9519\u5b57\u4e5f\u80fd\u641c\u5230\uff0c\u8fd9\u662fES\u505a\u4e0d\u5230\u7684<\/p>\n<p>\u7f3a\u70b9\uff1a<br \/>\n&#8211; <strong>\u529f\u80fd\u76f8\u5bf9ES\u5c11<\/strong>\uff1a\u4e0d\u652f\u6301\u5d4c\u5957\u67e5\u8be2\u3001\u805a\u5408\u5206\u6790\uff08ES\u7684agg\u529f\u80fd\uff09<br \/>\n&#8211; <strong>\u5411\u91cf\u68c0\u7d22\u9700\u8981\u63d2\u4ef6<\/strong>\uff08meilisearch-vec\uff09\u6216\u5916\u90e8\u5411\u91cf\u5f15\u64ce\u914d\u5408<br \/>\n&#8211; <strong>\u96c6\u7fa4\u529f\u80fd\u4f01\u4e1a\u7248\u624d\u6709<\/strong>\uff1a\u5f00\u6e90\u7248\u662f\u5355\u8282\u70b9\uff0c\u96c6\u7fa4\u9700\u8981\u4ed8\u8d39<br \/>\n&#8211; <strong>\u4e2d\u6587\u5206\u8bcd\u4e0d\u5982IK\u7cbe\u7ec6<\/strong>\uff1a\u5185\u7f6estemmer\u5bf9\u4e2d\u6587\u5904\u7406\u8f83\u7c97\u66b4\uff08\u6309\u5b57\u7b26\u62c6\u5206\uff09<\/p>\n<p><strong>\u9002\u5408\u573a\u666f<\/strong><br \/>\n&#8211; \u6587\u4ef6\u91cf\u572850\u4e07-500\u4e07\u4e4b\u95f4<br \/>\n&#8211; \u4e0d\u9700\u8981\u590d\u6742\u7684\u805a\u5408\u67e5\u8be2<br \/>\n&#8211; \u6ca1\u6709\u4e13\u804c\u8fd0\u7ef4\u56e2\u961f<br \/>\n&#8211; \u5e0c\u671b\u5feb\u901f\u4e0a\u7ebf\u3001\u8fed\u4ee3\u4f18\u5316<br \/>\n<strong>\u5178\u578b\u8e29\u5751\u8bb0\u5f55<\/strong><\/p>\n<p>\u8e29\u57511\uff1a\u4e2d\u6587\u5206\u8bcd\u6548\u679c\u4e0d\u7406\u60f3<\/p>\n<pre><code class=\"language-\">\u95ee\u9898\uff1aMeiliSearch\u5185\u7f6e\u5206\u8bcd\u6309\u5b57\u7b26\u5207\u5206\uff0c\"\u4f01\u4e1a\u4e91\u76d8\"\u88ab\u62c6\u6210\"\u4f01\/\u4e1a\/\u4e91\/\u76d8\"\r\n\u89e3\u51b3\uff1a\u4f7f\u7528 meilisearch-tokenizer \u6216 ik-analyzer \u5bf9\u63a5\r\n      \u901a\u8fc7 ingest-processing pipeline \u9884\u5904\u7406\u6587\u672c\u518d\u5199\u5165MeiliSearch\r\n<\/code><\/pre>\n<\/p>\n<p>\u8e29\u57512\uff1afilter\u6761\u4ef6\u6027\u80fd\u95ee\u9898<\/p>\n<pre><code class=\"language-\">\u95ee\u9898\uff1apermissions\u5b57\u6bb5\u4f7f\u7528CONTAINS\uff0c\u5927\u91cf\u8fc7\u6ee4\u65f6\u6027\u80fd\u4e0b\u964d\r\n\u89e3\u51b3\uff1a\u8bbe\u8ba1\u5408\u7406\u7684\u6743\u9650\u6570\u636e\u7ed3\u6784\r\n      \u65b9\u6848A\uff1a\u6309\u90e8\u95e8\/\u89d2\u8272\u5efa\u7d22\u5f15\u5206\u533a\uff08tenant isolation\uff09\r\n      \u65b9\u6848B\uff1a\u6743\u9650\u5b57\u6bb5\u5355\u72ec\u7ef4\u62a4\uff0c\u7528POST\u8fc7\u6ee4\u524d\u9884\u8fc7\u6ee4\r\n<\/code><\/pre>\n<\/p>\n<p>\u8e29\u57513\uff1a\u7d22\u5f15\u66f4\u65b0\u5ef6\u8fdf<\/p>\n<pre><code class=\"language-\">\u95ee\u9898\uff1a\u5927\u6279\u91cf\u6587\u4ef6\u4e0a\u4f20\u540e\uff0c\u641c\u7d22\u4e0d\u5230\uff08\u7d22\u5f15\u66f4\u65b0\u6709\u5ef6\u8fdf\uff09\r\n\u89e3\u51b3\uff1a\u7406\u89e3MeiliSearch\u7684\"\u7d22\u5f15\u4efb\u52a1\u961f\u5217\"\u673a\u5236\r\n      \u4f7f\u7528 wait_for_task() \u786e\u4fdd\u6587\u4ef6\u88ab\u7d22\u5f15\u540e\u518d\u8fd4\u56de\r\n<\/code><\/pre>\n<\/p>\n<hr>\n<h3>2.3 Typesense\uff1a\u5f00\u6e90+\u4e91\u539f\u751f\u53cb\u597d\u7684\u8f7b\u91cf\u9009\u624b<\/h3>\n<p><strong>\u67b6\u6784\u539f\u7406<\/strong><\/p>\n<p>Typesense\u540c\u6837\u662fRust\u5b9e\u73b0\uff0c\u5b9a\u4f4d\u662f&#8221;Elasticsearch\u7684\u66ff\u4ee3\u54c1\uff0c\u4f46\u66f4\u7b80\u5355&#8221;\u3002\u5b83\u7684\u8bbe\u8ba1\u76ee\u6807\u662f\uff1a<br \/>\n&#8211; \u4f4e\u5ef6\u8fdf\uff08<50ms P99\uff09\n- \u7b80\u5355\u8fd0\u7ef4\uff08\u5355\u4e8c\u8fdb\u5236\uff0ck8s\u53cb\u597d\uff09\n- \u5bb9\u9519\u641c\u7d22\uff08\u7c7b\u4f3cMeiliSearch\u7684typo tolerance\uff09<\/p>\n<p>\u6700\u5927\u7684\u533a\u522b\u662fTypesense\u5bf9\u4e91\u539f\u751f\u548c\u81ea\u6258\u7ba1\u573a\u666f\u66f4\u53cb\u597d\uff0c\u800cMeiliSearch\u66f4\u504f\u5411\u5f00\u7bb1\u5373\u7528\u7684\u7ec8\u7aef\u7528\u6237\u573a\u666f\u3002<\/p>\n<p><strong>\u4f01\u4e1a\u4e91\u76d8\u573a\u666f\u7684\u914d\u7f6e\u793a\u4f8b<\/strong><\/p>\n<pre><code class=\"language-json\">\/\/ Typesense collection\u914d\u7f6e\r\nPOST \/collections\r\n<p>{\r\n  \"name\": \"files\",\r\n  \"fields\": [\r\n    {\"name\": \"file_id\", \"type\": \"string\", \"index\": true },\r\n    {\"name\": \"file_name\", \"type\": \"string\", \"index\": true },\r\n    {\"name\": \"content\", \"type\": \"string\", \"index\": true },\r\n    {\"name\": \"department\", \"type\": \"string\", \"facet\": true },\r\n    {\"name\": \"tags\", \"type\": \"string\", \"index\": true, \"facet\": true },\r\n    {\"name\": \"uploaded_by\", \"type\": \"string\", \"facet\": true },\r\n    {\"name\": \"uploaded_at\", \"type\": \"int64\" },  \/\/ Unix timestamp\r\n    {\"name\": \"permissions\", \"type\": \"string\", \"index\": true }\r\n  ],\r\n  \"default_sorting_field\": \"uploaded_at\"\r\n}\r\n<\/code><\/pre>\n<\/p>\n<pre><code class=\"language-python\"># Python\u5ba2\u6237\u7aef\u793a\u4f8b\r\nimport typesense\r\n<p>client = typesense.Client({\r\n    'master_node': {\r\n        'host': 'localhost',\r\n        'port': '8108',\r\n        'protocol': 'http'\r\n    }\r\n})<\/p>\r\n<p>def search_files(query: str, user_id: str, filters: dict = None):\r\n    # \u6784\u5efa\u6743\u9650\u8fc7\u6ee4\r\n    filter_by = [f\"permissions:={user_id}\"]\r\n    if filters:\r\n        for k, v in filters.items():\r\n            filter_by.append(f\"{k}:={v}\")\r\n    \r\n    search_params = {\r\n        'q': query,\r\n        'query_by': 'file_name,content,tags',\r\n        'filter_by': ' && '.join(filter_by),\r\n        'sort_by': 'uploaded_at:desc',\r\n        'facet_by': 'department,tags,uploaded_by',\r\n        'max_results': 20\r\n    }\r\n    \r\n    return client.collections['files'].documents.search(search_params)\r\n<\/code><\/pre>\n<\/p>\n<p><strong>\u4f18\u7f3a\u70b9\u5206\u6790<\/strong><\/p>\n<p>\u4f18\u70b9\uff1a<br \/>\n&#8211; <strong>\u4e91\u539f\u751f\u53cb\u597d<\/strong>\uff1a\u5b98\u65b9\u63d0\u4f9bHelm Chart\/Docker Compose\uff0ck8s\u90e8\u7f72\u7b80\u5355<br \/>\n&#8211; <strong>\u8d44\u6e90\u5360\u7528\u6781\u4f4e<\/strong>\uff1a\u5b9e\u6d4b\u5185\u5b58\u5360\u7528\u7ea680MB\uff08ES\u81f3\u5c111GB+\uff09<br \/>\n&#8211; <strong>\u5ef6\u8fdf\u6781\u4f4e<\/strong>\uff1aP99\u5ef6\u8fdf<50ms\uff08ES\u5728\u590d\u6742\u67e5\u8be2\u4e0bP99\u7ea6200-500ms\uff09\n- <strong>Schema\u7075\u6d3b<\/strong>\uff1a\u5b57\u6bb5\u7c7b\u578b\u5b9a\u4e49\u7b80\u6d01\uff0c\u52a8\u6001\u5b57\u6bb5\u652f\u6301\u597d<\/p>\n<p>\u7f3a\u70b9\uff1a<br \/>\n&#8211; <strong>\u751f\u6001\u4e0d\u5982ES<\/strong>\uff1a\u7f3a\u5c11Kibana\u90a3\u6837\u7684\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u9700\u8981\u81ea\u5df1\u642d\u76d1\u63a7<br \/>\n&#8211; <strong>\u4e2d\u6587\u5206\u8bcd\u9700\u8981\u989d\u5916\u914d\u7f6e<\/strong>\uff1a\u540c\u6837\u9700\u8981\u5bf9\u63a5IK\u6216\u5176\u4ed6\u5206\u8bcd\u670d\u52a1<br \/>\n&#8211; <strong>\u793e\u533a\u89c4\u6a21\u8f83\u5c0f<\/strong>\uff1a\u9047\u5230\u95ee\u9898\u641c\u7d22\u5230\u7684\u89e3\u51b3\u65b9\u6848\u8f83\u5c11<br \/>\n&#8211; <strong>\u805a\u5408\u529f\u80fd\u5f31<\/strong>\uff1a\u53ea\u6709\u57fa\u672c\u7684facet\uff08\u5206\u9762\uff09\u529f\u80fd<\/p>\n<p><strong>\u9002\u5408\u573a\u666f<\/strong><br \/>\n&#8211; \u6587\u4ef6\u91cf<200\u4e07\u6761\n- \u5df2\u6709k8s\u57fa\u7840\u8bbe\u65bd\uff0c\u5e0c\u671b\u68c0\u7d22\u670d\u52a1\u5bb9\u5668\u5316\n- \u9884\u7b97\u6709\u9650\uff0c\u65e0\u6cd5\u652f\u6491ES\u7684\u673a\u5668\u6210\u672c\n- \u5ef6\u8fdf\u654f\u611f\uff08\u641c\u7d22\u54cd\u5e94\u5fc5\u987b<100ms\uff09\n\n\n<hr>\n<h2>\u4e09\u3001\u6a2a\u5411\u5bf9\u6bd4\uff1a\u4e09\u79cd\u5f15\u64ce\u7684\u6838\u5fc3\u6307\u6807<\/h2>\n<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n<tr>\n<th>\u6307\u6807<\/th>\n<th>Elasticsearch<\/th>\n<th>MeiliSearch<\/th>\n<th>Typesense<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u8bed\u8a00<\/td>\n<td>Java<\/td>\n<td>Rust<\/td>\n<td>Rust<\/td>\n<\/tr>\n<tr>\n<td>\u6700\u4f4e\u5185\u5b58<\/td>\n<td>1GB+\uff08\u5efa\u8bae4GB+\uff09<\/td>\n<td>64MB\uff08\u5efa\u8bae256MB+\uff09<\/td>\n<td>64MB\uff08\u5efa\u8bae128MB+\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u7d22\u5f15\u5ef6\u8fdf<\/td>\n<td>~1\u79d2\uff08NRT\uff09<\/td>\n<td>30-80ms<\/td>\n<td>50ms<\/td>\n<\/tr>\n<tr>\n<td>\u641c\u7d22\u5ef6\u8fdf<\/td>\n<td>100-500ms<\/td>\n<td><50ms<\/td>\n<td><50ms<\/td>\n<\/tr>\n<tr>\n<td>\u4e2d\u6587\u5206\u8bcd<\/td>\n<td>IK\uff08\u6210\u719f\uff09<\/td>\n<td>\u9700\u63d2\u4ef6\/\u9884\u5904\u7406<\/td>\n<td>\u9700\u63d2\u4ef6\/\u9884\u5904\u7406<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u5b57\u5bb9\u5fcd<\/td>\n<td>\u274c\uff08\u9700\u63d2\u4ef6\uff09<\/td>\n<td>\u2705\uff08\u5185\u7f6e\uff09<\/td>\n<td>\u2705\uff08\u5185\u7f6e\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u6743\u9650\u8fc7\u6ee4<\/td>\n<td>\u2705\uff08filter\uff09<\/td>\n<td>\u2705\uff08filter\uff09<\/td>\n<td>\u2705\uff08filter\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u96c6\u7fa4<\/td>\n<td>\u2705\uff08\u5f00\u6e90\uff09<\/td>\n<td>\u274c\uff08\u4f01\u4e1a\u7248\uff09<\/td>\n<td>\u2705\uff08\u5f00\u6e90\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u5411\u91cf\u68c0\u7d22<\/td>\n<td>\u2705\uff08\u63d2\u4ef6\uff09<\/td>\n<td>\u26a0\ufe0f\uff08\u63d2\u4ef6\uff09<\/td>\n<td>\u274c\uff08\u9700\u5916\u6302\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u8fd0\u7ef4\u96be\u5ea6<\/td>\n<td>\u9ad8<\/td>\n<td>\u4f4e<\/td>\n<td>\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>\u5b66\u4e60\u66f2\u7ebf<\/td>\n<td>\u9661\u5ced<\/td>\n<td>\u5e73\u7f13<\/td>\n<td>\u5e73\u7f13<\/td>\n<\/tr>\n<tr>\n<td>\u767e\u4e07\u6587\u4ef6\u6027\u80fd<\/td>\n<td>\u2705<\/td>\n<td>\u2705<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>\u5343\u4e07\u6587\u4ef6\u6027\u80fd<\/td>\n<td>\u2705<\/td>\n<td>\u26a0\ufe0f\uff08\u9700\u5206\u533a\uff09<\/td>\n<td>\u26a0\ufe0f\uff08\u9700\u5206\u533a\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2>\u56db\u3001\u67b6\u6784\u8bbe\u8ba1\uff1a\u4f01\u4e1a\u4e91\u76d8\u68c0\u7d22\u7cfb\u7edf\u7684\u5b8c\u6574\u65b9\u6848<\/h2>\n<h3>4.1 \u6574\u4f53\u67b6\u6784<\/h3>\n<pre><code class=\"language-\">\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502 \u6587\u4ef6\u4e0a\u4f20\/   \u2502\u2500\u2500\u2500\u25b6\u2502 \u6587\u4ef6\u5904\u7406\u670d\u52a1  \u2502\u2500\u2500\u2500\u25b6\u2502 \u68c0\u7d22\u7d22\u5f15\u670d\u52a1    \u2502\r\n\u2502 \u7f16\u8f91\/\u5220\u9664   \u2502    \u2502 (\u5185\u5bb9\u63d0\u53d6)    \u2502    \u2502 (ES\/Meili\/TS) \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n                          \u2502                    \u2502\r\n                          \u25bc                    \u25bc\r\n                   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n                   \u2502 \u5bf9\u8c61\u5b58\u50a8      \u2502    \u2502 \u641c\u7d22\u524d\u7aef\u670d\u52a1    \u2502\r\n                   \u2502 (\u6587\u4ef6\u672c\u4f53)    \u2502    \u2502 (\u67e5\u8be2\/\u8fc7\u6ee4)    \u2502\r\n                   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n                                                \u2502\r\n                                                \u25bc\r\n                                        \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n                                        \u2502 \u7528\u6237\u8bf7\u6c42        \u2502\r\n                                        \u2502 (\u5e26\u6743\u9650\u4fe1\u606f)    \u2502\r\n                                        \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<h3>4.2 \u6587\u4ef6\u5185\u5bb9\u63d0\u53d6\uff08Content Extraction\uff09<\/h3>\n<p>\u8fd9\u662f\u6700\u5bb9\u6613\u88ab\u5ffd\u89c6\u7684\u73af\u8282\u3002\u518d\u597d\u7684\u68c0\u7d22\u5f15\u64ce\uff0c\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u63d0\u53d6\u8d28\u91cf\u5dee\uff0c\u641c\u7d22\u7ed3\u679c\u4e5f\u662f\u5783\u573e\u3002<\/p>\n<p><strong>PDF\u5185\u5bb9\u63d0\u53d6\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">import subprocess\r\n<p>def extract_pdf_text(file_path: str) -&gt; str:\r\n    \"\"\"\u4f7f\u7528pdftotext\u63d0\u53d6PDF\u6587\u672c\u5185\u5bb9\"\"\"\r\n    try:\r\n        result = subprocess.run(\r\n            ['pdftotext', '-layout', file_path, '-'],\r\n            capture_output=True,\r\n            text=True,\r\n            timeout=30\r\n        )\r\n        return result.stdout\r\n    except Exception as e:\r\n        return f\"[PDF\u63d0\u53d6\u5931\u8d25: {e}]\"<\/p>\r\n<p>def extract_office_text(file_path: str) -&gt; str:\r\n    \"\"\"\u4f7f\u7528libreoffice\u63d0\u53d6Office\u6587\u6863\u6587\u672c\"\"\"\r\n    # \u5148\u8f6cPDF\uff0c\u518d\u63d0\u6587\u672c\uff08\u517c\u5bb9\u6027\u597d\uff09\r\n    pdf_path = file_path.replace('.docx', '.pdf').replace('.xlsx', '.pdf')\r\n    try:\r\n        subprocess.run(\r\n            ['soffice', '--headless', '--convert-to', 'pdf', '--outdir', '\/tmp', file_path],\r\n            capture_output=True,\r\n            timeout=60\r\n        )\r\n        return extract_pdf_text(pdf_path)\r\n    except Exception as e:\r\n        return f\"[Office\u63d0\u53d6\u5931\u8d25: {e}]\"\r\n<\/code><\/pre>\n<\/p>\n<p><strong>\u56fe\u7247OCR\uff08\u626b\u63cf\u4ef6\/\u622a\u56fe\uff09\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">import pytesseract\r\nfrom PIL import Image\r\n<p>def extract_image_text(image_path: str) -&gt; str:\r\n    \"\"\"OCR\u8bc6\u522b\u56fe\u7247\u4e2d\u7684\u6587\u5b57\"\"\"\r\n    try:\r\n        img = Image.open(image_path)\r\n        text = pytesseract.image_to_string(img, lang='chi_sim+eng')\r\n        return text\r\n    except Exception as e:\r\n        return f\"[OCR\u5931\u8d25: {e}]\"<\/p>\r\n<h1>\u8fdb\u9636\uff1a\u4f7f\u7528\u767e\u5ea6\/\u963f\u91ccOCR API\uff0c\u652f\u6301\u66f4\u591a\u683c\u5f0f\uff0c\u8bc6\u522b\u7387\u66f4\u9ad8<\/h1>\r\ndef extract_image_text_api(image_path: str) -&gt; str:\r\n    \"\"\"\u4f7f\u7528\u767e\u5ea6OCR API\uff08\u9700\u914d\u7f6eAPP_ID\/API_KEY\uff09\"\"\"\r\n    # \u5b9e\u9645\u751f\u4ea7\u4e2d\u63a8\u8350\u4f7f\u7528API\uff0c\u8bc6\u522b\u7387\u6bd4Tesseract\u9ad815-20%\r\n    pass\r\n<\/code><\/pre>\n<h3>4.3 \u589e\u91cf\u7d22\u5f15\u8bbe\u8ba1<\/h3>\n<p>\u6587\u4ef6\u53d8\u52a8\u540e\uff0c\u5fc5\u987b\u5c3d\u5feb\u540c\u6b65\u5230\u68c0\u7d22\u5f15\u64ce\uff0c\u5426\u5219\u7528\u6237\u4f1a\u56f0\u60d1&#8221;\u4e3a\u4ec0\u4e48\u641c\u4e0d\u5230\u521a\u4e0a\u4f20\u7684\u6587\u4ef6&#8221;\u3002<\/p>\n<p><strong>\u65b9\u6848A\uff1a\u6d88\u606f\u961f\u5217\u9a71\u52a8\uff08\u63a8\u8350\uff09<\/strong><\/p>\n<pre><code class=\"language-python\"># \u6587\u4ef6\u53d8\u66f4\u4e8b\u4ef6 \u2192 \u6d88\u606f\u961f\u5217 \u2192 \u7d22\u5f15\u6d88\u8d39\u8005\r\nimport redis\r\n<p>def on_file_changed(event: FileChangeEvent):\r\n    \"\"\"\u6587\u4ef6\u53d8\u66f4\u4e8b\u4ef6\u5904\u7406\"\"\"\r\n    # \u5199\u5165Redis Stream\uff0c\u786e\u4fdd\u4e0d\u4e22\u6d88\u606f\r\n    r.xadd('file_index_queue', {\r\n        'file_id': event.file_id,\r\n        'operation': event.op,  # 'create'\/'update'\/'delete'\r\n        'timestamp': event.timestamp\r\n    })<\/p>\r\n<p>def index_consumer():\r\n    \"\"\"\u7d22\u5f15\u6d88\u8d39\u8005\uff0c\u6301\u7eed\u4ece\u961f\u5217\u6d88\u8d39\"\"\"\r\n    while True:\r\n        messages = r.xreadgroup(\r\n            'file_index_group', 'consumer_1',\r\n            {'file_index_queue': '&gt;'},\r\n            count=10,\r\n            block=5000\r\n        )\r\n        for stream, entries in messages:\r\n            for entry_id, fields in entries:\r\n                # \u5904\u7406\u7d22\u5f15\u4efb\u52a1\r\n                process_index_task(fields)\r\n                # ACK\u6d88\u606f\r\n                r.xack('file_index_queue', 'file_index_group', entry_id)\r\n<\/code><\/pre>\n<\/p>\n<p><strong>\u65b9\u6848B\uff1a\u6570\u636e\u5e93CDC\uff08\u53d8\u66f4\u6570\u636e\u6355\u83b7\uff09<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5df2\u6709\u5927\u91cf\u5386\u53f2\u6570\u636e\u7684\u573a\u666f\uff0c\u53ef\u4ee5\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528Debezium + Kafka\u6355\u83b7\u6570\u636e\u5e93\u53d8\u66f4\r\n<h1>\u914d\u7f6eDebezium\u76d1\u63a7files\u8868<\/h1>\r\n<h1>\u53d8\u66f4\u4e8b\u4ef6 \u2192 Kafka \u2192 \u7d22\u5f15\u670d\u52a1\u6d88\u8d39<\/h1>\r\n<p>from kafka import KafkaConsumer<\/p>\r\n<p>consumer = KafkaConsumer(\r\n    'dbz.public.files',\r\n    bootstrap_servers=['localhost:9092'],\r\n    value_deserializer=lambda m: json.loads(m.decode('utf-8'))\r\n)<\/p>\r\n<p>for message in consumer:\r\n    event = message.value\r\n    if event['op'] == 'c':  # create\r\n        index_file(event['after'])\r\n    elif event['op'] == 'u':  # update\r\n        update_file_index(event['after'])\r\n    elif event['op'] == 'd':  # delete\r\n        delete_from_index(event['before']['file_id'])\r\n<\/code><\/pre>\n<\/p>\n<h3>4.4 \u6743\u9650\u8fc7\u6ee4\u7684\u5de5\u7a0b\u5b9e\u73b0<\/h3>\n<p>\u8fd9\u662f\u4f01\u4e1a\u4e91\u76d8\u68c0\u7d22\u533a\u522b\u4e8e\u901a\u7528\u641c\u7d22\u7684\u6838\u5fc3\u3002\u641c\u7d22\u7ed3\u679c\u5fc5\u987b\u53ea\u5305\u542b\u7528\u6237\u6709\u6743\u9650\u8bbf\u95ee\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u65b9\u6848\u4e00\uff1a\u7d22\u5f15\u65f6\u6ce8\u5165\u6743\u9650\u4fe1\u606f\uff08\u63a8\u8350\uff09<\/strong><\/p>\n<pre><code class=\"language-python\">def index_file(file_info: dict, authorized_users: list):\r\n    \"\"\"\u7d22\u5f15\u6587\u4ef6\u65f6\u6ce8\u5165\u6743\u9650\u4fe1\u606f\"\"\"\r\n    doc = {\r\n        'file_id': file_info['id'],\r\n        'file_name': file_info['name'],\r\n        'content': extract_content(file_info['path']),\r\n        'department': file_info['department'],\r\n        'uploaded_by': file_info['uploader'],\r\n        'uploaded_at': file_info['upload_time'],\r\n        'permissions': authorized_users  # \u6743\u9650\u5217\u8868\r\n    }\r\n    es.index(index='files', doc=doc)\r\n<h1>\u67e5\u8be2\u65f6\u8fc7\u6ee4<\/h1>\r\ndef search(user_id: str, query: str):\r\n    # \u5b89\u5168\uff1a\u6743\u9650\u8fc7\u6ee4\u5728\u670d\u52a1\u7aef\u6267\u884c\uff0c\u9632\u6b62\u5ba2\u6237\u7aef\u7ed5\u8fc7\r\n    return es.search(index='files', body={\r\n        'query': {\r\n            'bool': {\r\n                'must': [\r\n                    {'match': {'file_name': query}},\r\n                    {'match': {'content': query}}\r\n                ],\r\n                'filter': [\r\n                    {'terms': {'permissions': [user_id]}}  # \u6743\u9650\u8fc7\u6ee4\r\n                ]\r\n            }\r\n        }\r\n    })\r\n<\/code><\/pre>\n<p><strong>\u65b9\u6848\u4e8c\uff1aSearcher\u670d\u52a1\u4ee3\u7406\uff08\u9002\u5408\u654f\u611f\u573a\u666f\uff09<\/strong><\/p>\n<pre><code class=\"language-python\">class SearcherService:\r\n    \"\"\"\u641c\u7d22\u4ee3\u7406\u670d\u52a1\uff0c\u6240\u6709\u67e5\u8be2\u5fc5\u987b\u7ecf\u8fc7\u6743\u9650\u8fc7\u6ee4\"\"\"\r\n    \r\n    def __init__(self, search_engine: SearchEngine, acl_service: ACLService):\r\n        self.engine = search_engine\r\n        self.acl = acl_service\r\n    \r\n    def search(self, user_id: str, query: str, filters: dict):\r\n        # 1. \u4eceACL\u670d\u52a1\u83b7\u53d6\u7528\u6237\u53ef\u8bbf\u95ee\u7684\u6587\u4ef6\/\u6587\u4ef6\u5939ID\u5217\u8868\r\n        authorized_ids = self.acl.get_authorized_file_ids(user_id)\r\n        \r\n        # 2. \u5c06\u6743\u9650\u6761\u4ef6\u6ce8\u5165\u641c\u7d22\u5f15\u64ce\u67e5\u8be2\r\n        filters['file_id'] = authorized_ids\r\n        \r\n        # 3. \u6267\u884c\u641c\u7d22\uff0c\u53ea\u8fd4\u56de\u6709\u6743\u9650\u7684\u6587\u4ef6\r\n        return self.engine.search(query, filters)\r\n    \r\n    def suggest(self, user_id: str, prefix: str):\r\n        # \u6743\u9650\u8fc7\u6ee4\u7684\u81ea\u52a8\u8865\u5168\r\n        authorized_ids = self.acl.get_authorized_file_ids(user_id)\r\n        return self.engine.prefix_search(prefix, authorized_ids)\r\n<\/code><\/pre>\n<hr>\n<h2>\u4e94\u3001\u5b9e\u6218\uff1a\u57fa\u4e8eMeiliSearch\u7684\u4f01\u4e1a\u4e91\u76d8\u68c0\u7d22\u5b9e\u73b0<\/h2>\n<h3>5.1 \u73af\u5883\u51c6\u5907<\/h3>\n<pre><code class=\"language-bash\"># \u5b89\u88c5MeiliSearch\r\nwget https:\/\/install.meilisearch.com | sh\r\n.\/meilisearch\r\n<h1>\u6216Docker\u90e8\u7f72<\/h1>\r\ndocker run -d -p 7700:7700 \\\r\n    -e MEILI_MASTER_KEY='your-master-key' \\\r\n    -v \/data\/meili:\/meili_data \\\r\n    getmeili\/meilisearch:latest\r\n<\/code><\/pre>\n<h3>5.2 \u7d22\u5f15\u521b\u5efa\u4e0e\u914d\u7f6e<\/h3>\n<pre><code class=\"language-python\">import meilisearch\r\nimport json\r\n<p>client = meilisearch.Client('http:\/\/localhost:7700', 'your-master-key')<\/p>\r\n<h1>\u521b\u5efa\u7d22\u5f15<\/h1>\r\nclient.create_index('files', {'primaryKey': 'file_id'})\r\n<h1>\u83b7\u53d6\u7d22\u5f15<\/h1>\r\nindex = client.index('files')\r\n<h1>\u914d\u7f6e\u4e2d\u6587\u5206\u8bcd\u5668\uff08\u4f7f\u7528Ingester\u9884\u5904\u7406\uff09<\/h1>\r\n<h1>MeiliSearch\u5185\u7f6e\u5206\u8bcd\u8f83\u7c97\u66b4\uff0c\u5efa\u8bae\u901a\u8fc7ingest pipeline\u505a\u4e2d\u6587\u9884\u5904\u7406<\/h1>\r\nindex.update_settings({\r\n    \"searchableAttributes\": [\r\n        \"file_name\",\r\n        \"content\",\r\n        \"tags\",\r\n        \"department\"\r\n    ],\r\n    \"filterableAttributes\": [\r\n        \"department\",\r\n        \"file_type\", \r\n        \"uploaded_by\",\r\n        \"uploaded_at\",\r\n        \"permissions\"\r\n    ],\r\n    \"sortableAttributes\": [\r\n        \"uploaded_at\",\r\n        \"file_size\"\r\n    ],\r\n    \"typoTolerance\": {\r\n        \"enabled\": True,\r\n        \"minWordSizeForTypos\": {\r\n            \"oneTypo\": 4,\r\n            \"twoTypos\": 8\r\n        }\r\n    }\r\n})\r\n<\/code><\/pre>\n<h3>5.3 \u6587\u6863\u5199\u5165\uff08\u5b9e\u65f6\u7d22\u5f15\uff09<\/h3>\n<pre><code class=\"language-python\">import time\r\n<p>def index_file(file_record: dict):\r\n    \"\"\"\u5c06\u6587\u4ef6\u5199\u5165MeiliSearch\u7d22\u5f15\"\"\"\r\n    doc = {\r\n        'file_id': file_record['id'],\r\n        'file_name': file_record['name'],\r\n        'content': preprocess_chinese_text(file_record['content']),\r\n        'department': file_record.get('department', ''),\r\n        'file_type': file_record['type'],\r\n        'tags': file_record.get('tags', []),\r\n        'uploaded_by': file_record['uploader'],\r\n        'uploaded_at': file_record['upload_time'],  # Unix timestamp\r\n        'file_size': file_record['size'],\r\n        'permissions': file_record['permissions']  # ['user_1', 'user_2', 'dept_sales']\r\n    }\r\n    \r\n    task = index.add_documents([doc])\r\n    # \u7b49\u5f85\u7d22\u5f15\u5b8c\u6210\uff0c\u786e\u4fdd\u6587\u4ef6\u53ef\u88ab\u641c\u7d22\u5230\r\n    client.wait_for_task(task.task_uid)\r\n    return task<\/p>\r\n<p>def preprocess_chinese_text(text: str) -&gt; str:\r\n    \"\"\"\u4e2d\u6587\u6587\u672c\u9884\u5904\u7406\uff1a\u53bb\u7a7a\u683c\u3001\u7e41\u8f6c\u7b80\u3001\u964d\u566a\"\"\"\r\n    import re\r\n    # \u53bb\u9664\u591a\u4f59\u7a7a\u767d\r\n    text = re.sub(r'\\s+', ' ', text)\r\n    # \u7e41\u8f6c\u7b80\uff08\u53ef\u9009\uff09\r\n    # text = zhconv.convert(text, 'zh-cn')\r\n    # \u53bb\u9664\u7279\u6b8a\u5b57\u7b26\r\n    text = re.sub(r'[\\x00-\\x1f\\x7f-\\x9f]', '', text)\r\n    return text.strip()\r\n<\/code><\/pre>\n<\/p>\n<h3>5.4 \u641c\u7d22API<\/h3>\n<pre><code class=\"language-python\">def search(\r\n    query: str,\r\n    user_id: str,\r\n    department: str = None,\r\n    file_type: str = None,\r\n    page: int = 1,\r\n    page_size: int = 20\r\n):\r\n    \"\"\"\u4f01\u4e1a\u4e91\u76d8\u641c\u7d22API\"\"\"\r\n    \r\n    # \u6784\u5efa\u6743\u9650\u8fc7\u6ee4\uff1a\u7528\u6237\u53ea\u80fd\u641c\u5230\u81ea\u5df1\u6709\u6743\u9650\u7684\u6587\u4ef6\r\n    permission_filter = f\"permissions = '{user_id}'\"\r\n    filters = [permission_filter]\r\n    \r\n    if department:\r\n        filters.append(f\"department = '{department}'\")\r\n    if file_type:\r\n        filters.append(f\"file_type = '{file_type}'\")\r\n    \r\n    # \u65f6\u95f4\u8303\u56f4\u8fc7\u6ee4\uff08\u6700\u8fd1\u534a\u5e74\uff09\r\n    half_year_ago = int(time.time()) - 180 <\/em> 24 <em> 3600\r\n    filters.append(f\"uploaded_at &gt;= {half_year_ago}\")\r\n    \r\n    search_params = {\r\n        'q': query,\r\n        'filter': ' AND '.join(filters),\r\n        'limit': page_size,\r\n        'offset': (page - 1) <\/em> page_size,\r\n        'attributesToHighlight': ['file_name', 'content'],\r\n        'attributesToCrop': ['content'],\r\n        'cropLength': 150,\r\n        'sort': ['uploaded_at:desc']\r\n    }\r\n    \r\n    result = index.search(query, search_params)\r\n    \r\n    return {\r\n        'total': result['estimatedTotalHits'],\r\n        'page': page,\r\n        'page_size': page_size,\r\n        'hits': [\r\n            {\r\n                'file_id': hit['file_id'],\r\n                'file_name': hit['file_name'],\r\n                'highlight': hit['_formatted']['file_name'],\r\n                'snippet': hit['_formatted']['content'],\r\n                'department': hit['department'],\r\n                'uploaded_at': hit['uploaded_at'],\r\n                'file_type': hit['file_type']\r\n            }\r\n            for hit in result['hits']\r\n        ]\r\n    }\r\n<\/code><\/pre>\n<h3>5.5 \u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e09\u79cd\u5f15\u64ce\u5728\u540c\u4e00\u786c\u4ef6\u6761\u4ef6\u4e0b\uff084C8G VM\uff09\u7684\u5bf9\u6bd4\u6570\u636e\uff1a<\/p>\n<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n<tr>\n<th>\u573a\u666f<\/th>\n<th>Elasticsearch<\/th>\n<th>MeiliSearch<\/th>\n<th>Typesense<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>50\u4e07\u6587\u6863\u7d22\u5f15<\/td>\n<td>45\u79d2<\/td>\n<td>18\u79d2<\/td>\n<td>22\u79d2<\/td>\n<\/tr>\n<tr>\n<td>\u5355\u6b21\u641c\u7d22\u5ef6\u8fdf\uff08P99\uff09<\/td>\n<td>85ms<\/td>\n<td>28ms<\/td>\n<td>31ms<\/td>\n<\/tr>\n<tr>\n<td>\u5e76\u53d110 QPS\u641c\u7d22<\/td>\n<td>120ms<\/td>\n<td>45ms<\/td>\n<td>48ms<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u5360\u7528<\/td>\n<td>3.2GB<\/td>\n<td>180MB<\/td>\n<td>140MB<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u5b57\u5bb9\u5fcd\uff08\u641c\u7d22&#8221;\u5168\u5ef7\u68c0\u7d22&#8221;\uff09<\/td>\n<td>\u274c<\/td>\n<td>\u2705\uff08\u641c\u5230&#8221;\u5168\u6587\u68c0\u7d22&#8221;\uff09<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2>\u516d\u3001\u8fdb\u9636\uff1a\u8bed\u4e49\u68c0\u7d22\u4e0eAI\u589e\u5f3a<\/h2>\n<h3>6.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u8bed\u4e49\u68c0\u7d22<\/h3>\n<p>\u4f20\u7edf\u5173\u952e\u8bcd\u68c0\u7d22\u7684\u5c40\u9650\uff1a\u7528\u6237\u60f3\u641c&#8221;\u5408\u540c&#8221;\uff0c\u4f46&#8221;\u534f\u8bae&#8221;&#8221;\u5408\u7ea6&#8221;\u641c\u4e0d\u5230\uff1b\u7528\u6237\u60f3\u641c&#8221;\u4e0a\u5b63\u5ea6\u9500\u552e&#8221;\uff0c\u4f46\u53ea\u6709&#8221;\u9500\u552e\u6570\u636e&#8221;\u6ca1\u6709&#8221;\u5b63\u5ea6&#8221;\u3002<\/p>\n<p>\u8bed\u4e49\u68c0\u7d22\u901a\u8fc7\u5411\u91cf\u5d4c\u5165\uff08Embedding\uff09\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528\u6587\u672c\u5411\u91cf\u6a21\u578b\u5c06\u67e5\u8be2\u548c\u6587\u6863\u6620\u5c04\u5230\u5411\u91cf\u7a7a\u95f4\r\nfrom sentence_transformers import SentenceTransformer\r\n<p>model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')<\/p>\r\n<p>def semantic_search(query: str, user_id: str, top_k: int = 10):\r\n    # 1. \u5c06\u67e5\u8be2\u5411\u91cf\u5316\r\n    query_vector = model.encode(query).tolist()\r\n    \r\n    # 2. \u641c\u7d22\u5411\u91cf\u6570\u636e\u5e93\uff08Milvus\/Pinecone\/Qdrant\uff09\r\n    results = vector_db.search(\r\n        collection='file_embeddings',\r\n        vector=query_vector,\r\n        filter={'permissions': user_id},\r\n        top_k=top_k\r\n    )\r\n    \r\n    # 3. \u8fd4\u56de\u8bed\u4e49\u76f8\u8fd1\u7684\u6587\u4ef6\r\n    return results<\/p>\r\n<h1>\u79bb\u7ebf\uff1a\u4e3a\u5df2\u6709\u6587\u4ef6\u6279\u91cf\u751f\u6210\u5411\u91cf<\/h1>\r\ndef generate_file_embeddings(files: list):\r\n    \"\"\"\u6279\u91cf\u4e3a\u6587\u4ef6\u751f\u6210\u5411\u91cf\u7d22\u5f15\"\"\"\r\n    embeddings = model.encode([f['content'] for f in files])\r\n    for file, embedding in zip(files, embeddings):\r\n        vector_db.insert(\r\n            collection='file_embeddings',\r\n            id=file['file_id'],\r\n            vector=embedding.tolist(),\r\n            metadata={\r\n                'file_name': file['file_name'],\r\n                'permissions': file['permissions']\r\n            }\r\n        )\r\n<\/code><\/pre>\n<h3>6.2 \u6df7\u5408\u68c0\u7d22\u67b6\u6784<\/h3>\n<p>\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5173\u952e\u8bcd\u68c0\u7d22+\u8bed\u4e49\u68c0\u7d22\u7684\u6df7\u5408\u65b9\u6848\u6548\u679c\u6700\u597d\uff1a<\/p>\n<pre><code class=\"language-python\">def hybrid_search(query: str, user_id: str):\r\n    \"\"\"\u6df7\u5408\u641c\u7d22\uff1aBM25 + \u5411\u91cf\u76f8\u4f3c\u5ea6\"\"\"\r\n    \r\n    # 1. \u5173\u952e\u8bcd\u641c\u7d22\uff08MeiliSearch\/ES\uff09\r\n    keyword_results = meilisearch.search(query, {\r\n        'filter': f\"permissions = '{user_id}'\",\r\n        'limit': 50\r\n    })\r\n    \r\n    # 2. \u8bed\u4e49\u641c\u7d22\uff08\u5411\u91cf\u6570\u636e\u5e93\uff09\r\n    semantic_results = vector_db.search(\r\n        query_vector=model.encode(query),\r\n        filter={'permissions': user_id},\r\n        top_k=50\r\n    )\r\n    \r\n    # 3. RRF\u878d\u5408\uff08Reciprocal Rank Fusion\uff09\r\n    # \u4e24\u4e2a\u6392\u540d\u5217\u8868\u52a0\u6743\u5408\u5e76\r\n    fused = rrf_fusion([\r\n        (r['file_id'], rank) for rank, r in enumerate(keyword_results['hits'])\r\n    ], [\r\n        (r['id'], rank) for rank, r in enumerate(semantic_results)\r\n    ], k=60)  # k=60\u662f\u5e38\u7528\u7684\u878d\u5408\u53c2\u6570\r\n    \r\n    # 4. \u8fd4\u56de\u6700\u7ec8\u6392\u5e8f\u7ed3\u679c\r\n    return [get_file_by_id(fid) for fid, _ in fused[:20]]\r\n<p>def rrf_fusion(*rankings, k=60):\r\n    \"\"\"RRF\u878d\u5408\u7b97\u6cd5\"\"\"\r\n    scores = defaultdict(float)\r\n    for ranking in rankings:\r\n        for rank, item in enumerate(ranking):\r\n            scores[item[0]] += 1 \/ (k + rank + 1)\r\n    return sorted(scores.items(), key=lambda x: -x[1])\r\n<\/code><\/pre>\n<\/p>\n<hr>\n<h2>\u4e03\u3001\u8e29\u5751\u603b\u7ed3\u4e0e\u9009\u578b\u5efa\u8bae<\/h2>\n<h3>7.1 \u8e29\u5751\u65f6\u95f4\u7ebf\uff08\u6309\u4e25\u91cd\u7a0b\u5ea6\u6392\u5e8f\uff09<\/h3>\n<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n<tr>\n<th>\u8e29\u5751<\/th>\n<th>\u5f71\u54cd<\/th>\n<th>\u89c4\u907f\u65b9\u6848<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u4e2d\u6587\u5206\u8bcd\u914d\u7f6e\u9519\u8bef<\/td>\n<td>\u641c\u7d22\u7ed3\u679c\u4e25\u91cd\u504f\u9887<\/td>\n<td>\u6295\u5165\u8db3\u591f\u65f6\u95f4\u914d\u7f6eIK\/analyzed<\/td>\n<\/tr>\n<tr>\n<td>\u6743\u9650\u8fc7\u6ee4\u5931\u6548<\/td>\n<td>\u6570\u636e\u6cc4\u9732<\/td>\n<td>\u4e25\u683c\u6d4b\u8bd5\uff0c\u81ea\u52a8\u5316\u56de\u5f52<\/td>\n<\/tr>\n<tr>\n<td>\u5927\u6587\u4ef6\u7d22\u5f15\u8d85\u65f6<\/td>\n<td>\u6587\u4ef6\u4e0d\u53ef\u641c<\/td>\n<td>\u5206\u6bb5\u63d0\u53d6\u5185\u5bb9\uff0c\u8bbe\u7f6e\u8d85\u65f6<\/td>\n<\/tr>\n<tr>\n<td>\u589e\u91cf\u7d22\u5f15\u5ef6\u8fdf>30\u79d2<\/td>\n<td>\u7528\u6237\u4f53\u9a8c\u5dee<\/td>\n<td>\u76d1\u63a7\u7d22\u5f15\u961f\u5217\u5ef6\u8fdf\uff0c\u9608\u503c\u544a\u8b66<\/td>\n<\/tr>\n<tr>\n<td>\u5411\u91cf\u7d22\u5f15\u81a8\u80c0<\/td>\n<td>\u5b58\u50a8\u6210\u672c\u5931\u63a7<\/td>\n<td>\u5b9a\u671f\u6e05\u7406\u65e0\u6548\u5411\u91cf\uff0c\u8bbe\u7f6eTTL<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>7.2 \u9009\u578b\u51b3\u7b56\u6811<\/h3>\n<pre><code class=\"language-\">\u6587\u4ef6\u91cf &lt; 50\u4e07?\r\n  \u251c\u2500\u2500 \u662f \u2192 MeiliSearch\uff08\u6700\u5feb\u4e0a\u7ebf\uff09\r\n  \u2514\u2500\u2500 \u5426 \u2192 \u6587\u4ef6\u91cf &lt; 200\u4e07?\r\n              \u251c\u2500\u2500 \u662f \u2192 Typesense\uff08\u8d44\u6e90\u53cb\u597d\uff09\r\n              \u2514\u2500\u2500 \u5426 \u2192 \u6587\u4ef6\u91cf &lt; 500\u4e07?\r\n                      \u251c\u2500\u2500 \u662f \u2192 MeiliSearch + \u5206\u533a\r\n                      \u2514\u2500\u2500 \u5426 \u2192 Elasticsearch\uff08\u529f\u80fd\u6700\u5f3a\uff09\r\n<\/code><\/pre>\n<h3>7.3 \u56e2\u961f\u80fd\u529b\u8003\u91cf<\/h3>\n<p>&#8211; <strong>\u6709\u4e13\u804cDBA\/\u8fd0\u7ef4<\/strong> \u2192 Elasticsearch\uff08\u529f\u80fd\u4e0a\u9650\u9ad8\uff09<br \/>\n&#8211; <strong>\u5c0f\u56e2\u961f\u65e0\u8fd0\u7ef4<\/strong> \u2192 MeiliSearch\uff08\u90e8\u7f72\u7b80\u5355\uff09<br \/>\n&#8211; <strong>\u5df2\u6709k8s\u57fa\u7840\u8bbe\u65bd<\/strong> \u2192 Typesense\uff08\u4e91\u539f\u751f\u53cb\u597d\uff09<br \/>\n&#8211; <strong>\u9700\u8981\u5411\u91cf\u68c0\u7d22<\/strong> \u2192 Elasticsearch\uff08\u63d2\u4ef6\u6210\u719f\uff09\u6216 Qdrant\/Milvus \u5206\u79bb\u90e8\u7f72<\/p>\n<hr>\n<h2>\u516b\u3001\u7ed3\u8bed<\/h2>\n<p>\u5168\u6587\u68c0\u7d22\u662f\u4f01\u4e1a\u4e91\u76d8\u4f53\u9a8c\u7684&#8221;\u7075\u9b42\u5de5\u7a0b&#8221;\u3002\u9009\u9519\u5f15\u64ce\uff0c\u8868\u9762\u770b\u662f\u6280\u672f\u9009\u578b\u95ee\u9898\uff0c\u5b9e\u9645\u5f71\u54cd\u7684\u662f\u5458\u5de5\u6bcf\u5929\u627e\u6587\u4ef6\u7684\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p>\u4e09\u79cd\u65b9\u6848\u5404\u6709\u4f18\u52bf\uff1aElasticsearch\u529f\u80fd\u6700\u5f3a\u4f46\u8fd0\u7ef4\u6700\u590d\u6742\uff1bMeiliSearch\u5f00\u7bb1\u5373\u7528\u4f46\u529f\u80fd\u53d7\u9650\uff1bTypesense\u8f7b\u91cf\u7075\u6d3b\u4f46\u751f\u6001\u8f83\u5f31\u3002<\/p>\n<p>\u6211\u7684\u5efa\u8bae\u662f\uff1a<strong>\u5148MeiliSearch\uff0c\u540eElasticsearch<\/strong>\u3002\u5148\u7528MeiliSearch\u5feb\u901f\u4e0a\u7ebf\uff0c\u9a8c\u8bc1\u641c\u7d22\u4f53\u9a8c\u7684\u4ef7\u503c\uff0c\u79ef\u7d2f\u641c\u7d22\u6570\u636e\u540e\u518d\u8bc4\u4f30\u662f\u5426\u9700\u8981\u5347\u7ea7\u5230Elasticsearch\u7684\u80fd\u529b\u3002<\/p>\n<p>\u5168\u6587\u68c0\u7d22\u505a\u597d\u4e86\uff0c\u4e91\u76d8\u7684&#8221;\u7528\u8d77\u6765&#8221;\u624d\u4e0d\u662f\u4e00\u53e5\u7a7a\u8bdd\u3002<\/p>\n<hr>\n<p><strong>\u5173\u4e8e\u5df4\u522b\u9e1f<\/strong><\/p>\n<p>\u5df4\u522b\u9e1f\u4f01\u4e1a\u4e91\u76d8\u91c7\u7528\u81ea\u7814\u7684\u5168\u6587\u68c0\u7d22\u5f15\u64ce\uff0c\u652f\u6301\u6beb\u79d2\u7ea7\u641c\u7d22\u54cd\u5e94\u3001\u667a\u80fd\u4e2d\u6587\u5206\u8bcd\u3001\u8bed\u4e49\u76f8\u4f3c\u5ea6\u6392\u5e8f\uff0c\u4ee5\u53ca\u5b8c\u6574\u7684\u6743\u9650\u8fc7\u6ee4\u4f53\u7cfb\u3002\u5982\u679c\u4f60\u5728\u8bc4\u4f30\u4f01\u4e1a\u4e91\u76d8\u7684\u641c\u7d22\u80fd\u529b\uff0c\u6b22\u8fce\u4f53\u9a8c\u5df4\u522b\u9e1f\uff1a<a href=\"https:\/\/www.babel.cc\/blog\/\">https:\/\/www.babel.cc\/blog\/<\/a><\/p>\n<hr>\n<p>\u5b57\u6570\uff1a\u7ea618500\u5b57<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u4f01\u4e1a\u4e91\u76d8\u7684\u6838\u5fc3\u4ef7\u503c\u4e4b\u4e00\u662f\u8ba9\u6587\u4ef6\u4e0d\u518d\u6c89\u7761\u5728\u76ee\u5f55\u6df1\u5904\u3002\u5f53\u5458&#8230; <a class=\"read-more\" href=\"https:\/\/www.babel.cc\/blog\/?p=6914\">[Continue Reading]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6914","post","type-post","status-publish","format-standard","hentry","category-1","no-post-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6914"}],"version-history":[{"count":1,"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6914\/revisions"}],"predecessor-version":[{"id":6915,"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6914\/revisions\/6915"}],"wp:attachment":[{"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.babel.cc\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}