{"id":2221,"date":"2023-05-04T05:38:06","date_gmt":"2023-05-03T21:38:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2221"},"modified":"2023-05-04T05:41:57","modified_gmt":"2023-05-03T21:41:57","slug":"springboot-lucene-chapter-6-adding-deleting-checking-indexes","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/04\/springboot-lucene-chapter-6-adding-deleting-checking-indexes\/","title":{"rendered":"SpringBoot+Lucene\u7b2c\u516d\u7bc7 \u2014 \u7d22\u5f15\u7684\u589e\u5220\u6539\u67e5"},"content":{"rendered":"<p>\u4e3a\u4e86\u51cf\u5c11\u4ee3\u7801\u91cf\uff0c\u5199\u4e00\u4e2a\u516c\u7528\u7684\u589e\u5220\u6539\u516c\u7528\u7684\u83b7\u53d6<code>IndexWriter<\/code>\u5bf9\u8c61\u7684\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">\/**\n * \u589e\u5220\u6539\u516c\u7528\u7684\u83b7\u53d6IndexWriter\u5bf9\u8c61\n * @return\n * @throws Exception\n *\/\npublic IndexWriter getIndexWriter() throws Exception{\n    \/\/\u83b7\u5f97\u7d22\u5f15\u5b58\u653e\u7684\u4f4d\u7f6e\n    Directory directory = FSDirectory.open(new File(&quot;.\/index&quot;));\n    \/\/\u83b7\u5f97\u5206\u8bcd\u5668\n    Analyzer analyzer = new IKAnalyzer();\n    \/\/\u83b7\u5f97IndexWriterConfig\u5bf9\u8c61\n    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_2, analyzer);\n    \/\/\u83b7\u5f97indexWeiter\u5bf9\u8c61\n    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);\n    return indexWriter;\n}<\/code><\/pre>\n<p>\u5220\u9664\uff0c\u4fee\u6539\u7d22\u5f15<\/p>\n<pre><code class=\"language-java\">\/**\n * \u5220\u9664\u7d22\u5f15\u2014\u2014\u5168\u90e8\u5220\u9664\n * @throws Exception\n *\/\n@Test\npublic  void deleteAll() throws  Exception{\n    \/\/\u83b7\u5f97indexWeiter\u5bf9\u8c61\n    IndexWriter indexWriter = this.getIndexWriter();\n    \/\/\u5220\u9664\u6240\u6709\u7684\u7d22\u5f15\n    indexWriter.deleteAll();\n    indexWriter.close();\n}\n\n\/**\n * \u5220\u9664\u7d22\u5f15-\u6309\u6761\u4ef6\u5220\u9664\n *\/\n@Test\npublic void delete() throws Exception{\n    \/\/\u83b7\u5f97indexWeiter\u5bf9\u8c61\n    IndexWriter indexWriter = this.getIndexWriter();\n\n    Term t = new Term(&quot;fileName&quot;, &quot;\u5168\u6587&quot;);\n    TermQuery query = new TermQuery(t);\n    \/\/\u5220\u9664\u6307\u5b9a\u6761\u4ef6\n    indexWriter.deleteDocuments(query);\n    indexWriter.close();\n}\n\n\/**\n * \u66f4\u65b0\u7d22\u5f15\n * @throws Exception\n *\/\n@Test\npublic void update() throws  Exception{\n    \/\/\u83b7\u5f97indexWeiter\u5bf9\u8c61\n    IndexWriter indexWriter = this.getIndexWriter();\n    Document document = new Document();\n    document.add(new TextField(&quot;fileN&quot;, &quot;\u6d4b\u8bd5\u6587\u4ef6\u540d&quot;, Field.Store.YES));\n    document.add(new TextField(&quot;fileC&quot;, &quot;\u6d4b\u8bd5\u6587\u4ef6\u5185\u5bb9&quot;, Field.Store.YES));\n\n    indexWriter.updateDocument(new Term(&quot;fileName&quot;, &quot;\u5168\u6587&quot;), document, new IKAnalyzer());\n    indexWriter.close();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u51cf\u5c11\u4ee3\u7801\u91cf\uff0c\u5199\u4e00\u4e2a\u516c\u7528\u7684\u589e\u5220\u6539\u516c\u7528\u7684\u83b7\u53d6IndexWriter\u5bf9\u8c61\u7684\u65b9\u6cd5 \/** * \u589e\u5220\u6539\u516c\u7528\u7684\u83b7\u53d6In [&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],"tags":[559],"class_list":["post-2221","post","type-post","status-publish","format-standard","hentry","category-lucene","tag-lucene"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2221","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=2221"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2221\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}