Elasticsearch應用與代碼示例技術文章大綱
一、引言
- Elasticsearch在現代化應用中的核心作用
- 典型應用場景分析(日志分析/全文檢索/數據聚合)
二、環境準備(前提條件)
- Elasticsearch 8.x集群部署要點
- IK中文分詞插件配置指南
- Ingest Attachment插件安裝說明
三、核心代碼結構解析
src/main/
├── java/
│ ├── config/
│ │ └── ElasticSearchConfig.java
│ ├── controller/
│ │ └── ElasticSearchController.java
│ ├── service/
│ │ ├── ElasticSearchService.java
│ │ └── ElasticSearchServiceImpl.java
│ ├── model/
│ │ ├── FileData.java
│ │ ├── Attachment.java
│ │ └── SearchResult.java
│ ├── dto/
│ │ └── WarningInfoDto.java
│ └── util/
│ └── ElasticSearchUtils.java
四、核心組件實現(含代碼示例)
1. 配置中心(ElasticSearchConfig.java)
application.properties or yml
es.uri=192.168.1.1
es.port=9200
es.username=""
es.password=""
package com.zbxsoft.wds.config;import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.alibaba.fastjson.parser.deserializer.JSONPDeserializer;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** es配置*/
@Configuration
@ConfigurationProperties(prefix = "es")
public class ElasticSearchConfig{public String getUri() {return uri;}public void setUri(String uri) {this.uri = uri;}public Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}private String uri;private Integer port;private String password;private String username;@Beanpublic ElasticsearchClient elasticsearchClient(@Autowired(required = false) JsonpMapper jsonpMapper, @Autowired(required = false) ObjectMapper objectMapper) {// 解析hostlist配置信息// 創建HttpHost數組,其中存放es主機和端口的配置信息HttpHost[] httpHostArray = new HttpHost[1];httpHostArray[0] = new HttpHost(uri,Integer.valueOf(port));RestClientBuilder builder = RestClient.builder(httpHostArray);final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));RestClient client = RestClient.builder(new HttpHost(uri,Integer.valueOf(port))).build();ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());return new ElasticsearchClient(transport);}RestClient restClient = builder.build();ElasticsearchTransport transport = null;if (jsonpMapper != null) {transport = new RestClientTransport(restClient, jsonpMapper);} else if (objectMapper != null) {transport = new RestClientTransport(restClient, new JacksonJsonpMapper(objectMapper));} else {transport = new RestClientTransport(restClient, new JacksonJsonpMapper());}// Create the transport with a Jackson mapper// And create the API clientreturn new ElasticsearchClient(transport);}
}
2. 數據處理模型(FileData.java)
package com.zbxsoft.wds.config;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.zbxsoft.wds.fileupload.Attachment;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;import java.io.Serializable;
import java.util.Objects;@Data
@Document(indexName = "file_data", createIndex = false)
@JsonIgnoreProperties(ignoreUnknown = true)
public class FileData implements Serializable {@Field(name = "file_id",type = FieldType.Text)private String file_id;@Field(name = "file_type",type = FieldType.Text)private String file_type;@Field(name = "file_name",type = FieldType.Text)private String file_name;@Field(name = "file_url",type = FieldType.Text)private String file_url;@Field(name = "file_size",type = FieldType.Text)private String file_size;@Field(name = "group_file_id",type = FieldType.Text)private String group_file_id;@Field(name = "file_suffix",type = FieldType.Text)private String file_suffix;@Field(name = "file_dir_name",type = FieldType.Text)private String file_dir_name;//保存時使用@Field(name = "attachment.content",type = FieldType.Text)private String content;//檢索時使用private Attachment attachment;}}
3. 附件解析組件(Attachment.java)
package com.zbxsoft.wds.fileupload;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;import java.io.Serializable;@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Attachment implements Serializable {private String date;private String content_type;private String author;private String language;private String title;private String content;private String content_length;
}}
4. 服務層實現(ElasticSearchService.java)
package com.zbxsoft.wds.fileupload;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zbxsoft.wds.config.FileData;
import org.springframework.http.HttpEntity;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.util.List;
import java.util.Map;public interface ElasticsearchService {HttpEntity<?> createFileIndex(MultipartFile file) throws IOException;String createFileIndex(FileData fileData) throws IOException;Map<String,Double> queryWord(String keyword) throws IOException;IPage<SearchResult> queryWord(WarningInfoDto warningInfoDto) throws IOException;List<String> getAssociationalWordOther(WarningInfoDto warningInfoDto);String updateFileIndex(String id, FileData fileData) throws IOException;
}
工具類(ElasticSearchUtils.java)
package com.zbxsoft.wds.config;import cn.hutool.extra.spring.SpringUtil;import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.mapping.DateProperty;
import co.elastic.clients.elasticsearch._types.mapping.Property;
import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.elasticsearch.core.search.TotalHitsRelation;
import co.elastic.clients.elasticsearch.core.search.TrackHits;
import co.elastic.clients.elasticsearch.indices.*;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;import java.io.IOException;
import java.io.StringReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;@Slf4j
public class ElasticSearchUtils<T> {public static ElasticsearchClient elasticsearchClient;public String index;public Class obj;public String idField;public Set<String> fields;static {ElasticSearchConfig elasticSearchConfig = SpringUtil.getBean(ElasticSearchConfig.class);
// elasticsearchClient = elasticSearchConfig.elasticsearchClient();}/*** 獲取id字段** @return*/private String getIdValue(T t) throws Exception {Field field = t.getClass().getDeclaredField(idField);field.setAccessible(true);Object object = field.get(t);return object.toString();}/*** 判斷索引是否存在** @return*/public Boolean getIndex() {try {GetIndexRequest getIndexRequest = GetIndexRequest.of(builder -> builder.index(index));GetIndexResponse getIndexResponse = elasticsearchClient.indices().get(getIndexRequest);log.info("getIndexResponse:{}", getIndexResponse);return true;} catch (IndexOutOfBoundsException | IOException e) {log.info("getIndexResponse:{}", e.getMessage());return false;}}public void deleteIndex() throws IOException {//1.創建索引請求DeleteIndexRequest request = DeleteIndexRequest.of(builder -> builder.index(index));//2.執行創建請求DeleteIndexResponse delete = elasticsearchClient.indices().delete(request);//如果為true就刪除了log.info("DeleteIndexRequest:{}", delete);}/*** 插入數據** @throws IOException*/public void push(T t) throws Exception {String id = getIdValue(t);IndexRequest<T> indexRequest = IndexRequest.of(b -> b.index(index).id(id).document(t)//刷新可以立刻搜索到,消耗性能/*.refresh(Refresh.True)*/);elasticsearchClient.index(indexRequest);}/*** 索引信息查詢** @throws IOException*/public T query(String id) throws IOException {GetResponse<T> response = elasticsearchClient.get(g -> g.index(index).id(id), obj);if (response.found()) {return response.source();}return null;}/*** 索引信息查詢** @throws IOException*/public HitsMetadata<T> queryList(int page, int pageSize, Query query, SortOptions... sortOptions) throws IOException {SearchResponse<T> search = elasticsearchClient.search(s -> s.index(index).query(query).trackTotalHits(TrackHits.of(i -> i.enabled(true))).sort(Arrays.asList(sortOptions)).from((page - 1) * pageSize).size(pageSize), obj);return search.hits();}/*** 刪除文檔** @throws IOException*/public boolean del(String id) throws IOException {DeleteResponse delete = elasticsearchClient.delete(d -> d.index(index).id(id));Result result = delete.result();return "deleted".equals(result.jsonValue()) | "not_found".equals(result.jsonValue());}/*** 批量** @throws IOException*/public Map<String, String> batchDel(Set<String> ids) throws Exception {BulkRequest.Builder br = new BulkRequest.Builder();for (String id : ids) {br.operations(op -> op.delete(d -> d.index(index).id(id)));}return requestBulk(br);}/*** 批量** @throws IOException*/public Map<String, String> batchAdd(List<T> list) throws Exception {BulkRequest.Builder br = new BulkRequest.Builder();for (T t : list) {String idValue = getIdValue(t);br.operations(op -> op.index(idx -> idx.index(index).id(idValue).document(t)));}return requestBulk(br);}/*** 處理批量請求** @param br* @return* @throws IOException*/private Map<String, String> requestBulk(BulkRequest.Builder br) throws IOException {//刷新可以立刻搜索到,消耗性能//br.refresh(Refresh.True);BulkResponse result = elasticsearchClient.bulk(br.build());System.out.println(result);Map<String, String> returnResult = new HashMap<>();if (result.errors()) {returnResult = result.items().stream().filter(e -> e.error() != null).collect(Collectors.toMap(BulkResponseItem::id, b -> b.error().reason()));}return returnResult;}/*** 分組* @param map* @return* @throws IOException*/public Map<String, Aggregate> buildAggregate(Map<String, Aggregation> map) throws IOException {SearchResponse<T> search = elasticsearchClient.search(s -> s.index(index).size(0).aggregations(map), obj);return search.aggregations();}public Long queryCount(Query query) throws IOException {CountResponse search = elasticsearchClient.count(s -> s.index(index).query(query));return search.count();}}
服務層實現(ElasticSearchServiceImpl.java)
package com.zbxsoft.wds.fileupload;import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.*;import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zbxsoft.wds.aneo4j.TextAnalysis;
import com.zbxsoft.wds.aneo4j.rep.CustomNeo4jRepository;
import com.zbxsoft.wds.filequery.FileQuery;
import com.zbxsoft.wds.mapper.FileQueryMapper;
import com.zbxsoft.wds.config.FileData;
import com.zbxsoft.xwk.vo.R;
import lombok.extern.slf4j.Slf4j;import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.*;@Service
@Slf4j
public class ElasticsearchServiceImpl implements ElasticsearchService {@Autowiredprivate ElasticsearchClient client;@Autowiredprivate ResourceLoader resourceLoader;@Autowiredprotected FileQueryMapper fileQueryMapper;@Value("${file.server.address}")private String fileServerAddress;@Autowiredprivate CustomNeo4jRepository customNeo4jRepository;/*** 創建索引* @param fileData 文件數據* @return 索引id* @throws IOException exp*/public String createFileIndex(FileData fileData) throws IOException {String path = fileData.getFile_url();String url = fileServerAddress + path;log.info("已上傳文件地址:" + url);String fileName = FileUtil.getName(url);// 上傳并返回新文件名稱String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);File file = File.createTempFile(fileName, prefix);IOUtils.copy(new URL(url),file);try (PDDocument document = PDDocument.load(file)) {//提取pdf中的文本內容PDFTextStripper pdfTextStripper = new PDFTextStripper();String text = pdfTextStripper.getText(document);//該主鍵 在es dm 中用作數據唯一id, 在neo4j中作為實體的typeString id = fileData.getFile_id();//提取文章中的關鍵詞List<String> keywords = TextAnalysis.getKeywords(text);if (CollectionUtil.isNotEmpty(keywords)) {//構建知識圖譜customNeo4jRepository.buildArticleKeywordGraph(id, keywords);//更新keywords字段 file_id 就是數據庫中存在的主鍵updateKeywords(id, String.join(",", keywords));}} catch (IOException e) {log.error("提取文本或創建關鍵詞圖譜失敗,異常信息: {}", e.toString());}//最后建立該文章的索引String _docId = createIndex(file,fileData);return _docId;}/*** 更新文件對象的keyword* @param id 主鍵* @param keyword 關鍵詞*/private void updateKeywords(String id,String keyword){LambdaUpdateWrapper<FileQuery> updateWrapper = new LambdaUpdateWrapper<>();updateWrapper.eq(FileQuery::getFileQueryId,id).set(FileQuery::getKeyword,keyword);fileQueryMapper.update(updateWrapper);}/*** 創建索引* @param file 文件* @param fileData 文件對象* @return 索引id*/private String createIndex(File file, FileData fileData) {try {byte[] bytes = getContent(file);String base64 = Base64.getEncoder().encodeToString(bytes);fileData.setContent(base64);IndexRequest<Object> requestData = IndexRequest.of(i -> i.index("file_data").pipeline("attachment").id(fileData.getFile_id()).document(fileData));IndexResponse indexResponse = client.index(requestData);log.info("indexResponse:" + indexResponse);return indexResponse.id();} catch (IOException e) {log.error("文件上傳異常,異常信息: {}", e.toString());return "";}}/*** 更新索引,在更新文件的時候發生* @param id 索引id 也是關系庫主鍵* @param fileData 文件對象* @return* @throws IOException*/@Overridepublic String updateFileIndex(String id, FileData fileData) throws IOException {//直接刪除索引, 重建DeleteRequest deleteRequest = DeleteRequest.of(s -> s.index("file_data").id(id));client.delete(deleteRequest);String _docId = createFileIndex(fileData);return _docId;}/*** 文件轉base64** @param file 文件對象* @return buffer* @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());}return buffer;}/*** 給定keyword 檢索數據* @param keyword* @return* @throws IOException*/public Map<String,Double> queryWord(String keyword) throws IOException {SearchResponse<FileData> response = doQuery(keyword);log.info("檢索結果: {}", response.hits().hits());List<Pair<String,Double>> idList = Lists.newArrayList();Map<String,Double> idsWithScore = Maps.newHashMap();List<Hit<FileData>> hits = response.hits().hits();for (Hit<FileData> hit : hits) {Double score = hit.score();String id = hit.id();score = score == null ? 0.0d : score;idsWithScore.put(id,score);}log.info("查詢數據: {}", idsWithScore);return idsWithScore;}/*** es attachment.content 檢索封裝* @param keyword 關鍵詞* @return 檢索結果* @throws IOException exp*/private SearchResponse<FileData> doQuery(String keyword) throws IOException {Map<String, HighlightField> map = new HashMap<>();HighlightField build = new HighlightField.Builder().preTags("").postTags("").build();map.put("file_name",build);map.put("attachment.content",HighlightField.of(hf -> hf.preTags("").postTags("").numberOfFragments(4)));Highlight highlight = Highlight.of(h -> h.type(HighlighterType.of(ht -> ht.builtin(BuiltinHighlighterType.Unified))).fields(map).fragmentSize(50).numberOfFragments(5));//索引 file_name 分詞器 為ik_max_word 顆粒度較細 而attachment.content 使用ik_smart分詞器 , 顆粒度相對粗一點SearchResponse<FileData> response = client.search(s -> s.index("file_data").highlight(highlight).query(q -> q.bool(b -> b.should(sh -> sh.match(t -> t.field("file_name").query(keyword))).should(sh -> sh.match(t -> t.field("attachment.content").query(keyword))))),FileData.class);return response;}/*** 高亮分詞搜索其它類型文檔** @param warningInfoDto* @return*/public IPage<SearchResult> queryWord(WarningInfoDto warningInfoDto) throws IOException {//分頁SearchResponse<FileData> response = doQuery(warningInfoDto.getKeyword());//手動創建分頁對象IPage<SearchResult> warningInfoIPage = getFileDataIPage(warningInfoDto, response);return warningInfoIPage;}/*** 獲取檢索數據, 分頁* @param warningInfoDto* @param response* @return*/@NotNullprivate static IPage<SearchResult> getFileDataIPage(WarningInfoDto warningInfoDto, SearchResponse<FileData> response) {List<Hit<FileData>> hits = response.hits().hits();TotalHits total = response.hits().total();List<SearchResult> resultList = new LinkedList<>();//處理返回內容for (Hit<FileData> hit : hits) {Map<String, List<String>> map = hit.highlight();List<String> highLightWords = Lists.newArrayList();map.forEach((k,v) -> highLightWords.addAll(v));FileData fileData = hit.source();SearchResult searchResult = new SearchResult(fileData,highLightWords);resultList.add(searchResult);}// //設置一個最后需要返回的實體類集合//手動分頁返回信息IPage<SearchResult> warningInfoIPage = new Page<>();assert total != null;warningInfoIPage.setTotal(total.value());warningInfoIPage.setRecords(resultList);warningInfoIPage.setCurrent(warningInfoDto.getPageIndex());warningInfoIPage.setSize(warningInfoDto.getPageSize());warningInfoIPage.setPages(warningInfoIPage.getTotal() % warningInfoDto.getPageSize());return warningInfoIPage;}/*** 文檔信息關鍵詞聯想(根據輸入框的詞語聯想文件名稱)** @param warningInfoDto* @return*/public List<String> getAssociationalWordOther(WarningInfoDto warningInfoDto) {
// //需要查詢的字段
// 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<FileData> search = null;
// try {
// search = client.search(searchQuery);
// } 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 new ArrayList<>();}/*** 上傳文件并進行文件內容識別上傳到es 測試使用* @param file* @return*/public HttpEntity<?> createFileIndex(MultipartFile file) throws IOException {//http://192.168.100.243//file/wds/fileQuery/2024-12/1734319529179/downloadFile.pdf
// InputStream inputStream = resourceLoader.getResource(url).getInputStream();String fileName = file.getName();String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);File tempFile = File.createTempFile(fileName, prefix);// 上傳文件路徑file.transferTo(tempFile);FileData fileData = new FileData();fileData.setFile_url("http://localhost:9000/bucket/p2");fileData.setFile_id(UUID.fastUUID().toString(true));fileData.setFile_name("張飛吃豆芽333.pdf");fileData.setFile_suffix(".pdf");fileData.setFile_type("pdf");fileData.setFile_size("44");fileData.setFile_dir_name("p2");fileData.setGroup_file_id("g1");String _docId = createIndex(tempFile,fileData);return StringUtils.isBlank(_docId) ? R.badRequest("文件上傳異常") : R.success("文件上傳成功");}}
五、數據傳輸
1. 前端請求數據傳輸(WarningInfoDto.java)
package com.zbxsoft.wds.fileupload;import lombok.Data;import java.util.List;/*** 前端請求數據傳輸* WarningInfo* @author luoY*/
@Datapublic class WarningInfoDto{/*** 頁數*/private Integer pageIndex;/*** 每頁數量*/private Integer pageSize;/*** 查詢關鍵詞*/private String keyword;/*** 內容類型*/private List<String> contentType;/*** 用戶手機號*/private String phone;
}
}
2. 文檔全文檢索接口(ElasticSearchController.java)
package com.zbxsoft.wds.fileupload;import com.baomidou.mybatisplus.core.metadata.IPage;import com.zbxsoft.wds.common.R;import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;/*** es搜索引擎** @author luoy*/
@RestController
@RequestMapping("es")
public class ElasticsearchController {@Resourceprivate ElasticsearchService elasticsearchService;@PostMapping("/uploadFile")public HttpEntity<?> uploadFile(@RequestParam(required = false) MultipartFile file) throws IOException {return elasticsearchService.createFileIndex(file);}/*** 告警信息關鍵詞聯想** @param warningInfoDto* @return*/@PostMapping("getAssociationalWordDoc")public HttpEntity<?> getAssociationalWordDoc(@RequestBody WarningInfoDto warningInfoDto, HttpServletRequest request) {List<String> words = elasticsearchService.getAssociationalWordOther(warningInfoDto);return R.list(words);}/*** 告警信息高亮分詞分頁查詢** @param warningInfoDto* @return*/@PostMapping("queryHighLightWordDoc")public HttpEntity<?> queryHighLightWordDoc(@RequestBody WarningInfoDto warningInfoDto,HttpServletRequest request) throws IOException {IPage<SearchResult> warningInfoListPage = elasticsearchService.queryWord(warningInfoDto);return R.entity(warningInfoListPage);}
}
3. 數據返回(SearchResult.java)
package com.zbxsoft.wds.fileupload;import com.zbxsoft.wds.config.FileData;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;import java.io.Serializable;
import java.util.List;@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SearchResult implements Serializable {private FileData fileData;private List<String> highLightWords;
}
六、高級功能實現
- 多條件復合查詢構建技巧
- 聚合統計實現方案
- 搜索高亮顯示配置
七、性能優化建議
- 索引分片策略設計
- 查詢DSL優化技巧
- 批量操作最佳實踐
八、總結與展望
- 當前架構優勢分析
- 后續演進方向建議
(注:各代碼示例需配合完整的類定義和import語句,實際開發中需補充異常處理、日志記錄等生產級代碼要素)