{"id":2223,"date":"2023-05-04T05:48:18","date_gmt":"2023-05-03T21:48:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2223"},"modified":"2023-05-04T05:49:31","modified_gmt":"2023-05-03T21:49:31","slug":"lucene-implement-full-text-retrieval-technology-including-springboot-integration-with-lucene-7-6-0","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/04\/lucene-implement-full-text-retrieval-technology-including-springboot-integration-with-lucene-7-6-0\/","title":{"rendered":"Lucene\u5b9e\u73b0\u5168\u6587\u68c0\u7d22\u6280\u672f(\u5305\u542bSpringBoot\u6574\u5408Lucene 7.6.0)"},"content":{"rendered":"<p>Lucene\u5b9e\u73b0\u5168\u6587\u68c0\u7d22\u7684\u6d41\u7a0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.appblog.cn\/wp-content\/uploads\/2023\/05\/Lucene\u7684\u5b9e\u73b0\u5168\u6587\u68c0\u7d22\u7684\u6d41\u7a0b-1-1024x728.png\" alt=\"\" \/><\/p>\n<p>\u2460 \u7eff\u8272\u8868\u793a\u7d22\u5f15\u8fc7\u7a0b\uff0c\u5bf9\u8981\u641c\u7d22\u7684\u539f\u59cb\u5185\u5bb9\u8fdb\u884c\u7d22\u5f15\u6784\u5efa\u4e00\u4e2a\u7d22\u5f15\u5e93\uff0c\u7d22\u5f15\u8fc7\u7a0b\u5305\u62ec\uff1a\u786e\u5b9a\u539f\u59cb\u5185\u5bb9\u5373\u8981\u641c\u7d22\u7684\u5185\u5bb9\u3001\u91c7\u96c6\u6587\u6863\u3001\u521b\u5efa\u6587\u6863\u3001\u5206\u6790\u6587\u6863\u3001\u7d22\u5f15\u6587\u6863<\/p>\n<p>\u2461 \u7ea2\u8272\u8868\u793a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u7d22\u5f15\u5e93\u4e2d\u641c\u7d22\u5185\u5bb9\uff0c\u641c\u7d22\u8fc7\u7a0b\u5305\u62ec\uff1a\u7528\u6237\u901a\u8fc7\u641c\u7d22\u754c\u9762\u3001\u521b\u5efa\u67e5\u8be2\u3001\u6267\u884c\u641c\u7d22\uff0c\u4ece\u7d22\u5f15\u5e93\u641c\u7d22\u5f15\u64ce\u6e32\u67d3\u641c\u7d22\u7ed3\u679c<\/p>\n<h2>\u5f15\u5165\u6838\u5fc3\u4f9d\u8d56<\/h2>\n<p>lucene\u6838\u5fc3\u53ca\u5176\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;!--lucene--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.lucene&lt;\/groupId&gt;\n    &lt;artifactId&gt;lucene-core&lt;\/artifactId&gt;\n    &lt;version&gt;7.6.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.lucene&lt;\/groupId&gt;\n    &lt;artifactId&gt;lucene-queryparser&lt;\/artifactId&gt;\n    &lt;version&gt;7.6.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.lucene&lt;\/groupId&gt;\n    &lt;artifactId&gt;lucene-analyzers-common&lt;\/artifactId&gt;\n    &lt;version&gt;7.6.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4e2d\u6587\u5206\u8bcd\u5668<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.lucene&lt;\/groupId&gt;\n    &lt;artifactId&gt;lucene-analyzers-smartcn&lt;\/artifactId&gt;\n    &lt;version&gt;7.6.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u6587\u4ef6IO\u64cd\u4f5c<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;commons-io&lt;\/groupId&gt;\n    &lt;artifactId&gt;commons-io&lt;\/artifactId&gt;\n    &lt;version&gt;2.6&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u539f\u59cb\u6587\u6863<\/h2>\n<p>\u539f\u59cb\u6587\u6863\u662f\u6307\u8981\u7d22\u5f15\u548c\u641c\u7d22\u7684\u5185\u5bb9\u3002\u539f\u59cb\u5185\u5bb9\u5305\u62ec\u4e92\u8054\u7f51\u4e0a\u7684\u7f51\u9875\u3001\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3001\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\u7b49\u3002<\/p>\n<h2>\u521b\u5efa\u67e5\u8be2<\/h2>\n<p>\u7528\u6237\u8f93\u5165\u67e5\u8be2\u5173\u952e\u5b57\u6267\u884c\u641c\u7d22\u4e4b\u524d\u9700\u8981\u5148\u6784\u5efa\u4e00\u4e2a\u67e5\u8be2\u5bf9\u8c61\uff0c\u67e5\u8be2\u5bf9\u8c61\u4e2d\u53ef\u4ee5\u6307\u5b9a\u67e5\u8be2\u8981\u641c\u7d22\u7684Field\u6587\u6863\u57df\u3001\u67e5\u8be2\u5173\u952e\u5b57\u7b49\uff0c\u67e5\u8be2\u5bf9\u8c61\u4f1a\u751f\u6210\u5177\u4f53\u7684\u67e5\u8be2\u8bed\u6cd5<\/p>\n<p>\u4f8b\u5982\uff1a\u8bed\u6cd5 <code>fileName:lucene<\/code>\u8868\u793a\u8981\u641c\u7d22Field\u57df\u7684\u5185\u5bb9\u4e3a<code>lucene<\/code>\u7684\u6587\u6863<\/p>\n<h2>\u4ee3\u7801\u793a\u4f8b\uff08\u521b\u5efa\u7d22\u5f15\uff09<\/h2>\n<pre><code class=\"language-java\">\/\/\u521b\u5efa\u7d22\u5f15\n@Test\npublic void luceneCreateIndex() throws Exception{\n\n    \/\/\u6307\u5b9a\u7d22\u5f15\u5b58\u653e\u7684\u4f4d\u7f6e\n    \/\/E:\\Lucene_index\n    Directory directory = FSDirectory.open(Paths.get(new File(&quot;E:\\\\Lucene_index&quot;).getPath()));\n    System.out.println(&quot;pathname&quot;+Paths.get(new File(&quot;E:\\\\Lucene_index&quot;).getPath()));\n    \/\/\u521b\u5efa\u4e00\u4e2a\u5206\u8bcd\u5668\n    \/\/ StandardAnalyzer analyzer = new StandardAnalyzer();\n    \/\/ CJKAnalyzer cjkAnalyzer = new CJKAnalyzer();\n    SmartChineseAnalyzer smartChineseAnalyzer = new SmartChineseAnalyzer();\n    \/\/\u521b\u5efaindexwriterConfig(\u53c2\u6570\u5206\u8bcd\u5668)\n    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(smartChineseAnalyzer);\n    \/\/\u521b\u5efaindexwrite \u5bf9\u8c61(\u6587\u4ef6\u5bf9\u8c61\uff0c\u7d22\u5f15\u914d\u7f6e\u5bf9\u8c61)\n    IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);\n    \/\/\u539f\u59cb\u6587\u4ef6\n    File file = new File(&quot;E:\\\\Lucene_Document&quot;);\n\n    for (File f: file.listFiles()){\n        \/\/\u6587\u4ef6\u540d\n        String fileName = f.getName();\n        \/\/\u6587\u4ef6\u5185\u5bb9\n        String fileContent = FileUtils.readFileToString(f,&quot;GBK&quot;);\n        System.out.println(fileContent);\n        \/\/\u6587\u4ef6\u8def\u5f84\n        String path = f.getPath();\n        \/\/\u6587\u4ef6\u5927\u5c0f\n        long fileSize = FileUtils.sizeOf(f);\n\n        \/\/\u521b\u5efa\u6587\u4ef6\u57df\u540d\n        \/\/\u57df\u7684\u540d\u79f0 \u57df\u7684\u5185\u5bb9 \u662f\u5426\u5b58\u50a8\n        Field fileNameField = new TextField(&quot;fileName&quot;, fileName, Field.Store.YES);\n        Field fileContentField = new TextField(&quot;fileContent&quot;, fileContent, Field.Store.YES);\n        Field filePathField = new TextField(&quot;filePath&quot;, path, Field.Store.YES);\n        Field fileSizeField = new TextField(&quot;fileSize&quot;, fileSize+&quot;&quot;, Field.Store.YES);\n\n        \/\/\u521b\u5efaDocument \u5bf9\u8c61\n        Document indexableFields = new Document();\n        indexableFields.add(fileNameField);\n        indexableFields.add(fileContentField);\n        indexableFields.add(filePathField);\n        indexableFields.add(fileSizeField);\n        \/\/\u521b\u5efa\u7d22\u5f15\uff0c\u5e76\u5199\u5165\u7d22\u5f15\u5e93\n        indexWriter.addDocument(indexableFields);\n\n    }\n\n    \/\/\u5173\u95edindexWriter\n    indexWriter.close();\n}<\/code><\/pre>\n<h2>\u4ee3\u7801\u793a\u4f8b\uff08\u67e5\u8be2\u7d22\u5f15\uff09<\/h2>\n<pre><code class=\"language-java\">@Test\npublic void searchIndex() throws IOException {\n    \/\/\u6307\u5b9a\u7d22\u5f15\u5e93\u5b58\u653e\u8def\u5f84\n    \/\/E:\\Lucene_index\n    Directory directory = FSDirectory.open(Paths.get(new File(&quot;E:\\\\Lucene_index&quot;).getPath()));\n    \/\/\u521b\u5efaindexReader\u5bf9\u8c61\n    IndexReader indexReader = DirectoryReader.open(directory);\n    \/\/\u521b\u5efaindexSearcher\u5bf9\u8c61\n    IndexSearcher indexSearcher = new IndexSearcher(indexReader);\n    \/\/\u521b\u5efa\u67e5\u8be2\n    Query query = new TermQuery(new Term(&quot;fileContent&quot;, &quot;\u53ef\u7231&quot;));\n    \/\/\u6267\u884c\u67e5\u8be2\n    \/\/\u53c2\u6570\u4e00  \u67e5\u8be2\u5bf9\u8c61    \u53c2\u6570\u4e8c  \u67e5\u8be2\u7ed3\u679c\u8fd4\u56de\u7684\u6700\u5927\u503c\n    TopDocs topDocs = indexSearcher.search(query, 10);\n    System.out.println(&quot;\u67e5\u8be2\u7ed3\u679c\u7684\u603b\u6570&quot;+topDocs.totalHits);\n    \/\/\u904d\u5386\u67e5\u8be2\u7ed3\u679c\n    for (ScoreDoc scoreDoc: topDocs.scoreDocs){\n        \/\/scoreDoc.doc \u5c5e\u6027\u5c31\u662fdoucumnet\u5bf9\u8c61\u7684id\n        Document doc = indexSearcher.doc(scoreDoc.doc);\n        System.out.println(doc.getField(&quot;fileName&quot;));\n        System.out.println(doc.getField(&quot;fileContent&quot;));\n        System.out.println(doc.getField(&quot;filePath&quot;));\n        System.out.println(doc.getField(&quot;fileSize&quot;));\n    }\n    indexReader.close();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Lucene\u5b9e\u73b0\u5168\u6587\u68c0\u7d22\u7684\u6d41\u7a0b \u2460 \u7eff\u8272\u8868\u793a\u7d22\u5f15\u8fc7\u7a0b\uff0c\u5bf9\u8981\u641c\u7d22\u7684\u539f\u59cb\u5185\u5bb9\u8fdb\u884c\u7d22\u5f15\u6784\u5efa\u4e00\u4e2a\u7d22\u5f15\u5e93\uff0c\u7d22\u5f15\u8fc7\u7a0b\u5305\u62ec\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[560,41],"tags":[],"class_list":["post-2223","post","type-post","status-publish","format-standard","hentry","category-lucene","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2223"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2223\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}