🚧 lucene更新删除索引
This commit is contained in:
parent
d8b30d7af9
commit
03c8e339df
@ -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<ArticleLucene> {
|
||||
|
||||
public ArticleBeanIndex(
|
||||
String parentIndexPath,int subIndex) {
|
||||
super(parentIndexPath, subIndex);
|
||||
}
|
||||
|
||||
public ArticleBeanIndex(
|
||||
IndexWriter writer,
|
||||
CountDownLatch countDownLatch1,
|
||||
@ -52,4 +60,14 @@ public class ArticleBeanIndex extends BaseIndex<ArticleLucene> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,18 @@ public abstract class BaseIndex<T> implements Runnable {
|
||||
/** 对象列表 */
|
||||
private List<T> 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<T> implements Runnable {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
;
|
||||
|
||||
this.subIndex = subIndex;
|
||||
this.countDownLatch1 = countDownLatch1;
|
||||
this.countDownLatch2 = countDownLatch2;
|
||||
@ -107,6 +119,12 @@ public abstract class BaseIndex<T> implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void deleteDoc(String id) throws IOException;
|
||||
|
||||
public IndexWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
@ -30,4 +30,14 @@ public interface ArticleLuceneMapper {
|
||||
* @return
|
||||
*/
|
||||
List<ArticleDTO> getArticlesByIds(@Param("ids") String[] ids);
|
||||
|
||||
|
||||
/**
|
||||
* 加载文章内容
|
||||
*
|
||||
* @param id 文章id
|
||||
* @return
|
||||
*/
|
||||
ArticleLucene getById(@Param("id") String id);
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,28 @@ public interface LuceneService {
|
||||
*
|
||||
* @param list
|
||||
*/
|
||||
void writeArticle(List<ArticleLucene> list);
|
||||
void writeArticle(List<ArticleLucene> 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<ArticleLucene> searchArticle(String value);
|
||||
List<ArticleLucene> searchArticle(String value);
|
||||
|
||||
/**
|
||||
* 加载所有文章内容
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<ArticleLucene> getAllArticleLucene();
|
||||
/**
|
||||
* 加载所有文章内容
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<ArticleLucene> getAllArticleLucene();
|
||||
|
||||
/**
|
||||
* 加载所有文章内容
|
||||
*
|
||||
* @param ids 文章id(半角逗号分隔)
|
||||
* @return
|
||||
*/
|
||||
List<ArticleDTO> getArticlesByIds(String[] ids);
|
||||
/**
|
||||
* 加载所有文章内容
|
||||
*
|
||||
* @param ids 文章id(半角逗号分隔)
|
||||
* @return
|
||||
*/
|
||||
List<ArticleDTO> getArticlesByIds(String[] ids);
|
||||
}
|
||||
|
@ -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<ArticleLucene> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关键词搜索
|
||||
*
|
||||
|
@ -61,4 +61,11 @@
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
<select id="getById" resultMap="DTOResultMap">
|
||||
select art.id, art.article_title, content.article_content
|
||||
from forest_article art
|
||||
left join forest_article_content content on art.id = content.id_article
|
||||
where id = #{id};
|
||||
</select>
|
||||
</mapper>
|
||||
|
Loading…
Reference in New Issue
Block a user