fix: 修复FAQ删除后向量数据未删除的bug
This commit is contained in:
@@ -11,6 +11,8 @@ import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.safety.Safelist;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -19,8 +21,8 @@ import tech.easyflow.ai.entity.DocumentCollection;
|
||||
import tech.easyflow.ai.entity.FaqItem;
|
||||
import tech.easyflow.ai.entity.Model;
|
||||
import tech.easyflow.ai.mapper.FaqItemMapper;
|
||||
import tech.easyflow.ai.service.FaqCategoryService;
|
||||
import tech.easyflow.ai.service.DocumentCollectionService;
|
||||
import tech.easyflow.ai.service.FaqCategoryService;
|
||||
import tech.easyflow.ai.service.FaqItemService;
|
||||
import tech.easyflow.ai.service.ModelService;
|
||||
import tech.easyflow.common.util.StringUtil;
|
||||
@@ -28,10 +30,7 @@ import tech.easyflow.common.web.exceptions.BusinessException;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import static tech.easyflow.ai.entity.DocumentCollection.KEY_CAN_UPDATE_EMBEDDING_MODEL;
|
||||
import static tech.easyflow.ai.entity.DocumentCollection.KEY_SEARCH_ENGINE_TYPE;
|
||||
@@ -39,6 +38,7 @@ import static tech.easyflow.ai.entity.DocumentCollection.KEY_SEARCH_ENGINE_TYPE;
|
||||
@Service
|
||||
public class FaqItemServiceImpl extends ServiceImpl<FaqItemMapper, FaqItem> implements FaqItemService {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FaqItemServiceImpl.class);
|
||||
private static final Safelist ANSWER_SAFE_LIST = Safelist.basic()
|
||||
.addTags("h1", "h2", "h3", "h4", "h5", "h6");
|
||||
|
||||
@@ -190,16 +190,44 @@ public class FaqItemServiceImpl extends ServiceImpl<FaqItemMapper, FaqItem> impl
|
||||
|
||||
private void removeFromVector(DocumentCollection collection, FaqItem entity) {
|
||||
PreparedStore preparedStore = prepareStore(collection);
|
||||
preparedStore.documentStore.delete(Collections.singletonList(entity.getId()), preparedStore.storeOptions);
|
||||
boolean deleteSuccess = deleteFromVectorStore(preparedStore.documentStore, preparedStore.storeOptions, entity.getId());
|
||||
if (!deleteSuccess) {
|
||||
throw new BusinessException("FAQ向量删除失败");
|
||||
}
|
||||
|
||||
if (collection.isSearchEngineEnabled()) {
|
||||
DocumentSearcher searcher = searcherFactory.getSearcher((String) collection.getOptionsByKey(KEY_SEARCH_ENGINE_TYPE));
|
||||
if (searcher != null) {
|
||||
searcher.deleteDocument(entity.getId());
|
||||
boolean removed = searcher.deleteDocument(entity.getId());
|
||||
if (!removed) {
|
||||
LOG.warn("Delete faq search index failed. faqId={}, searcherType={}",
|
||||
entity.getId(), collection.getOptionsByKey(KEY_SEARCH_ENGINE_TYPE));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean deleteFromVectorStore(DocumentStore documentStore, StoreOptions storeOptions, BigInteger id) {
|
||||
List<Object> candidates = new ArrayList<>(3);
|
||||
candidates.add(id);
|
||||
candidates.add(String.valueOf(id));
|
||||
try {
|
||||
candidates.add(id.longValueExact());
|
||||
} catch (ArithmeticException ignored) {
|
||||
// 非 long 范围不追加 long 类型候选
|
||||
}
|
||||
|
||||
for (Object candidate : candidates) {
|
||||
StoreResult deleteResult = documentStore.delete(Collections.singletonList(candidate), storeOptions);
|
||||
if (deleteResult != null && deleteResult.isSuccess()) {
|
||||
return true;
|
||||
}
|
||||
LOG.warn("Delete faq vector by id candidate failed. faqId={}, candidateType={}, candidate={}",
|
||||
id, candidate == null ? "null" : candidate.getClass().getSimpleName(), candidate);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private PreparedStore prepareStore(DocumentCollection collection) {
|
||||
DocumentStore documentStore;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user