SpringBoot+ElasticSearch实现文档内容抽取、高亮分词、全文检索

需求
产品希望我们这边能够实现用户上传PDF、WORD、TXT之内得文本内容,然后用户可以根据附件名称或文件内容模糊查询文件信息,并可以在线查看文件内容。

一、环境
项目开发环境:

后台管理系统springboot+mybatis_plus+mysql+es
搜索引擎:elasticsearch7.9.3 +kibana图形化界面

二、功能实现
1.搭建环境
es+kibana的搭建这里就不介绍了,网上多的是

后台程序搭建也不介绍,这里有一点很重要,Java使用的连接es的包的版本一定要和es的版本对应上,不然你会有各种问题

2.文件内容识别
第一步: 要用es实现文本附件内容的识别,需要先给es安装一个插件:Ingest Attachment Processor Plugin

这知识一个内容识别的插件,还有其它的例如OCR之类的其它插件,有兴趣的可以去搜一下了解一下

Ingest Attachment Processor Plugin是一个文本抽取插件,本质上是利用了Elasticsearch的ingest node功能,提供了关键的预处理器attachment。在安装目录下运行以下命令即可安装。

到es的安装文件bin目录下执行

elasticsearch-plugin install ingest-attachment

因为我们这里es是使用docker安装的,所以需要进入到es的docker镜像里面的bin目录下安装插件

[root@iZuf63d0pqnjrga4pi18udZ plugins]# docker exec -it es bash
[root@elasticsearch elasticsearch]# ls
LICENSE.txt  NOTICE.txt  README.asciidoc  bin  config  data  jdk  lib  logs  modules  plugins
[root@elasticsearch elasticsearch]# cd bin/
[root@elasticsearch bin]# ls
elasticsearch          elasticsearch-certutil  elasticsearch-croneval  elasticsearch-env-from-file  elasticsearch-migrate  elasticsearch-plugin         elasticsearch-setup-passwords  elasticsearch-sql-cli            elasticsearch-syskeygen  x-pack-env           x-pack-watcher-env
elasticsearch-certgen  elasticsearch-cli       elasticsearch-env       elasticsearch-keystore       elasticsearch-node     elasticsearch-saml-metadata  elasticsearch-shard            elasticsearch-sql-cli-7.9.3.jar  elasticsearch-users      x-pack-security-env
[root@elasticsearch bin]# elasticsearch-plugin install ingest-attachment
-> Installing ingest-attachment
-> Downloading ingest-attachment from elastic
[=================================================] 100%?? 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.lang.RuntimePermission accessClassInPackage.sun.java2d.cmm.kcms
* java.lang.RuntimePermission accessDeclaredMembers
* java.lang.RuntimePermission getClassLoader
* java.lang.reflect.ReflectPermission suppressAccessChecks
* java.security.SecurityPermission createAccessControlContext
* java.security.SecurityPermission insertProvider
* java.security.SecurityPermission putProviderProperty.BC
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.Continue with installation? [y/N]y
-> Installed ingest-attachment

显示installed 就表示安装完成了,然后重启es,不然第二步要报错

第二步:创建一个文本抽取的管道

主要是用于将上传的附件转换成文本内容,支持(word,PDF,txt,excel没试,应该也支持)
在这里插入图片描述

{"description": "Extract attachment information","processors": [{"attachment": {"field": "content","ignore_missing": true}},{"remove": {"field": "content"}}]
}

第三步:定义我们内容存储的索引
在这里插入图片描述

{"mappings": {"properties": {"id":{"type": "keyword"},"fileName":{"type": "text","analyzer": "my_ana"},"contentType":{"type": "text","analyzer": "my_ana"},"fileUrl":{"type": "text"},"attachment": {"properties": {"content":{"type": "text","analyzer": "my_ana"}}}}},"settings": {"analysis": {"filter": {"jieba_stop": {"type":        "stop","stopwords_path": "stopword/stopwords.txt"},"jieba_synonym": {"type":        "synonym","synonyms_path": "synonym/synonyms.txt"}},"analyzer": {"my_ana": {"tokenizer": "jieba_index","filter": ["lowercase","jieba_stop","jieba_synonym"]}}}}
}

mapping:定义的是存储的字段格式
setting:索引的配置信息,这边定义了一个分词(使用的是jieba的分词)

注意:内容检索的是attachment.content字段,一定要使用分词,不使用分词的话,检索会检索不出来内容

第四步:测试

在这里插入图片描述

{"id":"1","name":"进口红酒","filetype":"pdf","contenttype":"文章","content":"文章内容"
}

测试内容需要将附件转换成base64格式

在线转换文件的地址:https://www.zhangxinxu.com/sp/base64.html

查询刚刚上传的文件:
在这里插入图片描述

{"took": 861,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 5,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "fileinfo","_type": "_doc","_id": "lkPEgYIBz3NlBKQzXYX9","_score": 1.0,"_source": {"fileName": "测试_20220809164145A002.docx","updateTime": 1660034506000,"attachment": {"date": "2022-08-09T01:38:00Z","content_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document","author": "DELL","language": "lt","content": "内容","content_length": 2572},"createTime": 1660034506000,"fileUrl": "http://localhost:8092/fileInfo/profile/upload/fileInfo/2022/08/09/测试_20220809164145A002.docx","id": 1306333192,"contentType": "文章","fileType": "docx"}},{"_index": "fileinfo","_type": "_doc","_id": "mUPHgYIBz3NlBKQzwIVW","_score": 1.0,"_source": {"fileName": "测试_20220809164527A001.docx","updateTime": 1660034728000,"attachment": {"date": "2022-08-09T01:38:00Z","content_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document","author": "DELL","language": "lt","content": "内容","content_length": 2572},"createTime": 1660034728000,"fileUrl": "http://localhost:8092/fileInfo/profile/upload/fileInfo/2022/08/09/测试_20220809164527A001.docx","id": 1306333193,"contentType": "文章","fileType": "docx"}},{"_index": "fileinfo","_type": "_doc","_id": "JDqshoIBbkTNu1UgkzFK","_score": 1.0,"_source": {"fileName": "txt测试_20220810153351A001.txt","updateTime": 1660116831000,"attachment": {"content_type": "text/plain; charset=UTF-8","language": "lt","content": "内容","content_length": 804},"createTime": 1660116831000,"fileUrl": "http://localhost:8092/fileInfo/profile/upload/fileInfo/2022/08/10/txt测试_20220810153351A001.txt","id": 1306333194,"contentType": "告示","fileType": "txt"}}]}
}

我们调用上传的接口,可以看到文本内容已经抽取到es里面了,后面就可以直接分词检索内容,高亮显示了

三.代码
介绍下代码实现逻辑:文件上传,数据库存储附件信息和附件上传地址;调用es实现文本内容抽取,将抽取的内容放到对应索引下;提供小程序全文检索的api实现根据文件名称关键词联想,文件名称内容全文检索模糊匹配,并高亮显示分词匹配字段;直接贴代码

yml配置文件:

# 数据源配置
spring:# 服务模块devtools:restart:# 热部署开关enabled: true# 搜索引擎elasticsearch:rest:url: 127.0.0.1uris: 127.0.0.1:9200connection-timeout: 1000read-timeout: 3000username: elasticpassword: 123456

elsticsearchConfig(连接配置)

package com.yj.rselasticsearch.domain.config;import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.time.Duration;@Configuration
public class ElasticsearchConfig {@Value("${spring.elasticsearch.rest.url}")private String edUrl;@Value("${spring.elasticsearch.rest.username}")private String userName;@Value("${spring.elasticsearch.rest.password}")private String password;@Beanpublic RestHighLevelClient restHighLevelClient() {//设置连接的用户名密码final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));RestHighLevelClient client =  new RestHighLevelClient(RestClient.builder(new HttpHost(edUrl, 9200,"http")).setHttpClientConfigCallback(httpClientBuilder -> {httpClientBuilder.disableAuthCaching();//保持连接池处于链接状态,该bug曾导致es一段时间没使用,第一次连接访问超时httpClientBuilder.setKeepAliveStrategy(((response, context) -> Duration.ofMinutes(5).toMillis()));return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}));return client;}
}

文件上传保存文件信息并抽取内容到es

实体对象FileInfo

package com.yj.common.core.domain.entity;import com.baomidou.mybatisplus.annotation.TableField;
import com.yj.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;import java.util.Date;@Setter
@Getter
@Document(indexName = "fileinfo",createIndex = false)
public class FileInfo {/*** 主键*/@Field(name = "id", type = FieldType.Integer)private Integer id;/*** 文件名称*/@Field(name = "fileName", type = FieldType.Text,analyzer = "jieba_index",searchAnalyzer = "jieba_index")private String fileName;/*** 文件类型*/@Field(name = "fileType",  type = FieldType.Keyword)private String fileType;/*** 内容类型*/@Field(name = "contentType", type = FieldType.Text)private String contentType;/*** 附件内容*/@Field(name = "attachment.content", type = FieldType.Text,analyzer = "jieba_index",searchAnalyzer = "jieba_index")@TableField(exist = false)private String content;/*** 文件地址*/@Field(name = "fileUrl", type = FieldType.Text)private String fileUrl;/*** 创建时间*/private Date createTime;/*** 更新时间*/private Date updateTime;
}

controller类

package com.yj.rselasticsearch.controller;import com.yj.common.core.controller.BaseController;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.FileInfo;
import com.yj.rselasticsearch.service.FileInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;/*** (file_info)表控制层** @author xxxxx*/
@RestController
@RequestMapping("/fileInfo")
public class FileInfoController extends BaseController {/*** 服务对象*/@Resourceprivate FileInfoService fileInfoService;@PutMapping("uploadFile")public AjaxResult uploadFile(String contentType, MultipartFile file) {return fileInfoService.uploadFileInfo(contentType,file);}
}

serviceImpl实现类

package com.yj.rselasticsearch.service.impl;import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yj.common.config.RuoYiConfig;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.utils.FastUtils;
import com.yj.common.utils.StringUtils;
import com.yj.common.utils.file.FileUploadUtils;
import com.yj.common.utils.file.FileUtils;
import com.yj.framework.config.ServerConfig;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.yj.common.core.domain.entity.FileInfo;
import com.yj.rselasticsearch.mapper.FileInfoMapper;
import com.yj.rselasticsearch.service.FileInfoService;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;@Service
@Slf4j
public class FileInfoServiceImpl implements FileInfoService{@Resourceprivate ServerConfig serverConfig;@Autowired@Qualifier("restHighLevelClient")private RestHighLevelClient client;@Resourceprivate FileInfoMapper fileInfoMapper;/*** 上传文件并进行文件内容识别上传到es* @param contentType* @param file* @return*/@Overridepublic AjaxResult uploadFileInfo(String contentType, MultipartFile file) {if (FastUtils.checkNullOrEmpty(contentType,file)){return AjaxResult.error("请求参数不能为空");}try {// 上传文件路径String filePath = RuoYiConfig.getUploadPath() + "/fileInfo";FileInfo fileInfo = new FileInfo();// 上传并返回新文件名称String fileName = FileUploadUtils.upload(filePath, file);String prefix = fileName.substring(fileName.lastIndexOf(".")+1);File files = File.createTempFile(fileName, prefix);file.transferTo(files);String url = serverConfig.getUrl() + "/fileInfo" + fileName;fileInfo.setFileName(FileUtils.getName(fileName));fileInfo.setFileType(prefix);fileInfo.setFileUrl(url);fileInfo.setContentType(contentType);int result = fileInfoMapper.insertSelective(fileInfo);if (result > 0) {fileInfo = fileInfoMapper.selectOne(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getFileUrl,fileInfo.getFileUrl()));byte[] bytes = getContent(files);String base64 = Base64.getEncoder().encodeToString(bytes);fileInfo.setContent(base64);IndexRequest indexRequest = new IndexRequest("fileinfo");//上传同时,使用attachment pipline进行提取文件indexRequest.source(JSON.toJSONString(fileInfo), XContentType.JSON);indexRequest.setPipeline("attachment");IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);log.info("indexResponse:" + indexResponse);}AjaxResult ajax = AjaxResult.success(fileInfo);return ajax;} catch (Exception e) {return AjaxResult.error(e.getMessage());}}/*** 文件转base64** @param file* @return* @throws IOException*/private byte[] getContent(File file) throws IOException {long fileSize = file.length();if (fileSize > Integer.MAX_VALUE) {log.info("file too big...");return null;}FileInputStream fi = new FileInputStream(file);byte[] buffer = new byte[(int) fileSize];int offset = 0;int numRead = 0;while (offset < buffer.length&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {offset += numRead;}// 确保所有数据均被读取if (offset != buffer.length) {throw new ServiceException("Could not completely read file "+ file.getName());}fi.close();return buffer;}
}

高亮分词检索

参数请求WarningInfoDto

package com.yj.rselasticsearch.domain.dto;import com.yj.common.core.domain.entity.WarningInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.util.List;/*** 前端请求数据传输* WarningInfo* @author luoY*/
@Data
@ApiModel(value ="WarningInfoDto",description = "告警信息")
public class WarningInfoDto{/*** 页数*/@ApiModelProperty("页数")private Integer pageIndex;/*** 每页数量*/@ApiModelProperty("每页数量")private Integer pageSize;/*** 查询关键词*/@ApiModelProperty("查询关键词")private String keyword;/*** 内容类型*/private List<String> contentType;/*** 用户手机号*/private String phone;
}

controller类

package com.yj.rselasticsearch.controller;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yj.common.core.controller.BaseController;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.FileInfo;
import com.yj.common.core.domain.entity.WarningInfo;
import com.yj.rselasticsearch.service.ElasticsearchService;
import com.yj.rselasticsearch.service.WarningInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.yj.rselasticsearch.domain.dto.WarningInfoDto;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** es搜索引擎** @author luoy*/
@Api("搜索引擎")
@RestController
@RequestMapping("es")
public class ElasticsearchController extends BaseController {@Resourceprivate ElasticsearchService elasticsearchService;/*** 告警信息关键词联想** @param warningInfoDto* @return*/@ApiOperation("关键词联想")@ApiImplicitParams({@ApiImplicitParam(name = "contenttype", value = "文档类型", required = true, dataType = "String", dataTypeClass = String.class),@ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "String", dataTypeClass = String.class)})@PostMapping("getAssociationalWordDoc")public AjaxResult getAssociationalWordDoc(@RequestBody WarningInfoDto warningInfoDto, HttpServletRequest request) {List<String> words = elasticsearchService.getAssociationalWordOther(warningInfoDto,request);return AjaxResult.success(words);}/*** 告警信息高亮分词分页查询** @param warningInfoDto* @return*/@ApiOperation("高亮分词分页查询")@ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "String", dataTypeClass = String.class),@ApiImplicitParam(name = "pageIndex", value = "页码", required = true, dataType = "Integer", dataTypeClass = Integer.class),@ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", dataTypeClass = Integer.class),@ApiImplicitParam(name = "contenttype", value = "文档类型", required = true, dataType = "String", dataTypeClass = String.class)})@PostMapping("queryHighLightWordDoc")public AjaxResult queryHighLightWordDoc(@RequestBody WarningInfoDto warningInfoDto,HttpServletRequest request) {IPage<FileInfo> warningInfoListPage = elasticsearchService.queryHighLightWordOther(warningInfoDto,request);return AjaxResult.success(warningInfoListPage);}
}

serviceImpl实现类

package com.yj.rselasticsearch.service.impl;import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.common.constant.DataConstants;
import com.yj.common.constant.HttpStatus;
import com.yj.common.core.domain.entity.FileInfo;
import com.yj.common.core.domain.entity.WarningInfo;
import com.yj.common.core.domain.entity.WhiteList;
import com.yj.common.core.redis.RedisCache;
import com.yj.common.exception.ServiceException;
import com.yj.common.utils.FastUtils;
import com.yj.rselasticsearch.domain.dto.RetrievalRecordDto;
import com.yj.rselasticsearch.domain.dto.WarningInfoDto;
import com.yj.rselasticsearch.domain.vo.MemberVo;
import com.yj.rselasticsearch.service.*;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;@Service
@Slf4j
public class ElasticsearchServiceImpl implements ElasticsearchService {@Resourceprivate WhiteListService whiteListService;@Autowired@Qualifier("restHighLevelClient")private RestHighLevelClient client;@Autowiredprivate RedisCache redisCache;@Resourceprivate TokenService tokenService;/*** 文档信息关键词联想(根据输入框的词语联想文件名称)** @param warningInfoDto* @return*/@Overridepublic List<String> getAssociationalWordOther(WarningInfoDto warningInfoDto, HttpServletRequest request) {//需要查询的字段BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().should(QueryBuilders.matchBoolPrefixQuery("fileName", warningInfoDto.getKeyword()));//contentType标签内容过滤boolQueryBuilder.must(QueryBuilders.termsQuery("contentType", warningInfoDto.getContentType()));//构建高亮查询NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withHighlightFields(new HighlightBuilder.Field("fileName")).withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>")).build();//查询SearchHits<FileInfo> search = null;try {search = elasticsearchRestTemplate.search(searchQuery, FileInfo.class);} catch (Exception ex) {ex.printStackTrace();throw new ServiceException(String.format("操作错误,请联系管理员!%s", ex.getMessage()));}//设置一个最后需要返回的实体类集合List<String> resultList = new LinkedList<>();//遍历返回的内容进行处理for (org.springframework.data.elasticsearch.core.SearchHit<FileInfo> searchHit : search.getSearchHits()) {//高亮的内容Map<String, List<String>> highlightFields = searchHit.getHighlightFields();//将高亮的内容填充到content中searchHit.getContent().setFileName(highlightFields.get("fileName") == null ? searchHit.getContent().getFileName() : highlightFields.get("fileName").get(0));if (highlightFields.get("fileName") != null) {resultList.add(searchHit.getContent().getFileName());}}//list去重List<String> newResult = null;if (!FastUtils.checkNullOrEmpty(resultList)) {if (resultList.size() > 9) {newResult = resultList.stream().distinct().collect(Collectors.toList()).subList(0, 9);} else {newResult = resultList.stream().distinct().collect(Collectors.toList());}}return newResult;}/*** 高亮分词搜索其它类型文档** @param warningInfoDto* @param request* @return*/@Overridepublic IPage<FileInfo> queryHighLightWordOther(WarningInfoDto warningInfoDto, HttpServletRequest request) {//分页Pageable pageable = PageRequest.of(warningInfoDto.getPageIndex() - 1, warningInfoDto.getPageSize());//需要查询的字段,根据输入的内容分词全文检索fileName和content字段BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().should(QueryBuilders.matchBoolPrefixQuery("fileName", warningInfoDto.getKeyword())).should(QueryBuilders.matchBoolPrefixQuery("attachment.content", warningInfoDto.getKeyword()));//contentType标签内容过滤boolQueryBuilder.must(QueryBuilders.termsQuery("contentType", warningInfoDto.getContentType()));//构建高亮查询NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withHighlightFields(new HighlightBuilder.Field("fileName"), new HighlightBuilder.Field("attachment.content")).withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>")).build();//查询SearchHits<FileInfo> search = null;try {search = elasticsearchRestTemplate.search(searchQuery, FileInfo.class);} catch (Exception ex) {ex.printStackTrace();throw new ServiceException(String.format("操作错误,请联系管理员!%s", ex.getMessage()));}//设置一个最后需要返回的实体类集合List<FileInfo> resultList = new LinkedList<>();//遍历返回的内容进行处理for (org.springframework.data.elasticsearch.core.SearchHit<FileInfo> searchHit : search.getSearchHits()) {//高亮的内容Map<String, List<String>> highlightFields = searchHit.getHighlightFields();//将高亮的内容填充到content中searchHit.getContent().setFileName(highlightFields.get("fileName") == null ? searchHit.getContent().getFileName() : highlightFields.get("fileName").get(0));searchHit.getContent().setContent(highlightFields.get("content") == null ? searchHit.getContent().getContent() : highlightFields.get("content").get(0));resultList.add(searchHit.getContent());}//手动分页返回信息IPage<FileInfo> warningInfoIPage = new Page<>();warningInfoIPage.setTotal(search.getTotalHits());warningInfoIPage.setRecords(resultList);warningInfoIPage.setCurrent(warningInfoDto.getPageIndex());warningInfoIPage.setSize(warningInfoDto.getPageSize());warningInfoIPage.setPages(warningInfoIPage.getTotal() % warningInfoDto.getPageSize());return warningInfoIPage;}
}

代码测试:
在这里插入图片描述

--请求jason
{"keyword":"全库备份","contentType":["告示"],"pageIndex":1,"pageSize":10
}--响应
{"msg": "操作成功","code": 200,"data": {"records": [{"id": 1306333194,"fileName": "txt测试_20220810153351A001.txt","fileType": "txt","contentType": "告示","content": "•\t秒级快速<span style='color:red'>备份</span>\r\n不论多大的数据量,<span style='color:red'>全库</span><span style='color:red'>备份</span>只需30秒,而且<span style='color:red'>备份过程</span>不会对数据库加锁,对应用程序几乎无影响,全天24小时均可进行<span style='color:red'>备份</span>。","fileUrl": "http://localhost:8092/fileInfo/profile/upload/fileInfo/2022/08/10/txt测试_20220810153351A001.txt","createTime": "2022-08-10T15:33:51.000+08:00","updateTime": "2022-08-10T15:33:51.000+08:00"}],"total": 1,"size": 10,"current": 1,"orders": [],"optimizeCountSql": true,"searchCount": true,"countId": null,"maxLimit": null,"pages": 1}
}

返回的内容将分词检索到匹配的内容,并将匹配的词高亮显示。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/777667.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springdata框架对es集成

什么是spring data框架 Spring Data是一个用于简化数据库、非关系型数据库、索引库访问&#xff0c;并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷&#xff0c;并支持 map-reduce框架和云计算数据服务。Spring Data可以极大的简化JPA(Elasticsearch…)的…

ssh 公私钥(github)

一、生成ssh公私钥 生成自定义名称的SSH公钥和私钥对&#xff0c;需要使用ssh-keygen命令&#xff0c;这是大多数Linux和Unix系统自带的标准工具。下面&#xff0c;简单展示如何使用ssh-keygen命令来生成具有自定义名称的SSH密钥对。 步骤 1: 打开终端 首先&#xff0c;打开我…

对话 Mines of Dalarnia: Web3 游戏创新,社区驱动与公链共建

作者&#xff1a;stellafootprint.network 嘉宾&#xff1a;Manfred Pack&#xff0c;Mines of Dalarnia 游戏开发总监 采访者&#xff1a;Alex Cooper&#xff0c;Footprint Analytics 北美社区与 BD 负责人 在区块链游戏领域&#xff0c;去中心化和玩家经济正在颠覆传统游戏…

泛型,数据结构,集合

文章目录 泛型介绍解决问题好处使用通配符泛型的下限泛型的上限 数据结构定义常见的数据结构栈(先进后出)队列(先进先出)数组结构链表结构哈希表结构 集合List集合特点特有方法子类及其底层数据结构LinkedList集合 Set集合特点没有特有方法子类及其底层数据结构LinkedHashSet集…

vscode使用sftp上传

1.用vscode打开项目 2.安装一下这个sftp 3.使用快捷键 ctrlshiftP 打开指令窗口&#xff0c;输入 sftp:config&#xff0c;选中回车&#xff0c;在当前目录中会自动生成 .vscode 文件夹及 sftp.json 4.修改sftp.json文件配置&#xff0c;改成以下&#xff08;默认的参数可能上传…

AJAX-项目优化(目录、基地址、token、请求拦截器)

目录管理 基地址存储 在utils/request.js配置axios请求基地址 作用&#xff1a;提取公共前缀地址&#xff0c;配置后axios请求时都会baseURLurl 填写API的公共前缀后&#xff0c;将js文件导入到html文件中 <script src"../../utils/request.js"></script&…

AI新工具 又一个开源大模型DBRX击败GPT3.5;根据音频和图像输入生成会说话、唱歌的动态视频

✨ 1: AniPortrait 腾讯开源&#xff1a;根据音频和图像输入生成会说话、唱歌的动态视频 AniPortrait 是个先进的框架&#xff0c;专门用来生成高质量的、由音频和参考肖像图片驱动的动画。如果你有视频&#xff0c;也可以用来实现面部的再现&#xff08;Face reenactment&am…

【氮化镓】位错对氮化镓(GaN)电子能量损失谱(EEL)的影响

本文献《Influence of dislocations on electron energy-loss spectra in gallium nitride》由C. J. Fall等人撰写&#xff0c;发表于2002年。研究团队通过第一性原理计算&#xff0c;探讨了位错对氮化镓&#xff08;GaN&#xff09;电子能量损失谱&#xff08;EEL&#xff09;…

内网穿透_ICMP_icmpsh

目录 一、ICMP协议详解 二、ICMP隧道 (一) 为什么会使用ICMP (二) 实验环境 (三) 操作流程 1. 下载icmpsh 2. 下载并安装依赖 3. 关闭本地icmp响应 4. 攻击机启动服务端开始监听 5. 靶机启动工具客户端 6. 攻击机接受到靶机传来的数据 三、郑重声明 一、ICMP协议详…

C# 反射的使用及场景

1&#xff0c;使用反映将一个对象的同名属性赋值给另一个对象 2, DataTable 转换成一个实体 3&#xff0c;使用反射动态执行方法 4,根据属性信息来执行对应的方法 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using…

Spring用到了哪些设计模式?

目录 Spring 框架中⽤到了哪些设计模式&#xff1f;工厂模式单例模式1.饿汉式&#xff0c;线程安全2.懒汉式&#xff0c;线程不安全3.懒汉式&#xff0c;线程安全4.双重检查锁&#xff08;DCL&#xff0c; 即 double-checked locking&#xff09;5.静态内部类6.枚举单例 代理模…

【论文阅读】UniLog: Automatic Logging via LLM and In-Context Learning

注 由于其公司的保密政策&#xff0c;本文没有公开源代码&#xff0c;数据是公开的。 文章目录 摘要一、介绍二、背景和动机2.1、日志语句生成2.2、大语言模型2.3、上下文学习&#xff08;In-Context Learning&#xff0c;ICL) 三、UNILOG3.1、模型骨干3.2、提示策略3.2.1、提…

3d金属模型灯怎么安装---模大狮模型网

在3D场景中&#xff0c;添加灯光是非常重要的步骤&#xff0c;可以提升场景的氛围和逼真度。特别是对于金属材质的模型&#xff0c;合适的灯光设置能够突出其质感和反射效果。下面将介绍如何在3D软件中安装金属模型灯&#xff0c;并分为以下四个分类进行详细说明。 一、选择合适…

竞赛 python+opencv+深度学习实现二维码识别

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; pythonopencv深度学习实现二维码识别 &#x1f947;学长这里给一个题目综合评分(每项满分5分) 难度系数&#xff1a;3分工作量&#xff1a;3分创新点&#xff1a;3分 该项目较为新颖&…

【InternLM 实战营第二期笔记】书生·浦语大模型全链路开源体系及InternLM2技术报告笔记

大模型 大模型成为发展通用人工智能的重要途径 专用模型&#xff1a;针对特定任务&#xff0c;一个模型解决一个问题 通用大模型&#xff1a;一个模型应对多种任务、多种模态 书生浦语大模型开源历程 2023.6.7&#xff1a;InternLM千亿参数语言大模型发布 2023.7.6&#…

视觉信息处理与FPGA实现第八次作业——verilog实现对比度调节

一、查看灰度图的数据格式 2.1 安装HxD HxD下载链接&#xff1a;https://download.csdn.net/download/weixin_44357071/89045331 解压直接打开exe就能使用。 将需要查看二进制数据的图片拖到软件框里就能读取 2.2 找到bmp图像的图片点阵数据起始地址&#xff0c;原理和例子…

R语言批量计算t检验,输出pvalue和均值

1.输入数据如下&#xff1a; 2.代码如下 setwd("E:/R/Rscripts/rG4相关绘图") # 读取CSV文件 data <- read.csv("box-cds-ABD-不同类型rg4-2.csv", stringsAsFactors FALSE)# 筛选出Type2列为指定五种类型的数据 filtered_data <- subset(data, …

使用Kaggle API快速下载Kaggle数据集

前言 在使用Kaggle网站下载数据集时&#xff0c;直接在网页上点击下载可能会很慢&#xff0c;甚至会出现下载失败的情况。本文将介绍如何使用Kaggle API快速下载数据集。 具体步骤 安装Kaggle API包 在终端中输入以下命令来安装Kaggle API相关的包&#xff1a; pip install…

elementplus-vue-审核按钮-对话框(Dialog )

效果图&#xff1a; 代码&#xff1a; <template> <el-button type"success" click"dialogVisible true" :icon"Edit">审核</el-button> <el-dialog v-model"dialogVisible" title"是否通过" width&q…

公链角逐中突围,Solana 何以成为 Web3 世界的流量焦点?

在众多区块链公链中&#xff0c;Solana 凭借其创纪录的处理速度和极低的交易费用&#xff0c;成为了众多开发者和投资者的宠儿。就像网络上流行的那句话所说&#xff1a;“Why slow, when you can Solana?”&#xff0c;Solana 正以它的速度和强大的生态系统&#xff0c;重新定…