|
|
|
@ -7,10 +7,13 @@ import com.keyware.shandan.bianmu.service.DirectoryService; |
|
|
|
|
import com.keyware.shandan.browser.SearchLogProcessor; |
|
|
|
|
import com.keyware.shandan.browser.entity.FullSearchParam; |
|
|
|
|
import com.keyware.shandan.browser.entity.PageVo; |
|
|
|
|
import com.keyware.shandan.browser.entity.SearchResultRow; |
|
|
|
|
import com.keyware.shandan.common.constants.DirConstant; |
|
|
|
|
import com.keyware.shandan.common.util.StreamUtil; |
|
|
|
|
import com.keyware.shandan.common.util.StringUtils; |
|
|
|
|
import com.keyware.shandan.system.entity.SysFile; |
|
|
|
|
import com.keyware.shandan.system.entity.SysSetting; |
|
|
|
|
import com.keyware.shandan.system.service.SysSettingService; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.elasticsearch.action.search.SearchRequest; |
|
|
|
|
import org.elasticsearch.action.search.SearchResponse; |
|
|
|
@ -18,7 +21,9 @@ import org.elasticsearch.client.RequestOptions; |
|
|
|
|
import org.elasticsearch.client.Requests; |
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient; |
|
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder; |
|
|
|
|
import org.elasticsearch.index.query.QueryBuilder; |
|
|
|
|
import org.elasticsearch.index.query.QueryBuilders; |
|
|
|
|
import org.elasticsearch.search.SearchHits; |
|
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder; |
|
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; |
|
|
|
|
import org.elasticsearch.search.sort.SortOrder; |
|
|
|
@ -28,9 +33,8 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.lang.reflect.Field; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* FileSearchService |
|
|
|
@ -41,6 +45,7 @@ import java.util.List; |
|
|
|
|
@Slf4j |
|
|
|
|
@Service |
|
|
|
|
public class FileSearchService { |
|
|
|
|
private final String[] imageTypes = {"apng", "avif", "jpeg", "jpg", "png", "svg", "webp", "bmp", "gif", "ico", "tiff"}; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private RestHighLevelClient esClient; |
|
|
|
@ -48,6 +53,8 @@ public class FileSearchService { |
|
|
|
|
private DirectoryService directoryService; |
|
|
|
|
@Autowired |
|
|
|
|
private SearchLogProcessor searchLogProcessor; |
|
|
|
|
@Autowired |
|
|
|
|
private SysSettingService settingService; |
|
|
|
|
|
|
|
|
|
public PageVo searchFile(FullSearchParam param) throws IOException { |
|
|
|
|
SearchRequest request = Requests.searchRequest("shandan"); |
|
|
|
@ -75,7 +82,7 @@ public class FileSearchService { |
|
|
|
|
List<String> ids = new ArrayList<>(); |
|
|
|
|
ids.add(metaId); |
|
|
|
|
ids.addAll(getChildrenByDirId(metaId)); |
|
|
|
|
builder.must(QueryBuilders.termsQuery("entityId", ids)); |
|
|
|
|
builder.must(QueryBuilders.termsQuery("entityId.keyword", ids)); |
|
|
|
|
} |
|
|
|
|
if (StringUtils.hasText(text)) { |
|
|
|
|
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); |
|
|
|
@ -127,8 +134,6 @@ public class FileSearchService { |
|
|
|
|
} |
|
|
|
|
request.source(searchSourceBuilder); |
|
|
|
|
|
|
|
|
|
// 设置排序
|
|
|
|
|
|
|
|
|
|
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); |
|
|
|
|
return PageVo.ofSearchHits(response.getHits(), param); |
|
|
|
|
} |
|
|
|
@ -137,4 +142,46 @@ public class FileSearchService { |
|
|
|
|
DirectoryVo dir = directoryService.getById(dirId); |
|
|
|
|
return StreamUtil.as(directoryService.childrenLists(dir, ReviewStatus.SUBMITTED)).map(DirectoryVo::getId).toList(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public PageVo<Object> searchImage(String dirId, String searchText, int pageNo, int pageSize) throws IOException { |
|
|
|
|
SearchRequest request = Requests.searchRequest("shandan"); |
|
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
|
|
|
|
searchSourceBuilder.from(pageNo * pageSize - pageSize).size(pageSize); |
|
|
|
|
|
|
|
|
|
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); |
|
|
|
|
|
|
|
|
|
queryBuilder.must(QueryBuilders.termsQuery("fileType", imageTypes)); |
|
|
|
|
if (!DirConstant.DIR_ROOT_ID.equals(dirId)) { |
|
|
|
|
List<String> ids = new ArrayList<>(); |
|
|
|
|
ids.add(dirId); |
|
|
|
|
ids.addAll(getChildrenByDirId(dirId)); |
|
|
|
|
queryBuilder.must(QueryBuilders.termsQuery("entityId.keyword", ids)); |
|
|
|
|
} |
|
|
|
|
if (StringUtils.hasText(searchText)) { |
|
|
|
|
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); |
|
|
|
|
boolQuery.should(QueryBuilders.matchPhraseQuery("fileName", searchText)); |
|
|
|
|
queryBuilder.must(boolQuery); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
searchSourceBuilder.query(queryBuilder); |
|
|
|
|
request.source(searchSourceBuilder); |
|
|
|
|
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); |
|
|
|
|
|
|
|
|
|
SysSetting bianmuSetting = settingService.getById("BIANMU"); |
|
|
|
|
PageVo page = new PageVo(); |
|
|
|
|
page.setPage(pageNo); |
|
|
|
|
page.setSize(pageSize); |
|
|
|
|
|
|
|
|
|
SearchHits hits = response.getHits(); |
|
|
|
|
page.setTotal(hits.getTotalHits()); |
|
|
|
|
page.setPageTotal((int) Math.ceil(page.getTotal() / (double) page.getSize())); |
|
|
|
|
page.setRecords(Arrays.stream(hits.getHits()).map(hit -> { |
|
|
|
|
Map<String, Object> dataMap = hit.getSourceAsMap(); |
|
|
|
|
dataMap.put("title", dataMap.get("fileName")); |
|
|
|
|
String path = ((String) dataMap.get("path")).replace("\\", "/"); |
|
|
|
|
dataMap.put("src", bianmuSetting.getSysAddress() + "/upload/" + path); |
|
|
|
|
return dataMap; |
|
|
|
|
}).collect(Collectors.toList())); |
|
|
|
|
return PageVo.ofSearchHits(response.getHits(), page); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|