diff --git a/src/main/java/com/rymcu/forest/lucene/lucene/ArticleBeanIndex.java b/src/main/java/com/rymcu/forest/lucene/lucene/ArticleBeanIndex.java index c8d1ea0..28feec5 100644 --- a/src/main/java/com/rymcu/forest/lucene/lucene/ArticleBeanIndex.java +++ b/src/main/java/com/rymcu/forest/lucene/lucene/ArticleBeanIndex.java @@ -7,7 +7,10 @@ import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; +import java.io.IOException; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -19,6 +22,11 @@ import java.util.concurrent.CountDownLatch; */ public class ArticleBeanIndex extends BaseIndex { + public ArticleBeanIndex( + String parentIndexPath,int subIndex) { + super(parentIndexPath, subIndex); + } + public ArticleBeanIndex( IndexWriter writer, CountDownLatch countDownLatch1, @@ -52,4 +60,14 @@ public class ArticleBeanIndex extends BaseIndex { writer.updateDocument(new Term("id", t.getIdArticle() + ""), doc); } } + + public void indexDoc(ArticleLucene t) throws Exception { + indexDoc(getWriter(),t); + } + + @Override + public void deleteDoc( String id) throws IOException { + Query query = new TermQuery(new Term("id", id)); + getWriter().deleteDocuments(query); + } } diff --git a/src/main/java/com/rymcu/forest/lucene/lucene/BaseIndex.java b/src/main/java/com/rymcu/forest/lucene/lucene/BaseIndex.java index b6d703f..c56c200 100644 --- a/src/main/java/com/rymcu/forest/lucene/lucene/BaseIndex.java +++ b/src/main/java/com/rymcu/forest/lucene/lucene/BaseIndex.java @@ -29,6 +29,18 @@ public abstract class BaseIndex implements Runnable { /** 对象列表 */ private List list; + public BaseIndex(String parentIndexPath, int subIndex) { + this.parentIndexPath = parentIndexPath; + this.subIndex = subIndex; + try { + this.writer = IndexUtil.getIndexWriter(parentIndexPath + "/index" + subIndex, true); + } catch (IOException e) { + e.printStackTrace(); + } + this.countDownLatch1 = null; + this.countDownLatch2 = null; + } + public BaseIndex( IndexWriter writer, CountDownLatch countDownLatch1, @@ -60,7 +72,7 @@ public abstract class BaseIndex implements Runnable { } catch (IOException e) { e.printStackTrace(); } - ; + this.subIndex = subIndex; this.countDownLatch1 = countDownLatch1; this.countDownLatch2 = countDownLatch2; @@ -107,6 +119,12 @@ public abstract class BaseIndex implements Runnable { } } + public abstract void deleteDoc(String id) throws IOException; + + public IndexWriter getWriter() { + return writer; + } + @Override public void run() { try { diff --git a/src/main/java/com/rymcu/forest/lucene/mapper/ArticleLuceneMapper.java b/src/main/java/com/rymcu/forest/lucene/mapper/ArticleLuceneMapper.java index 32422a0..88f3f5c 100755 --- a/src/main/java/com/rymcu/forest/lucene/mapper/ArticleLuceneMapper.java +++ b/src/main/java/com/rymcu/forest/lucene/mapper/ArticleLuceneMapper.java @@ -30,4 +30,14 @@ public interface ArticleLuceneMapper { * @return */ List getArticlesByIds(@Param("ids") String[] ids); + + + /** + * 加载文章内容 + * + * @param id 文章id + * @return + */ + ArticleLucene getById(@Param("id") String id); + } diff --git a/src/main/java/com/rymcu/forest/lucene/service/LuceneService.java b/src/main/java/com/rymcu/forest/lucene/service/LuceneService.java index 2998cd1..60ba33d 100644 --- a/src/main/java/com/rymcu/forest/lucene/service/LuceneService.java +++ b/src/main/java/com/rymcu/forest/lucene/service/LuceneService.java @@ -18,7 +18,28 @@ public interface LuceneService { * * @param list */ - void writeArticle(List list); + void writeArticle(List list); + + /** + * 写入单个文章索引 + * + * @param id + */ + void writeArticle(String id) throws Exception; + + /** + * 更新单个文章索引 + * + * @param id + */ + void updateArticle(String id) throws Exception; + + /** + * 删除单个文章索引 + * + * @param id + */ + void deleteArticle(String id); /** * 关键词搜索 @@ -27,20 +48,20 @@ public interface LuceneService { * @return * @throws Exception */ - List searchArticle(String value); + List searchArticle(String value); - /** - * 加载所有文章内容 - * - * @return - */ - List getAllArticleLucene(); + /** + * 加载所有文章内容 + * + * @return + */ + List getAllArticleLucene(); - /** - * 加载所有文章内容 - * - * @param ids 文章id(半角逗号分隔) - * @return - */ - List getArticlesByIds(String[] ids); + /** + * 加载所有文章内容 + * + * @param ids 文章id(半角逗号分隔) + * @return + */ + List getArticlesByIds(String[] ids); } diff --git a/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java b/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java index a643350..20349dc 100644 --- a/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java +++ b/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java @@ -21,13 +21,11 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -44,7 +42,7 @@ public class LuceneServiceImpl implements LuceneService { @Resource private ArticleLuceneMapper luceneMapper; /** Lucene索引文件路径 */ - private final String indexPath = System.getProperty("user.dir") + "/lucene/index"; + private final String indexPath = "lucene/index"; /** * 将文章的数据解析为一个个关键字词存储到索引文件中 @@ -66,7 +64,7 @@ public class LuceneServiceImpl implements LuceneService { int end = Math.min((i + 1) * perThreadCount, totalCount); List subList = list.subList(start, end); Runnable runnable = - new ArticleBeanIndex("lucene/index", i, countDownLatch1, countDownLatch2, subList); + new ArticleBeanIndex(indexPath, i, countDownLatch1, countDownLatch2, subList); // 子线程交给线程池管理 pool.execute(runnable); } @@ -83,6 +81,31 @@ public class LuceneServiceImpl implements LuceneService { } } + @Override + public void writeArticle(String id) throws Exception { + writeArticle(luceneMapper.getById(id), true); + } + + @Override + public void updateArticle(String id) throws Exception { + writeArticle(luceneMapper.getById(id), false); + } + + @Override + public void deleteArticle(String id) {} + + private void writeArticle(ArticleLucene article, boolean create) throws Exception { + if (!create) { + int size = Objects.requireNonNull(new File(indexPath).listFiles()).length; + while (size-- >= 0) { + ArticleBeanIndex index = new ArticleBeanIndex(indexPath, size); + index.deleteDoc(article.getIdArticle()); + } + } + ArticleBeanIndex index = new ArticleBeanIndex(indexPath, 0x0); + index.indexDoc(article); + } + /** * 关键词搜索 * diff --git a/src/main/java/mapper/lucene/ArticleLuceneMapper.xml b/src/main/java/mapper/lucene/ArticleLuceneMapper.xml index 62e97bc..4df2a67 100755 --- a/src/main/java/mapper/lucene/ArticleLuceneMapper.xml +++ b/src/main/java/mapper/lucene/ArticleLuceneMapper.xml @@ -61,4 +61,11 @@ #{id} + +