网站开发学那种语言,网站规划与设计一千字,上海网络营销培训,建e室内设计网官网平面图1.什么是ElasticSearch分布式搜索引擎#xff1f;
Elasticsearch是一个开源的分布式搜索引擎#xff0c;提供实时的、高可用性的搜索和分析解决方案。它支持快速索引和搜索大规模数据#xff0c;具有分布式架构、RESTful API、基于JSON的查询语言等功能#xff0c;适用于各…1.什么是ElasticSearch分布式搜索引擎
Elasticsearch是一个开源的分布式搜索引擎提供实时的、高可用性的搜索和分析解决方案。它支持快速索引和搜索大规模数据具有分布式架构、RESTful API、基于JSON的查询语言等功能适用于各种应用场景如全文搜索。
比如说现在我拥有一个电子商务网站然后我需要一个搜索引擎来让用户能够快速地搜索和找到他们感兴趣的商品。这时候我就可以使用Elasticsearch作为你的分布式搜索引擎。
假设现在我的电子商务网站有几百万个商品并且我希望用户可以根据商品的名称、描述、价格、品牌等信息进行搜索。
首先我会使用Elasticsearch的API将商品数据索引到Elasticsearch集群中。每个商品将被表示为一个文档包含属性如商品名称、描述、价格等。
当用户在我的网站上进行搜索时我就可以使用Elasticsearch的搜索API发送一个搜索请求。比如用户搜索关键词手机。
Elasticsearch将会在索引中查找包含关键词手机的所有文档并返回匹配的结果。 2.项目中的搜索功能实现 在Dao层中新建Elasticsearch文件添加Elasticsearch接口用于连接数据库只需要继承接口即可。
Repository
public interface DiscussPostRepository extends ElasticsearchRepositoryDiscussPost, Integer {}
上面的代码其实是用Elasticsearch创建一个帖子的仓库接口以便在应用程序中访问和操作Elasticsearch中的帖子数据。这个相对而言比较简单。 service层写一个ElasticsearchService文件。
Service
public class ElasticsearchService {Autowiredprivate DiscussPostRepository discussRepository;Autowiredprivate ElasticsearchTemplate elasticTemplate;public void saveDiscussPost(DiscussPost post) {discussRepository.save(post);}public void deleteDiscussPost(int id) {discussRepository.deleteById(id);}public PageDiscussPost searchDiscussPost(String keyword, int current, int limit) {SearchQuery searchQuery new NativeSearchQueryBuilder().withQuery(QueryBuilders.multiMatchQuery(keyword, title, content)).withSort(SortBuilders.fieldSort(type).order(SortOrder.DESC)).withSort(SortBuilders.fieldSort(score).order(SortOrder.DESC)).withSort(SortBuilders.fieldSort(createTime).order(SortOrder.DESC)).withPageable(PageRequest.of(current, limit)).withHighlightFields(new HighlightBuilder.Field(title).preTags(em).postTags(/em),new HighlightBuilder.Field(content).preTags(em).postTags(/em)).build();return elasticTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() {Overridepublic T AggregatedPageT mapResults(SearchResponse response, ClassT aClass, Pageable pageable) {SearchHits hits response.getHits();if (hits.getTotalHits() 0) {return null;}ListDiscussPost list new ArrayList();for (SearchHit hit : hits) {DiscussPost post new DiscussPost();String id hit.getSourceAsMap().get(id).toString();post.setId(Integer.valueOf(id));String userId hit.getSourceAsMap().get(userId).toString();post.setUserId(Integer.valueOf(userId));String title hit.getSourceAsMap().get(title).toString();post.setTitle(title);String content hit.getSourceAsMap().get(content).toString();post.setContent(content);String status hit.getSourceAsMap().get(status).toString();post.setStatus(Integer.valueOf(status));String createTime hit.getSourceAsMap().get(createTime).toString();post.setCreateTime(new Date(Long.valueOf(createTime)));String commentCount hit.getSourceAsMap().get(commentCount).toString();post.setCommentCount(Integer.valueOf(commentCount));// 处理高亮显示的结果HighlightField titleField hit.getHighlightFields().get(title);if (titleField ! null) {post.setTitle(titleField.getFragments()[0].toString());}HighlightField contentField hit.getHighlightFields().get(content);if (contentField ! null) {post.setContent(contentField.getFragments()[0].toString());}list.add(post);}return new AggregatedPageImpl(list, pageable,hits.getTotalHits(), response.getAggregations(), response.getScrollId(), hits.getMaxScore());}});}}
上述代码展示了一个名为 ElasticsearchService 的服务类它提供了以下功能 saveDiscussPost 方法用于将帖子数据保存到 Elasticsearch 中。它接收一个 DiscussPost 对象作为参数并调用 discussRepository 的 save 方法将数据保存到 Elasticsearch 中。 deleteDiscussPost 方法用于从 Elasticsearch 中删除指定 ID 的数据。它接收一个整数类型的 ID 参数并调用 discussRepository 的 deleteById 方法来删除数据。 searchDiscussPost 方法用于在 Elasticsearch 中搜索帖子数据。它接收关键字、当前页码和每页显示数量作为参数并执行一个复杂的搜索查询。将查询结果封装成一个 PageDiscussPost 对象返回。
上面说了很多其实你只要知道这段代码作用就是这样的ElasticsearchService 提供了对 Elasticsearch 中讨论帖子数据的保存、删除和搜索功能。它充分利用了 Spring Data Elasticsearch 和 ElasticsearchTemplate 提供的功能和方法简化了与 Elasticsearch 的交互和操作。该服务类可以被其他组件或业务逻辑调用以实现对 Elasticsearch 数据的管理和检索。 在Controller层写一个SearchController文件。
Controller
public class SearchController implements CommunityConstant {Autowiredprivate ElasticsearchService elasticsearchService;Autowiredprivate UserService userService;Autowiredprivate LikeService likeService;// search?keywordxxxRequestMapping(path /search, method RequestMethod.GET)public String search(String keyword, Page page, Model model) {// 搜索帖子org.springframework.data.domain.PageDiscussPost searchResult elasticsearchService.searchDiscussPost(keyword, page.getCurrent() - 1, page.getLimit());// 聚合数据ListMapString, Object discussPosts new ArrayList();if (searchResult ! null) {for (DiscussPost post : searchResult) {MapString, Object map new HashMap();// 帖子map.put(post, post);// 作者map.put(user, userService.findUserById(post.getUserId()));// 点赞数量map.put(likeCount, likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId()));discussPosts.add(map);}}model.addAttribute(discussPosts, discussPosts);model.addAttribute(keyword, keyword);// 分页信息page.setPath(/search?keyword keyword);page.setRows(searchResult null ? 0 : (int) searchResult.getTotalElements());return /site/search;}}
这段代码的作用是展示了一个名为 SearchController 的控制器类用于处理搜索功能的请求。 接收用户输入的关键字 keyword、分页信息 page 和模型对象 model。 调用 elasticsearchService 的 searchDiscussPost 方法传递关键字、当前页码和每页显示数量以执行搜索操作。 将搜索结果 searchResult 中的每个帖子以及帖子的作者和点赞数量等信息存储在 discussPosts 列表中的 Map 对象中。 将 discussPosts、关键字 keyword 和分页信息 page 添加到模型对象 model 中以便在视图中进行展示。
看了这么多反正你要知道的就是SearchController 控制器类负责接收用户的搜索请求调用 ElasticsearchService 执行搜索操作获取搜索结果并将结果和相关信息传递给视图进行展示。通过这个控制器类用户可以在前端页面输入关键字进行搜索然后获取搜索结果并在页面上显示。
文章转载自: http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn