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