Search in sources :

Example 1 with FileSearch

use of com.qiwenshare.file.config.es.FileSearch in project qiwen-file by qiwenshare.

the class FileDealComp method uploadESByUserFileId.

public void uploadESByUserFileId(Long userFileId) {
    try {
        Map<String, Object> param = new HashMap<>();
        param.put("userFileId", userFileId);
        List<UserFile> userfileResult = userFileMapper.selectByMap(param);
        if (userfileResult != null && userfileResult.size() > 0) {
            FileSearch fileSearch = new FileSearch();
            BeanUtil.copyProperties(userfileResult.get(0), fileSearch);
            // if (fileSearch.getIsDir() == 0) {
            // 
            // Reader reader = ufopFactory.getReader(fileSearch.getStorageType());
            // ReadFile readFile = new ReadFile();
            // readFile.setFileUrl(fileSearch.getFileUrl());
            // String content = reader.read(readFile);
            // //全文搜索
            // fileSearch.setContent(content);
            // 
            // }
            elasticSearchService.save(fileSearch);
        }
    } catch (Exception e) {
        log.debug("ES更新操作失败,请检查配置");
    }
}
Also used : HashMap(java.util.HashMap) FileSearch(com.qiwenshare.file.config.es.FileSearch)

Example 2 with FileSearch

use of com.qiwenshare.file.config.es.FileSearch in project qiwen-file by qiwenshare.

the class FileController method searchFile.

@Operation(summary = "文件搜索", description = "文件搜索", tags = { "file" })
@GetMapping(value = "/search")
@MyLog(operation = "文件搜索", module = CURRENT_MODULE)
@ResponseBody
public RestResult<SearchHits<FileSearch>> searchFile(SearchFileDTO searchFileDTO) {
    JwtUser sessionUserBean = SessionUtil.getSession();
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    HighlightBuilder.Field allHighLight = new HighlightBuilder.Field("*").preTags("<span class='keyword'>").postTags("</span>");
    queryBuilder.withHighlightFields(allHighLight);
    // 设置分页
    int currentPage = (int) searchFileDTO.getCurrentPage() - 1;
    int pageCount = (int) (searchFileDTO.getPageCount() == 0 ? 10 : searchFileDTO.getPageCount());
    String order = searchFileDTO.getOrder();
    Sort.Direction direction = null;
    if (searchFileDTO.getDirection() == null) {
        direction = Sort.Direction.DESC;
    } else if ("asc".equals(searchFileDTO.getDirection())) {
        direction = Sort.Direction.ASC;
    } else if ("desc".equals(searchFileDTO.getDirection())) {
        direction = Sort.Direction.DESC;
    } else {
        direction = Sort.Direction.DESC;
    }
    if (order == null) {
        queryBuilder.withPageable(PageRequest.of(currentPage, pageCount));
    } else {
        queryBuilder.withPageable(PageRequest.of(currentPage, pageCount, Sort.by(direction, order)));
    }
    DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
    QueryBuilder q1 = QueryBuilders.boolQuery().must(QueryBuilders.multiMatchQuery(searchFileDTO.getFileName(), "fileName", "content")).must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())).boost(// 分词
    1f);
    QueryBuilder q2 = QueryBuilders.boolQuery().must(QueryBuilders.wildcardQuery("fileName", "*" + searchFileDTO.getFileName() + "*")).must(QueryBuilders.wildcardQuery("content", "*" + searchFileDTO.getFileName() + "*")).must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())).boost(// 模糊匹配
    2f);
    disMaxQueryBuilder.add(q1);
    disMaxQueryBuilder.add(q2);
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(disMaxQueryBuilder).withHighlightFields(allHighLight).build();
    // 
    // queryBuilder.withQuery(QueryBuilders.boolQuery()
    // //                .must(QueryBuilders.matchQuery("fileName", searchFileDTO.getFileName()))
    // .must(QueryBuilders.multiMatchQuery(searchFileDTO.getFileName(),"fileName", "content"))
    // .must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId()))
    // ).withQuery(QueryBuilders.wildcardQuery("fileName", "*" + searchFileDTO.getFileName() + "*"));
    SearchHits<FileSearch> search = elasticsearchRestTemplate.search(searchQuery, FileSearch.class);
    return RestResult.success().data(search);
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) Sort(org.springframework.data.domain.Sort) JwtUser(com.qiwenshare.common.util.security.JwtUser) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) FileSearch(com.qiwenshare.file.config.es.FileSearch) Operation(io.swagger.v3.oas.annotations.Operation) MyLog(com.qiwenshare.common.anno.MyLog)

Aggregations

FileSearch (com.qiwenshare.file.config.es.FileSearch)2 MyLog (com.qiwenshare.common.anno.MyLog)1 JwtUser (com.qiwenshare.common.util.security.JwtUser)1 Operation (io.swagger.v3.oas.annotations.Operation)1 HashMap (java.util.HashMap)1 DisMaxQueryBuilder (org.elasticsearch.index.query.DisMaxQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1 Sort (org.springframework.data.domain.Sort)1 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1