{"id":984275,"date":"2024-12-27T07:26:44","date_gmt":"2024-12-26T23:26:44","guid":{"rendered":""},"modified":"2024-12-27T07:26:47","modified_gmt":"2024-12-26T23:26:47","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%90%9c%e7%b4%a2%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/984275.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u641c\u7d22\u529f\u80fd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211947\/f2755052-abde-4e9f-91cd-c28fb8a2ee4b.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u641c\u7d22\u529f\u80fd\" \/><\/p>\n<p><p> \u5b9e\u73b0Python\u641c\u7d22\u529f\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u6280\u672f\u548c\u5de5\u5177\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7b2c\u4e09\u65b9\u5e93\u5982Whoosh\u548cElasticsearch\u7b49<\/strong>\u3002\u4ee5\u4e0b\u662f\u5bf9Python\u4e2d\u641c\u7d22\u529f\u80fd\u5b9e\u73b0\u7684\u51e0\u4e2a\u5e38\u7528\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u6570\u636e\u7ed3\u6784<\/strong>\u662f\u5b9e\u73b0\u7b80\u5355\u641c\u7d22\u529f\u80fd\u7684\u57fa\u7840\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u5b58\u50a8\u9700\u8981\u641c\u7d22\u7684\u8bcd\u6761\uff0c\u7136\u540e\u901a\u8fc7\u904d\u5386\u5217\u8868\u6765\u67e5\u627e\u5339\u914d\u7684\u5143\u7d20\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u6216\u590d\u6742\u641c\u7d22\u9700\u6c42\uff0c\u5176\u6027\u80fd\u548c\u529f\u80fd\u6709\u9650\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6a21\u5f0f\u5339\u914d\u7684\u573a\u666f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u590d\u6742\u7684\u641c\u7d22\u6761\u4ef6\uff0c\u5982\u5339\u914d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3001\u63d0\u53d6\u5b50\u6a21\u5f0f\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u6027\u9ad8\uff0c\u4f46\u9700\u8981\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u3002<\/p>\n<\/p>\n<p><p><strong>Whoosh\u548cElasticsearch<\/strong>\u662f\u66f4\u9ad8\u7ea7\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5168\u6587\u641c\u7d22\u529f\u80fd\u7684\u573a\u666f\u3002Whoosh\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684\u641c\u7d22\u5f15\u64ce\u5e93\uff0c\u9002\u5408\u4e2d\u5c0f\u578b\u9879\u76ee\uff0c\u6613\u4e8e\u914d\u7f6e\u548c\u4f7f\u7528\u3002\u800cElasticsearch\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u641c\u7d22\u548c\u5206\u6790\u5f15\u64ce\uff0c\u9002\u5408\u5904\u7406\u6d77\u91cf\u6570\u636e\u548c\u9ad8\u5e76\u53d1\u8bf7\u6c42\u3002\u5b83\u9700\u8981\u642d\u914dPython\u5ba2\u6237\u7aef\uff0c\u5982Elasticsearch-py\uff0c\u6765\u8fdb\u884c\u641c\u7d22\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u57fa\u672c\u7684\u641c\u7d22\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u4e14\u7ed3\u6784\u7b80\u5355\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u8868\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u548c\u641c\u7d22\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5217\u8868\u4e2d\u641c\u7d22\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def search_in_list(data_list, target):<\/p>\n<p>    for index, item in enumerate(data_list):<\/p>\n<p>        if item == target:<\/p>\n<p>            return index<\/p>\n<p>    return -1<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>data = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>target = &#39;banana&#39;<\/p>\n<p>result = search_in_list(data, target)<\/p>\n<p>print(f&quot;Element found at index: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u904d\u5386\u5217\u8868\uff0c\u67e5\u627e\u76ee\u6807\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002\u82e5\u672a\u627e\u5230\uff0c\u8fd4\u56de-1\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5728\u6570\u636e\u91cf\u8f83\u5927\u65f6\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b57\u5178\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u5408\u7528\u4e8e\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u548c\u68c0\u7d22\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u641c\u7d22\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def search_in_dict(data_dict, target_key):<\/p>\n<p>    return data_dict.get(target_key, &quot;Key not found&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>target_key = &#39;age&#39;<\/p>\n<p>result = search_in_dict(data, target_key)<\/p>\n<p>print(f&quot;Value for {target_key}: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u641c\u7d22\u5229\u7528\u4e86\u54c8\u5e0c\u8868\u7684\u7279\u6027\uff0c\u80fd\u5728\u5e38\u6570\u65f6\u95f4\u5185\u5b8c\u6210\u67e5\u627e\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u4e0e\u66ff\u6362\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u6a21\u5f0f\u5339\u914d\u7684\u573a\u666f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u5339\u914d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u641c\u7d22\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def regex_search(text, pattern):<\/p>\n<p>    match = re.search(pattern, text)<\/p>\n<p>    if match:<\/p>\n<p>        return match.group()<\/p>\n<p>    return &quot;No match found&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>text = &quot;The quick brown fox jumps over the lazy dog&quot;<\/p>\n<p>pattern = r&#39;quick.*fox&#39;<\/p>\n<p>result = regex_search(text, pattern)<\/p>\n<p>print(f&quot;Matched text: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u4f7f\u7528<code>re.search()<\/code>\u51fd\u6570\u67e5\u627e\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u5e76\u8fd4\u56de\u5339\u914d\u7684\u6587\u672c\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u590d\u6742\u7684\u641c\u7d22\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u5b50\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6765\u5339\u914d\u6587\u672c\uff0c\u8fd8\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u5b50\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u5b50\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_subpattern(text, pattern):<\/p>\n<p>    matches = re.findall(pattern, text)<\/p>\n<p>    return matches<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>text = &quot;My phone numbers are 123-456-7890 and 987-654-3210.&quot;<\/p>\n<p>pattern = r&#39;\\d{3}-\\d{3}-\\d{4}&#39;<\/p>\n<p>result = extract_subpattern(text, pattern)<\/p>\n<p>print(f&quot;Extracted phone numbers: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u4f7f\u7528<code>re.findall()<\/code>\u51fd\u6570\u63d0\u53d6\u6240\u6709\u5339\u914d\u7684\u5b50\u6a21\u5f0f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7075\u6d3b\u6027\u4f7f\u5f97\u5b83\u5728\u6587\u672c\u5904\u7406\u548c\u6570\u636e\u63d0\u53d6\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Whoosh<\/h3>\n<\/p>\n<p><p>Whoosh\u662f\u4e00\u4e2a\u7528Python\u7f16\u5199\u7684\u5168\u6587\u641c\u7d22\u5e93\uff0c\u9002\u5408\u4e2d\u5c0f\u578b\u9879\u76ee\u3002\u5b83\u652f\u6301\u521b\u5efa\u7d22\u5f15\u3001\u6dfb\u52a0\u6587\u6863\u3001\u6267\u884c\u67e5\u8be2\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Whoosh<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Whoosh\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Whoosh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u7d22\u5f15\u662f\u4f7f\u7528Whoosh\u8fdb\u884c\u641c\u7d22\u7684\u7b2c\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u5e76\u6dfb\u52a0\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from whoosh.index import create_in<\/p>\n<p>from whoosh.fields import Schema, TEXT<\/p>\n<p>import os<\/p>\n<h2><strong>\u5b9a\u4e49\u7d22\u5f15\u7684Schema<\/strong><\/h2>\n<p>schema = Schema(title=TEXT(stored=True), content=TEXT)<\/p>\n<h2><strong>\u521b\u5efa\u7d22\u5f15<\/strong><\/h2>\n<p>if not os.path.exists(&quot;index&quot;):<\/p>\n<p>    os.mkdir(&quot;index&quot;)<\/p>\n<p>ix = create_in(&quot;index&quot;, schema)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u6863<\/strong><\/h2>\n<p>writer = ix.writer()<\/p>\n<p>writer.add_document(title=&quot;Document1&quot;, content=&quot;The quick brown fox&quot;)<\/p>\n<p>writer.add_document(title=&quot;Document2&quot;, content=&quot;Jumps over the lazy dog&quot;)<\/p>\n<p>writer.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5b9a\u4e49\u4e86\u7d22\u5f15\u7684Schema\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u7d22\u5f15\u76ee\u5f55\u3002\u7136\u540e\uff0c\u4f7f\u7528\u7d22\u5f15\u7f16\u5199\u5668\u6dfb\u52a0\u6587\u6863\u3002Whoosh\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u6863\u548c\u5b57\u6bb5\u7c7b\u578b\u652f\u6301\uff0c\u9002\u5408\u5404\u79cd\u641c\u7d22\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3. \u6267\u884c\u67e5\u8be2<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u7d22\u5f15\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Whoosh\u6267\u884c\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Whoosh\u8fdb\u884c\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from whoosh.qparser import QueryParser<\/p>\n<p>def search_whoosh(query_str):<\/p>\n<p>    with ix.searcher() as searcher:<\/p>\n<p>        query = QueryParser(&quot;content&quot;, ix.schema).parse(query_str)<\/p>\n<p>        results = searcher.search(query)<\/p>\n<p>        for result in results:<\/p>\n<p>            print(result[&#39;title&#39;])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>search_whoosh(&quot;quick&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f7f\u7528QueryParser\u89e3\u6790\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u5e76\u5728\u7d22\u5f15\u4e2d\u6267\u884c\u67e5\u8be2\u3002\u7ed3\u679c\u8fd4\u56de\u5339\u914d\u6587\u6863\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Elasticsearch<\/h3>\n<\/p>\n<p><p>Elasticsearch\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u641c\u7d22\u548c\u5206\u6790\u5f15\u64ce\uff0c\u9002\u5408\u5904\u7406\u6d77\u91cf\u6570\u636e\u548c\u9ad8\u5e76\u53d1\u8bf7\u6c42\u3002\u5b83\u9700\u8981\u4e0ePython\u5ba2\u6237\u7aef\uff08\u5982Elasticsearch-py\uff09\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Elasticsearch\u548cElasticsearch-py<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Elasticsearch\uff0c\u5e76\u4f7f\u7528pip\u5b89\u88c5Elasticsearch-py\u5ba2\u6237\u7aef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install elasticsearch<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u7d22\u5f15\u5e76\u6dfb\u52a0\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Elasticsearch\u521b\u5efa\u7d22\u5f15\u5e76\u6dfb\u52a0\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<h2><strong>\u8fde\u63a5\u5230Elasticsearch<\/strong><\/h2>\n<p>es = Elasticsearch()<\/p>\n<h2><strong>\u521b\u5efa\u7d22\u5f15<\/strong><\/h2>\n<p>index_name = &#39;documents&#39;<\/p>\n<p>if not es.indices.exists(index=index_name):<\/p>\n<p>    es.indices.create(index=index_name)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u6863<\/strong><\/h2>\n<p>doc1 = {&quot;title&quot;: &quot;Document1&quot;, &quot;content&quot;: &quot;The quick brown fox&quot;}<\/p>\n<p>doc2 = {&quot;title&quot;: &quot;Document2&quot;, &quot;content&quot;: &quot;Jumps over the lazy dog&quot;}<\/p>\n<p>es.index(index=index_name, id=1, document=doc1)<\/p>\n<p>es.index(index=index_name, id=2, document=doc2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u8fde\u63a5\u5230Elasticsearch\u96c6\u7fa4\uff0c\u521b\u5efa\u7d22\u5f15\u5e76\u6dfb\u52a0\u6587\u6863\u3002Elasticsearch\u652f\u6301\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u5b57\u6bb5\u7c7b\u578b\uff0c\u9002\u5408\u5404\u79cd\u641c\u7d22\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3. \u6267\u884c\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u7d22\u5f15\u5e76\u6dfb\u52a0\u6587\u6863\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Elasticsearch\u6267\u884c\u641c\u7d22\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Elasticsearch\u8fdb\u884c\u641c\u7d22\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def search_elasticsearch(query_str):<\/p>\n<p>    response = es.search(<\/p>\n<p>        index=index_name,<\/p>\n<p>        body={<\/p>\n<p>            &quot;query&quot;: {<\/p>\n<p>                &quot;match&quot;: {<\/p>\n<p>                    &quot;content&quot;: query_str<\/p>\n<p>                }<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    )<\/p>\n<p>    for hit in response[&#39;hits&#39;][&#39;hits&#39;]:<\/p>\n<p>        print(hit[&#39;_source&#39;][&#39;title&#39;])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>search_elasticsearch(&quot;quick&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f7f\u7528Elasticsearch\u7684\u641c\u7d22API\u6267\u884c\u67e5\u8be2\uff0c\u5e76\u8fd4\u56de\u5339\u914d\u6587\u6863\u7684\u6807\u9898\u3002Elasticsearch\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u67e5\u8be2DSL\uff0c\u652f\u6301\u590d\u6742\u7684\u67e5\u8be2\u6761\u4ef6\u548c\u805a\u5408\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u641c\u7d22\u529f\u80fd\u7684\u65b9\u6cd5\uff0c\u4ece\u7b80\u5355\u7684\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u5230\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982Whoosh\u548cElasticsearch\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\uff1b\u5bf9\u4e8e\u9700\u8981\u5168\u6587\u641c\u7d22\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9Whoosh\u6216Elasticsearch\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5b9e\u9645\u5e94\u7528\u573a\u666f\u8fdb\u884c\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u4f18\u5316\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u57fa\u672c\u7684\u641c\u7d22\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001\u5b57\u5178\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u641c\u7d22\u529f\u80fd\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u7ebf\u6027\u641c\u7d22\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u636e\u7ed3\u6784\uff0c\u67e5\u627e\u76ee\u6807\u503c\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>filter()<\/code>\u51fd\u6570\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>Python\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u641c\u7d22\u529f\u80fd\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u6765\u5b9e\u73b0\u641c\u7d22\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>Whoosh<\/code>\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684\u641c\u7d22\u5f15\u64ce\u5e93\uff0c\u9002\u5408\u5904\u7406\u6587\u672c\u641c\u7d22\uff1b<code>Elasticsearch<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5206\u5e03\u5f0f\u641c\u7d22\u5f15\u64ce\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7684<code>elasticsearch<\/code>\u5e93\u8fdb\u884c\u4ea4\u4e92\uff1b<code>SQLAlchemy<\/code>\u5219\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u641c\u7d22\u67e5\u8be2\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u4e2d\u7684\u641c\u7d22\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u641c\u7d22\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7d22\u5f15\u3001\u54c8\u5e0c\u8868\u7b49\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u6837\u53ef\u4ee5\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230O(1)\u6216O(log n)\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u9002\u5408\u7684\u641c\u7d22\u7b97\u6cd5\uff0c\u5982\u4e8c\u5206\u67e5\u627e\u6216\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\u6280\u672f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u641c\u7d22\u901f\u5ea6\u3002\u540c\u65f6\uff0c\u5408\u7406\u7ec4\u7ec7\u6570\u636e\u7ed3\u6784\u548c\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u4e5f\u80fd\u5e26\u6765\u6027\u80fd\u63d0\u5347\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0Python\u641c\u7d22\u529f\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u6280\u672f\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7b2c\u4e09\u65b9\u5e93\u5982Whoosh\u548cEl [&hellip;]","protected":false},"author":3,"featured_media":984288,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984275"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=984275"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984275\/revisions"}],"predecessor-version":[{"id":984290,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984275\/revisions\/984290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/984288"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=984275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=984275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=984275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}