知识问答接口
This commit is contained in:
parent
699de4b458
commit
560fb3d992
stdiet-admin/src/main
filters
java/com/stdiet/web/controller/custom
stdiet-custom/src/main
java/com/stdiet/custom
dto/response
mapper
service
utils
resources/mapper/custom
@ -181,4 +181,8 @@ aliyun:
|
||||
Buckets: stdiet
|
||||
EndPoint: https://oss-cn-shenzhen.aliyuncs.com
|
||||
#案例文件夹路径
|
||||
casePrefix: case/
|
||||
casePrefix: case/
|
||||
|
||||
lucene:
|
||||
index:
|
||||
nutritionQuestion: /home/api/manage.shengtangdiet.com/test/uploadPath/nutritionQuestion
|
@ -19,7 +19,7 @@ stdiet:
|
||||
# 开发环境配置
|
||||
server:
|
||||
# 服务器的HTTP端口,默认为8080
|
||||
port: 8090
|
||||
port: 8091
|
||||
servlet:
|
||||
# 应用的访问路径
|
||||
context-path: /
|
||||
@ -184,3 +184,7 @@ aliyun:
|
||||
#案例文件夹路径
|
||||
casePrefix: case/
|
||||
|
||||
lucene:
|
||||
index:
|
||||
nutritionQuestion: D:\develop\LuceneIndex\nutritionQuestion
|
||||
|
||||
|
@ -181,4 +181,8 @@ aliyun:
|
||||
Buckets: stdiet
|
||||
EndPoint: https://oss-cn-shenzhen.aliyuncs.com
|
||||
#案例文件夹路径
|
||||
casePrefix: case/
|
||||
casePrefix: case/
|
||||
|
||||
lucene:
|
||||
index:
|
||||
nutritionQuestion: /home/api/manage.shengtangdiet.com/uploadPath/nutritionQuestion
|
@ -14,10 +14,7 @@ import com.stdiet.common.utils.sign.AesUtils;
|
||||
import com.stdiet.custom.domain.*;
|
||||
import com.stdiet.custom.dto.response.CustomerCaseResponse;
|
||||
import com.stdiet.custom.page.WxLogInfo;
|
||||
import com.stdiet.custom.service.ISysCustomerCaseService;
|
||||
import com.stdiet.custom.service.ISysOrderService;
|
||||
import com.stdiet.custom.service.ISysWxUserInfoService;
|
||||
import com.stdiet.custom.service.ISysWxUserLogService;
|
||||
import com.stdiet.custom.service.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -47,6 +44,9 @@ public class WechatAppletController extends BaseController {
|
||||
@Autowired
|
||||
private ISysOrderService sysOrderService;
|
||||
|
||||
@Autowired
|
||||
private ISysNutritionQuestionService sysNutritionQuestionService;
|
||||
|
||||
/**
|
||||
* 查询微信小程序中展示的客户案例
|
||||
*/
|
||||
@ -283,4 +283,13 @@ public class WechatAppletController extends BaseController {
|
||||
return AjaxResult.error("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信用户记录详细信息
|
||||
*/
|
||||
@GetMapping(value = "/getNutritionQuestionList")
|
||||
public AjaxResult getNutritionQuestionList(SysNutritionQuestion sysNutritionQuestion, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
|
||||
Map<String,Object> result = sysNutritionQuestionService.getNutritionQuestionListByKey(sysNutritionQuestion, pageNum, pageSize);
|
||||
return AjaxResult.success(result);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class NutritionQuestionResponse implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** $column.columnComment */
|
||||
private Long id;
|
||||
private String id;
|
||||
|
||||
/** 标题 */
|
||||
@Excel(name = "标题")
|
||||
|
@ -2,6 +2,7 @@ package com.stdiet.custom.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import com.stdiet.custom.domain.SysNutritionQuestion;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 营养知识小问答Mapper接口
|
||||
@ -64,4 +65,18 @@ public interface SysNutritionQuestionMapper
|
||||
* @return
|
||||
*/
|
||||
public List<String> getNutritionQuestionListByKey(SysNutritionQuestion sysNutritionQuestion);
|
||||
|
||||
/**
|
||||
* 查询总数
|
||||
* @return
|
||||
*/
|
||||
public int getNutritionQuestionCount();
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param start
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
public List<SysNutritionQuestion> getNutritionQuestionListByPage(@Param("start")int start, @Param("pageSize")int pageSize);
|
||||
}
|
@ -67,4 +67,10 @@ public interface ISysNutritionQuestionService
|
||||
*/
|
||||
public Map<String, Object> getNutritionQuestionListByKey(SysNutritionQuestion sysNutritionQuestion, int pageNum, int pageSize);
|
||||
|
||||
/**
|
||||
* 重新生成知识问答索引
|
||||
* @return
|
||||
*/
|
||||
public boolean regenerateNutritionQuestionIndex();
|
||||
|
||||
}
|
@ -7,11 +7,13 @@ import java.util.Map;
|
||||
|
||||
import com.stdiet.common.utils.DateUtils;
|
||||
import com.stdiet.common.utils.reflect.ReflectUtils;
|
||||
import com.stdiet.custom.dto.response.NutritionQuestionResponse;
|
||||
import com.stdiet.custom.utils.LuceneIndexUtils;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.stdiet.custom.mapper.SysNutritionQuestionMapper;
|
||||
import com.stdiet.custom.domain.SysNutritionQuestion;
|
||||
@ -29,7 +31,8 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer
|
||||
@Autowired
|
||||
private SysNutritionQuestionMapper sysNutritionQuestionMapper;
|
||||
|
||||
public static final String index_path = "D:\\develop\\LuceneIndex\\nutritionQuestion";
|
||||
@Value("${lucene.index.nutritionQuestion}")
|
||||
public String index_path;
|
||||
//private static final String index_path = "";
|
||||
|
||||
//建立索引的字段名称
|
||||
@ -85,7 +88,10 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer
|
||||
public int updateSysNutritionQuestion(SysNutritionQuestion sysNutritionQuestion)
|
||||
{
|
||||
sysNutritionQuestion.setUpdateTime(DateUtils.getNowDate());
|
||||
return sysNutritionQuestionMapper.updateSysNutritionQuestion(sysNutritionQuestion);
|
||||
if(sysNutritionQuestionMapper.updateSysNutritionQuestion(sysNutritionQuestion) > 0){
|
||||
return updateNutritionQuestionIndex(sysNutritionQuestion) ? 1 : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,7 +103,17 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer
|
||||
@Override
|
||||
public int deleteSysNutritionQuestionByIds(Long[] ids)
|
||||
{
|
||||
return sysNutritionQuestionMapper.deleteSysNutritionQuestionByIds(ids);
|
||||
if(sysNutritionQuestionMapper.deleteSysNutritionQuestionByIds(ids) > 0){
|
||||
//建立索引
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path);
|
||||
try {
|
||||
return luceneIndexUtils.deleteOneByPrimaryIds(ids) ? 1 : 0;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,7 +125,17 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer
|
||||
@Override
|
||||
public int deleteSysNutritionQuestionById(Long id)
|
||||
{
|
||||
return sysNutritionQuestionMapper.deleteSysNutritionQuestionById(id);
|
||||
if(sysNutritionQuestionMapper.deleteSysNutritionQuestionById(id) > 0){
|
||||
try {
|
||||
//建立索引
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path);
|
||||
return luceneIndexUtils.deleteOneByPrimaryKey(id) ? 1 : 0;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -119,19 +145,58 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer
|
||||
@Override
|
||||
public Map<String, Object> getNutritionQuestionListByKey(SysNutritionQuestion sysNutritionQuestion, int pageNum, int pageSize){
|
||||
//return sysNutritionQuestionMapper.getNutritionQuestionListByKey(sysNutritionQuestion);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
int total = 0;
|
||||
List<NutritionQuestionResponse> nutritionQuestionResponsesList = new ArrayList<>();
|
||||
try{
|
||||
//建立索引
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path);
|
||||
return luceneIndexUtils.queryByKeyword(sysNutritionQuestion.getKey(), index_field_array, pageNum, pageSize);
|
||||
Map<String, Object> indexMap = luceneIndexUtils.queryByKeyword(sysNutritionQuestion.getKey(), index_field_array, pageNum, pageSize);
|
||||
total = (int)indexMap.get("total");
|
||||
List<Document> documentList = (List<Document>)indexMap.get("data");
|
||||
if(documentList != null && documentList.size() > 0){
|
||||
for (Document document : documentList) {
|
||||
nutritionQuestionResponsesList.add(documentToNutritionQuestion(document));
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("total", 0);
|
||||
result.put("data", new ArrayList<>());
|
||||
result.put("total", total);
|
||||
result.put("data", nutritionQuestionResponsesList);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新生成知识问答索引
|
||||
* @return
|
||||
*/
|
||||
public boolean regenerateNutritionQuestionIndex(){
|
||||
int pageSize = 1000;
|
||||
int total = sysNutritionQuestionMapper.getNutritionQuestionCount();
|
||||
try {
|
||||
//建立索引
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path);
|
||||
if(luceneIndexUtils.deleteAllIndex()){
|
||||
if(total > 0){
|
||||
//总页数
|
||||
int totalPage = total/pageSize + (total%pageSize > 0 ? 1 : 0);
|
||||
int start = 0;
|
||||
for(int i = 1; i <= totalPage; i++){
|
||||
start = (i-1) * pageSize;
|
||||
List<SysNutritionQuestion> list = sysNutritionQuestionMapper.getNutritionQuestionListByPage(start,pageSize);
|
||||
luceneIndexUtils.addIndexs(nutritionQuestionToDocument(list));
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立索引
|
||||
* @param sysNutritionQuestion
|
||||
@ -141,14 +206,62 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer
|
||||
try{
|
||||
//建立索引
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path);
|
||||
Document document = new Document();
|
||||
for (String fieldName : index_field_array) {
|
||||
document.add(new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES));
|
||||
}
|
||||
Document document = nutritionQuestionToDocument(sysNutritionQuestion);
|
||||
return luceneIndexUtils.addIndexOne(document);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新索引
|
||||
* @param sysNutritionQuestion
|
||||
* @return
|
||||
*/
|
||||
private boolean updateNutritionQuestionIndex(SysNutritionQuestion sysNutritionQuestion){
|
||||
try{
|
||||
//建立索引
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path);
|
||||
Document document = nutritionQuestionToDocument(sysNutritionQuestion);
|
||||
return luceneIndexUtils.updateByPrimaryId(sysNutritionQuestion.getId(), document);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* document转为NutritionQuestionResponse
|
||||
* @param document
|
||||
* @return
|
||||
*/
|
||||
private NutritionQuestionResponse documentToNutritionQuestion(Document document){
|
||||
NutritionQuestionResponse nutritionQuestionResponse = new NutritionQuestionResponse();
|
||||
for (String fieldName : index_field_array) {
|
||||
ReflectUtils.setFieldValue(nutritionQuestionResponse, fieldName, document.get(fieldName));
|
||||
}
|
||||
return nutritionQuestionResponse;
|
||||
}
|
||||
|
||||
private Document nutritionQuestionToDocument(SysNutritionQuestion sysNutritionQuestion){
|
||||
Document document = new Document();
|
||||
for (String fieldName : index_field_array) {
|
||||
document.add(new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES));
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
private List<Document> nutritionQuestionToDocument(List<SysNutritionQuestion> sysNutritionQuestionList){
|
||||
List<Document> result = new ArrayList<>();
|
||||
for (SysNutritionQuestion sysNutritionQuestion : sysNutritionQuestionList) {
|
||||
Document document = new Document();
|
||||
for (String fieldName : index_field_array) {
|
||||
document.add(new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES));
|
||||
}
|
||||
result.add(document);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package com.stdiet.custom.utils;
|
||||
|
||||
import com.stdiet.common.utils.StringUtils;
|
||||
import com.stdiet.custom.service.impl.SysNutritionQuestionServiceImpl;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.index.*;
|
||||
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
|
||||
@ -61,7 +60,7 @@ public class LuceneIndexUtils {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public int addIndexs(ArrayList<Document> documents) throws IOException {
|
||||
public int addIndexs(List<Document> documents) throws IOException {
|
||||
|
||||
//流 读取索引库 加装工具
|
||||
indexWriter = new IndexWriter(directory,config);
|
||||
@ -143,12 +142,12 @@ public class LuceneIndexUtils {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean deleteOne(Term term) throws IOException {
|
||||
|
||||
public boolean deleteOneByPrimaryKey(Long id) throws IOException {
|
||||
|
||||
//流 读取索引库 加装工具
|
||||
indexWriter = new IndexWriter(directory,config);
|
||||
|
||||
TermQuery term = new TermQuery(new Term(default_primary_key, id+""));
|
||||
try {
|
||||
indexWriter.deleteDocuments(term);
|
||||
indexWriter.commit();
|
||||
@ -163,6 +162,34 @@ public class LuceneIndexUtils {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除索引库中一条数据
|
||||
* @param term
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean deleteOneByPrimaryIds(Long[] ids) throws IOException {
|
||||
|
||||
//流 读取索引库 加装工具
|
||||
indexWriter = new IndexWriter(directory,config);
|
||||
|
||||
try {
|
||||
for (Long id : ids) {
|
||||
TermQuery term = new TermQuery(new Term(default_primary_key, id+""));
|
||||
indexWriter.deleteDocuments(term);
|
||||
indexWriter.commit();
|
||||
}
|
||||
return true;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
indexWriter.rollback();
|
||||
return false;
|
||||
}finally {
|
||||
indexWriter.close();
|
||||
indexWriter= null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改索引库中一条数据
|
||||
* 注意:此处修改为 根据查找条件修改 如果有则修改 没有则新添 多条则修改一条(索引库的底层其实做法是 先删除,后修改- -)
|
||||
@ -171,14 +198,14 @@ public class LuceneIndexUtils {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean updateOne(Term term, Document document) throws IOException {
|
||||
public boolean updateByPrimaryId(Long id, Document document) throws IOException {
|
||||
|
||||
try {
|
||||
|
||||
//流 读取索引库 加装工具
|
||||
indexWriter = new IndexWriter(directory,config);
|
||||
|
||||
indexWriter.updateDocument(term,document);
|
||||
indexWriter.updateDocument(new Term(default_primary_key, id+""),document);
|
||||
indexWriter.commit();
|
||||
return true;
|
||||
}catch (Exception e){
|
||||
@ -281,39 +308,4 @@ public class LuceneIndexUtils {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
try{
|
||||
LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(SysNutritionQuestionServiceImpl.index_path);
|
||||
/*for (int i = 1 ; i < 100; i++){
|
||||
Document document = new Document();
|
||||
document.add(new TextField("id",i+"", Field.Store.YES));
|
||||
document.add(new TextField("title","什么食物是脂肪杀手?", Field.Store.YES));
|
||||
document.add(new TextField("content","黄瓜", Field.Store.YES));
|
||||
document.add(new TextField("key","脂肪|杀手|食物", Field.Store.YES));
|
||||
luceneIndexUtils.addIndexOne(document);
|
||||
|
||||
}*/
|
||||
/*Document document = new Document();
|
||||
document.add(new TextField("id","1", Field.Store.YES));
|
||||
document.add(new TextField("title","什么食物是脂肪杀手?", Field.Store.YES));
|
||||
document.add(new TextField("content","黄瓜", Field.Store.YES));
|
||||
document.add(new TextField("key","脂肪|杀手|食物", Field.Store.YES));
|
||||
luceneIndexUtils.addIndexOne(document);*/
|
||||
|
||||
String[] columns = {"key","content","title"};
|
||||
Map<String,Object> map = luceneIndexUtils.queryByKeyword("猝死", SysNutritionQuestionServiceImpl.index_field_array, 1,10);
|
||||
|
||||
System.out.println(Long.parseLong(map.get("total").toString()));
|
||||
|
||||
for(Document document : (List<Document>)map.get("data")){
|
||||
System.out.println(document.get("id")+"-"+document.get("key"));
|
||||
}
|
||||
|
||||
//LuceneIndexUtils.printAnalysisResult("什么食物是脂肪杀手?");
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -111,4 +111,15 @@
|
||||
order by id desc
|
||||
</select>
|
||||
|
||||
<!-- 查询总数 -->
|
||||
<select id="getNutritionQuestionCount" resultType="int">
|
||||
select count(id) from sys_nutrition_question where del_flag = 0
|
||||
</select>
|
||||
|
||||
|
||||
<!-- 分页查询-->
|
||||
<select id="getNutritionQuestionListByPage" resultMap="SysNutritionQuestionResult">
|
||||
select * from sys_nutrition_question where del_flag = 0 order by id asc limit #{start},#{pageSize}
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
x
Reference in New Issue
Block a user