首页 > 其他分享 >冲刺8

冲刺8

时间:2023-04-22 23:00:42浏览次数:35  
标签:String 冲刺 searchDoc elasticsearch org import new

1.对安卓的功能进行了改进。

2.由于是第一次写这规模的项目,所以很多问题都是拔出萝卜带出泥。藕断丝连。封装性很差。

3.和前端对和接口。

package com.medicalretrieval.controller;

import com.medicalretrieval.pojo.elasticsearch.Document;
import com.medicalretrieval.pojo.elasticsearch.Paragraph;
import com.medicalretrieval.pojo.elasticsearch.ReturnDoc;
import com.medicalretrieval.pojo.elasticsearch.SearchDoc;
import com.medicalretrieval.service.DocumentService;

import com.medicalretrieval.service.ParagraphService;
import com.medicalretrieval.utils.PDFUtils;
import com.medicalretrieval.utils.Page4Navigator;
import com.medicalretrieval.utils.Transition;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.unit.Fuzziness;
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.fetch.subphase.highlight.SearchContextHighlight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import wiremock.org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <pre>
 *     对于文档的控制
 * </pre>
 * @author 梁宏凯
 */
@RequestMapping("/item/")
@RestController
public class DocumentController {
    @Autowired
    DocumentService documentService;

    @Autowired
    ParagraphService paragraphService;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private PDFUtils pdfUtils;

    @Autowired
    private Transition transition;
    /**
     * <pre>批量保存pdf</pre>
     * @return 提交结果
     */
    @PostMapping("saveBatch")
    public boolean saveBatch(@RequestBody MultipartFile[] multipartFiles) throws IOException {
        List<Document> documents = new ArrayList<>();
        List<File> files = transition.TransitionDocument(multipartFiles,documents);
        if(CollectionUtils.isEmpty(documents)){
            return false;
        }

        List<Paragraph> paragraphs = new ArrayList<>();
        int cnt = 0;
        for (Document d :
                documents) {
            pdfUtils.ReadPDFText(files.get(cnt++),d,paragraphs);
            files.remove(0);
        }
        documentService.saveAll(documents);
        paragraphService.saveAll(paragraphs);
        return true;
    }

    /**
     * <pre>保存一个数据</pre>
     * @param file 文档类
     * @return 提交结果
     */
    @PostMapping("saveOne")
    public boolean saveOne(@RequestBody MultipartFile file) throws IOException {

        Document document =new Document();
        File file1 = transition.TransitionDocument(file,document);
        List<Paragraph> paragraphs = new ArrayList<>();
        System.out.println(document);
        pdfUtils.ReadPDFText(file1,document,paragraphs);
        paragraphService.saveAll(paragraphs);
        System.out.println("传输成功!!!!!");
        System.out.println(document);
        documentService.saveOne(document);
        return true;
    }


    /**
     * <pre>通过文章标题查找文档</pre>
     * @param title 文章标题
     * @return 文档类的链表
     */
    @GetMapping("findByTitle")
    public Page4Navigator<ReturnDoc> findDocumentByTitle(
                                                         @RequestParam(value = "title",defaultValue = "") String title,
                                                         @RequestParam(value = "current",defaultValue = "1")int current,
                                                         @RequestParam(value = "pageSize",defaultValue = "10")int pageSize
                                                        )
    {
        NativeSearchQueryBuilder query = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if(StringUtils.isNotBlank(title)){
            boolQueryBuilder.must(QueryBuilders.matchQuery("title",title));
        }
        query.withQuery(boolQueryBuilder);

        Pageable pageable = PageRequest.of(current-1,pageSize);
        SearchHits<Document> searchHits = elasticsearchRestTemplate.search(query.build(),Document.class, IndexCoordinates.of("document"));
        searchHits.getTotalHits();
        List<ReturnDoc> list = new ArrayList<>();

        for (SearchHit<Document>searchHit:searchHits){
            ReturnDoc returnDoc = new ReturnDoc();
            returnDoc.setDocument(searchHit.getContent());
            returnDoc.setPage(1);
            returnDoc.setScore(searchHit.getScore()*100);

            list.add(returnDoc);
        }

        Page<ReturnDoc> page = new PageImpl<>(list, pageable, searchHits.getTotalHits());

        return new Page4Navigator<>(page, 5);

    }

    private void createQuery(BoolQueryBuilder queryBuilder, String subject, String searchTypeOf, String linkOf,String fieldName){
        if(subject!=null && subject.length()>0){
            if("precision".equals(searchTypeOf)){
                //精确
                if("and".equals(linkOf)){
                    queryBuilder.must(QueryBuilders.matchQuery(fieldName,subject));
                }else if("or".equals(linkOf)){
                    queryBuilder.should(QueryBuilders.matchQuery(fieldName,subject));
                }else{
                    queryBuilder.mustNot(QueryBuilders.matchQuery(fieldName,subject));
                }
            }else{
                //模糊
                if("and".equals(linkOf)){
                    queryBuilder.must(QueryBuilders.fuzzyQuery(fieldName,subject).fuzziness(Fuzziness.AUTO));
                }else if("or".equals(linkOf)){
                    queryBuilder.should(QueryBuilders.fuzzyQuery(fieldName,subject).fuzziness(Fuzziness.AUTO));
                }else{
                    queryBuilder.mustNot(QueryBuilders.fuzzyQuery(fieldName,subject).fuzziness(Fuzziness.AUTO));
                }
            }
        }
    }

    @PostMapping("advancedSearch")
    public Page4Navigator<ReturnDoc> advanceSearch(@RequestBody SearchDoc searchDoc){

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

//        boolQueryBuilder.must(QueryBuilders.matchQuery("title",searchDoc.getTitle()))
        createQuery(boolQueryBuilder,searchDoc.getTitle(),searchDoc.getSearchTypeOfTitle(),searchDoc.getLinkOfTitle(),"title");
        createQuery(boolQueryBuilder,searchDoc.getAuthor(),searchDoc.getSearchTypeOfAuthor(),searchDoc.getLinkOfAuthor(),"author");
        createQuery(boolQueryBuilder,searchDoc.getContent(),searchDoc.getSearchTypeOfContent(),searchDoc.getLinkOfContent(),"pageContents");
        builder.withQuery(boolQueryBuilder);
        SearchHits<Document> searchHits = elasticsearchRestTemplate.search(builder.build(), Document.class, IndexCoordinates.of("document"));


        builder = new NativeSearchQueryBuilder();
        boolQueryBuilder = QueryBuilders.boolQuery();
        createQuery(boolQueryBuilder,searchDoc.getContent(),searchDoc.getSearchTypeOfContent(),searchDoc.getLinkOfContent(),"content");
        createQuery(boolQueryBuilder,searchDoc.getContent(),searchDoc.getSearchTypeOfContent(),searchDoc.getLinkOfContent(),"imgInfos");

        builder.withHighlightFields(
                new HighlightBuilder.Field("content")
                        .preTags("")
                        .postTags("")
                        .fragmentSize(50)
                ,new HighlightBuilder.Field("imgInfos")
                        .preTags("")
                        .postTags("")
                        .fragmentSize(50)
        );
        builder.withQuery(boolQueryBuilder);
        SearchHits<Paragraph> paragraph = elasticsearchRestTemplate.search(builder.build(), Paragraph.class, IndexCoordinates.of("paragraph"));
        Map<Long,List<String>> map = new HashMap<>();
        for (SearchHit<Paragraph> p:paragraph){
            Long id = p.getContent().getId();
            List<String> field = p.getHighlightField("content");
            field.addAll(p.getHighlightField("imgInfos"));
            if (map.containsKey(id/10000)){
                map.put(id/10000,field);
            }else {
                map.get(id/10000).addAll(field);
            }
        }
        Pageable pageable = PageRequest.of(searchDoc.getCurrent()-1, searchDoc.getPageSize(), Sort.by(Sort.Direction.DESC,"score"));

        List<ReturnDoc> list = new ArrayList<>();
        for(SearchHit<Document> d:searchHits){
            ReturnDoc returnDoc = new ReturnDoc();
            returnDoc.setDocument(d.getContent());
            returnDoc.setPage(1);
            returnDoc.setScore(d.getScore());
            returnDoc.setAbstract(map.get(d.getContent().getId()));
            list.add(returnDoc);
        }

        Page<ReturnDoc> page = new PageImpl<>(list,pageable, searchHits.getTotalHits());
        return new Page4Navigator<>(page,5);
    }

    @GetMapping("find")
    public List<PdfRestInfo> find(@RequestParam(value = "title",defaultValue = "") String title,@RequestParam(value = "authors",defaultValue = "")List<String>authors,@RequestParam(value = "content",defaultValue = "")String content,@RequestParam(value = "current",defaultValue = "1")int current){
        List<PdfRestInfo> pdfRestInfoList = new ArrayList<>();
        List<ReturnDoc> returnDocList = findDocumentByTitle(title, current,100).getContent();
        if(returnDocList==null){
            return null;
        }
        for (ReturnDoc r:returnDocList){
            PdfRestInfo pdfRestInfo = new PdfRestInfo();

            pdfRestInfo.setAuthors(r.getDocument().getAuthor().toString().replaceAll(",|\\[|]",""));
            pdfRestInfo.setFilePath(r.getDocument().getUrl());
            pdfRestInfo.setName(r.getDocument().getTitle());
            String str = "";
            for (String s:r.getAbstract()){
                str=str+s+'\n';

            }
            pdfRestInfo.setTextSummary(str);

            pdfRestInfoList.add(pdfRestInfo);
        }
        return pdfRestInfoList;
    }

    private class PdfRestInfo
    {
        private String filePath;

        private String name;

        private String authors;
        private String TextSummary;


        public String getAuthors() {
            return authors;
        }

        public void setAuthors(String authors) {
            this.authors = authors;
        }



        public void setFilePath(String filePath){
            this.filePath = filePath;
        }
        public String getFilePath(){
            return this.filePath;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getName(){
            return this.name;
        }
        public void setTextSummary(String TextSummary){
            this.TextSummary = TextSummary;
        }
        public String getTextSummary(){
            return this.TextSummary;
        }
    }




}

 

标签:String,冲刺,searchDoc,elasticsearch,org,import,new
From: https://www.cnblogs.com/lhk20213937/p/17344370.html

相关文章

  • 团队冲刺8
    1.任务量:10天目前已经花费的时间:7天还剩余的时间:3天3. 4.调试vue和springboot的接口,对接好。importrequestsfrom"./request";importmockRequestfrom'./mockRequest';//注册的接口/user/exportconstreqRegister=(data)=>requests({url:'/user/',met......
  • 冲刺清北营 4
    今天成人礼。于是打算写点无营养鲜花。口胡场都能被打自闭,真有你的。一花二乃三玖四叶五月六小.jpg那你说的对。猴戏世家P4737。场上脑抽,想到了后一半,没想到前一半。死于一直想从下到上扫描线无果,然后开始想怎么在线做。从上到下扫描线,然后开个set维护栅栏。扫到一......
  • 冲刺7
    1.写完了安卓的功能。2.安卓有些繁琐,xml,Java代码,布局。都得需要设置相应的东西。3.对安卓代码进行改进。4.packagecom.example.medicalretrieval;importandroid.content.Intent;importandroid.net.Uri;importandroid.os.Bundle;importandroidx.fragment.app.Fragm......
  • 团队冲刺7
    1.任务量:10天目前已经花费的时间:6天还剩余的时间:4天3. 4.vue的部分完成,只不过还没调试vue和springboot的接口。<template><divclass="login-wrap"><el-formclass="login-container"><h1class="title">用户登录:</h1>......
  • 青岛市程序设计竞赛冲刺①
    2021年青岛市小学组第三题原题: 解题代码:#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>usingnamespacestd;constintN=5e2+5,dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};intn,m,vis[N][N],ans=0;charc......
  • 冲刺6
    这个作业属于哪个课程2023软件工程-双学位这个作业要求在哪里团队作业4——项目冲刺这个作业的目标团队项目Scrum冲刺day6目录1.会议1.1今日已完成的工作1.2明日计划完成的工作1.3工作中遇到的困难2.燃尽图3.代码/文档签入记录签入记录对应的Issue内容与链接,代......
  • 团队项目冲刺02
    信息详情这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/2023softwareengine这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/2023softwareengine/homework/12920这个作业的目标项目冲刺目录目录目录一、会议1.昨天已完成的工作2.今天......
  • 团队冲刺第二天
    今天完成了用户界面的绝大部分 <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><divid="app"><el-tabsv-model="activeName......
  • 团队项目4——项目冲刺
    这个作业属于哪个课程2023软件工程——双学位这个作业的要求何在团队作业4——项目冲刺作业目标团队作业Scrum冲刺博客合集项目地址https://gitcode.net/m0_62281440/teamwork每日博客合集博客地址第一篇Scrum冲刺博客https://www.cnblogs.com/bk......
  • 团队冲刺第七天
    今日我预计花1个多小时时间去将人脸识别导入项目中,但实际却很差强人意,为团队效率考虑,我们决定先完善pc端。               今日完成:前端qt设计界面学习中,改去协助做pc界面       明日目标:初步做出qt界面       遇到问......