|
|
|
@ -4,8 +4,8 @@ import cn.hutool.core.util.ReflectUtil; |
|
|
|
|
import com.keyware.shandan.bianmu.entity.DirectoryVo; |
|
|
|
|
import com.keyware.shandan.bianmu.enums.ReviewStatus; |
|
|
|
|
import com.keyware.shandan.bianmu.service.DirectoryService; |
|
|
|
|
import com.keyware.shandan.browser.entity.FullSearchParam; |
|
|
|
|
import com.keyware.shandan.browser.entity.PageVo; |
|
|
|
|
import com.keyware.shandan.browser.entity.SearchResultSort; |
|
|
|
|
import com.keyware.shandan.common.constants.DirConstant; |
|
|
|
|
import com.keyware.shandan.common.util.StreamUtil; |
|
|
|
|
import com.keyware.shandan.common.util.StringUtils; |
|
|
|
@ -17,20 +17,18 @@ 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.builder.SearchSourceBuilder; |
|
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; |
|
|
|
|
import org.elasticsearch.search.sort.FieldSortBuilder; |
|
|
|
|
import org.elasticsearch.search.sort.SortOrder; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.data.elasticsearch.annotations.FieldType; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.lang.annotation.Annotation; |
|
|
|
|
import java.lang.reflect.Field; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -48,10 +46,15 @@ public class FileSearchService { |
|
|
|
|
@Autowired |
|
|
|
|
private DirectoryService directoryService; |
|
|
|
|
|
|
|
|
|
public PageVo searchFile(PageVo page, String text, String metaId, SearchResultSort sort) throws IOException { |
|
|
|
|
public PageVo searchFile(FullSearchParam param) throws IOException { |
|
|
|
|
|
|
|
|
|
SearchRequest request = Requests.searchRequest("shandan"); |
|
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
|
|
|
|
searchSourceBuilder.from(page.getPage() * page.getSize() - page.getSize()).size(page.getSize()); |
|
|
|
|
searchSourceBuilder.from(param.getPage() * param.getSize() - param.getSize()).size(param.getSize()); |
|
|
|
|
|
|
|
|
|
String metaId = param.getMetaId(); |
|
|
|
|
String text = param.getSearch(); |
|
|
|
|
String[] searchFields = param.getSearchFields().split(","); |
|
|
|
|
|
|
|
|
|
BoolQueryBuilder builder = QueryBuilders.boolQuery(); |
|
|
|
|
if (!DirConstant.DIR_ROOT_ID.equals(metaId)) { |
|
|
|
@ -61,19 +64,19 @@ public class FileSearchService { |
|
|
|
|
builder.must(QueryBuilders.termsQuery("entityId", ids)); |
|
|
|
|
} |
|
|
|
|
if (StringUtils.isNotBlank(text)) { |
|
|
|
|
BoolQueryBuilder should = QueryBuilders.boolQuery(); |
|
|
|
|
should.should(QueryBuilders.matchQuery("fileName", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("entryStaff", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("equipmentModel", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("source", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("taskCode", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("taskNature", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("troopCode", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("targetNumber", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("missileNumber", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("remark", text)); |
|
|
|
|
should.should(QueryBuilders.matchQuery("text", text)); |
|
|
|
|
builder.must(should); |
|
|
|
|
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); |
|
|
|
|
String[] includesCol = {"fileName", "entryStaff", "equipmentModel", "source", "taskCode", "taskNature", "troopCode", "targetNumber", "missileNumber", "themeTask", "remark", "text"}; |
|
|
|
|
|
|
|
|
|
for (String colName : searchFields) { |
|
|
|
|
if (Arrays.asList(includesCol).contains(colName)) { |
|
|
|
|
if ("phrase".equals(param.getSearchType())) { |
|
|
|
|
boolQuery.should(QueryBuilders.matchPhraseQuery(colName, text)); |
|
|
|
|
} else { |
|
|
|
|
boolQuery.should(QueryBuilders.matchQuery(colName, text)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
builder.must(boolQuery); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 高亮显示设置
|
|
|
|
@ -87,23 +90,23 @@ public class FileSearchService { |
|
|
|
|
searchSourceBuilder.query(builder).highlighter(highlightBuilder); |
|
|
|
|
|
|
|
|
|
// 设置排序
|
|
|
|
|
if (!sort.isEmpty()) { |
|
|
|
|
String sortField = sort.getField(); |
|
|
|
|
if (!param.isEmpty()) { |
|
|
|
|
String sortField = param.getField(); |
|
|
|
|
// 使用反射获取文件实体类中字段上的注解设置的字段类型
|
|
|
|
|
Field field = ReflectUtil.getField(SysFile.class, sortField); |
|
|
|
|
if (field != null) { |
|
|
|
|
org.springframework.data.elasticsearch.annotations.Field fieldAnno = field.getAnnotation(org.springframework.data.elasticsearch.annotations.Field.class); |
|
|
|
|
if(fieldAnno.type() != FieldType.Text |
|
|
|
|
if (fieldAnno.type() != FieldType.Text |
|
|
|
|
&& fieldAnno.type() != FieldType.Object |
|
|
|
|
&& fieldAnno.type() != FieldType.Auto |
|
|
|
|
&& fieldAnno.type() != FieldType.Nested |
|
|
|
|
&& fieldAnno.type() != FieldType.Ip |
|
|
|
|
&& fieldAnno.type() != FieldType.Attachment){ |
|
|
|
|
if(fieldAnno.type() == FieldType.Keyword){ |
|
|
|
|
&& fieldAnno.type() != FieldType.Attachment) { |
|
|
|
|
if (fieldAnno.type() == FieldType.Keyword) { |
|
|
|
|
sortField = sortField.concat(".keyword"); |
|
|
|
|
} |
|
|
|
|
searchSourceBuilder.sort(sortField, SortOrder.fromString(sort.getSort())); |
|
|
|
|
}else { |
|
|
|
|
searchSourceBuilder.sort(sortField, SortOrder.fromString(param.getSort())); |
|
|
|
|
} else { |
|
|
|
|
log.error("不支持的排序类型:{}:{}", sortField, fieldAnno.type()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -113,7 +116,7 @@ public class FileSearchService { |
|
|
|
|
// 设置排序
|
|
|
|
|
|
|
|
|
|
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); |
|
|
|
|
return PageVo.ofSearchHits(response.getHits(), page); |
|
|
|
|
return PageVo.ofSearchHits(response.getHits(), param); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<String> getChildrenByDirId(String dirId) { |
|
|
|
|