|
|
@@ -3,17 +3,18 @@ package com.doc.biz.controller;
|
|
|
import com.doc.biz.domain.DocInfo;
|
|
|
import com.doc.biz.domain.DocSpace;
|
|
|
import com.doc.biz.domain.EsDocInfo;
|
|
|
+import com.doc.biz.service.IDocDirService;
|
|
|
import com.doc.biz.service.IDocInfoService;
|
|
|
import com.doc.biz.service.IDocSpaceService;
|
|
|
import com.doc.biz.service.IEsDocInfoService;
|
|
|
import com.doc.common.config.EsConfig;
|
|
|
import com.doc.common.core.domain.AjaxResult;
|
|
|
-import com.doc.common.enums.SpaceType;
|
|
|
import com.doc.common.utils.SecurityUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
|
import io.swagger.annotations.ApiImplicitParams;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import javafx.util.Pair;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
@@ -37,62 +38,99 @@ import java.util.List;
|
|
|
@RequestMapping("/es")
|
|
|
public class ElasticSearchController {
|
|
|
@Resource
|
|
|
- private IEsDocInfoService esDocInfoService;
|
|
|
+ private IEsDocInfoService esService;
|
|
|
@Resource
|
|
|
private IDocInfoService infoService;
|
|
|
@Resource
|
|
|
private EsConfig esConfig;
|
|
|
@Resource
|
|
|
- private IDocSpaceService docSpaceService;
|
|
|
+ private IDocSpaceService spaceService;
|
|
|
+ @Resource
|
|
|
+ private IDocDirService dirService;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 模糊查询ik分词器分词的content字段
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return 查询结果
|
|
|
*/
|
|
|
@GetMapping("/query")
|
|
|
@ApiOperation("模糊查询")
|
|
|
- @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "空间类型(1.公共空间 2.部门空间 3.个人空间", required = true, dataTypeClass = String.class), @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = true, dataTypeClass = String.class), @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataTypeClass = Integer.class), @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataTypeClass = Integer.class)})
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "type", value = "空间类型(1.公共空间 2.部门空间 3.个人空间", required = true, dataTypeClass = String.class),
|
|
|
+ @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = true, dataTypeClass = String.class),
|
|
|
+ @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataTypeClass = Integer.class),
|
|
|
+ @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataTypeClass = Integer.class)
|
|
|
+ })
|
|
|
public AjaxResult query(String type, String keyword, int page, int size) {
|
|
|
- //因为es查询是从第0页开始,所以页码要-1才行
|
|
|
- page = page - 1;
|
|
|
- DocSpace space = new DocSpace();
|
|
|
- space.setSpaceType(type);
|
|
|
+ DocSpace space = spaceService.selectDocSpaceListByType(type);
|
|
|
|
|
|
- if (type.equals(SpaceType.DEPT.getValue())) {
|
|
|
- space.setOwner(SecurityUtils.getDeptId());
|
|
|
- } else if (type.equals(SpaceType.PERSONAL.getValue())) {
|
|
|
- space.setOwner(SecurityUtils.getUserId());
|
|
|
+ Pair<Long, List<SearchHit<EsDocInfo>>> pair = findByContent(space.getSpaceId(), keyword, page, size);
|
|
|
+ if (pair != null) {
|
|
|
+ return AjaxResult.success("查询成功", pair.getValue()).put("total", pair.getKey());
|
|
|
+ } else {
|
|
|
+ return AjaxResult.success("无结果");
|
|
|
}
|
|
|
- List<DocSpace> list = docSpaceService.selectDocSpaceList(space);
|
|
|
- if (list.size() > 0) {
|
|
|
- String indexName = "docs_" + list.get(0).getSpaceId();
|
|
|
- esConfig.setIndexName(indexName);
|
|
|
- try {
|
|
|
- Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Order.desc("id")));
|
|
|
- long total = esDocInfoService.countByContent(keyword);
|
|
|
- List<SearchHit<EsDocInfo>> all = esDocInfoService.findByContent(keyword, pageable);
|
|
|
- List<SearchHit<EsDocInfo>> result = new ArrayList<>();
|
|
|
- if (all.size() > 0) {
|
|
|
- for (SearchHit<EsDocInfo> re : all) {
|
|
|
- EsDocInfo esDocInfo = re.getContent();
|
|
|
- esDocInfo.setContent("");
|
|
|
- DocInfo docInfo = infoService.selectDocInfoByDocId(esDocInfo.getId());
|
|
|
- if (docInfo != null) {
|
|
|
- esDocInfo.setDocInfo(docInfo);
|
|
|
- result.add(re);
|
|
|
- }
|
|
|
- }
|
|
|
- return AjaxResult.success("查询成功", result).put("total", total);
|
|
|
- } else {
|
|
|
- return AjaxResult.success("无结果");
|
|
|
- }
|
|
|
- } catch (NoSuchIndexException e) {
|
|
|
- return AjaxResult.success("无结果");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模糊查询ik分词器分词的content字段
|
|
|
+ *
|
|
|
+ * @return 查询结果
|
|
|
+ */
|
|
|
+ @GetMapping("/search")
|
|
|
+ @ApiOperation("模糊查询所有")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = true, dataTypeClass = String.class),
|
|
|
+ @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataTypeClass = Integer.class),
|
|
|
+ @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataTypeClass = Integer.class)
|
|
|
+ })
|
|
|
+ public AjaxResult search(String type, String keyword, int page, int size) {
|
|
|
+ List<DocSpace> spaceList = spaceService.getSpaceByUser(SecurityUtils.getUserId());
|
|
|
+ Long total = 0L;
|
|
|
+ List<SearchHit<EsDocInfo>> result = new ArrayList<>();
|
|
|
+ for (DocSpace space : spaceList) {
|
|
|
+ Pair<Long, List<SearchHit<EsDocInfo>>> pair = findByContent(space.getSpaceId(), keyword, page, size);
|
|
|
+ if (pair != null) {
|
|
|
+ total += pair.getKey();
|
|
|
+ result.addAll(pair.getValue());
|
|
|
}
|
|
|
+ }
|
|
|
+ if (total != 0) {
|
|
|
+ return AjaxResult.success("查询成功", result).put("total", total);
|
|
|
} else {
|
|
|
return AjaxResult.success("无结果");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public Pair<Long, List<SearchHit<EsDocInfo>>> findByContent(Long spaceId, String keyword, int page, int size) {
|
|
|
+ //因为es查询是从第0页开始,所以页码要-1才行
|
|
|
+ page = page - 1;
|
|
|
+ String indexName = "docs_" + spaceId;
|
|
|
+ esConfig.setIndexName(indexName);
|
|
|
+ try {
|
|
|
+ Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Order.desc("id")));
|
|
|
+ long total = esService.countByContent(keyword);
|
|
|
+ List<SearchHit<EsDocInfo>> all = esService.findByContent(keyword, pageable);
|
|
|
+ List<SearchHit<EsDocInfo>> result = new ArrayList<>();
|
|
|
+ if (all.size() > 0) {
|
|
|
+ for (SearchHit<EsDocInfo> re : all) {
|
|
|
+ EsDocInfo esDocInfo = re.getContent();
|
|
|
+ esDocInfo.setContent("");
|
|
|
+ DocInfo docInfo = infoService.selectDocInfoByDocId(esDocInfo.getId());
|
|
|
+ if (docInfo != null) {
|
|
|
+ docInfo.setSpace(spaceService.selectDocSpaceBySpaceId(docInfo.getSpaceId()));
|
|
|
+ docInfo.setDir(dirService.selectDocDirByDirId(docInfo.getDirId()));
|
|
|
+ esDocInfo.setDocInfo(docInfo);
|
|
|
+ result.add(re);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new Pair<>(total, result);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (NoSuchIndexException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|