苏州h5网站建设价钱搜素引擎排名优化

pingmian/2026/1/20 5:35:41/文章来源:
苏州h5网站建设价钱,搜素引擎排名优化,团购机票网站建设,楼市最新消息价格目录 一.安装elasticsearch 1.拉取镜像 2.创建存放数据及配置文件的文件夹#xff0c;启动时挂载。 4.修改文件夹权限 5.启动容器 5.1参数解释 6.安装ik分词器 6.2测试一下Ik分词器 二.添加文章索引库 1查询所有的文章信息#xff0c;批量导入到es索引库中 2)测试 …目录 一.安装elasticsearch 1.拉取镜像 2.创建存放数据及配置文件的文件夹启动时挂载。 4.修改文件夹权限 5.启动容器 5.1参数解释 6.安装ik分词器 6.2测试一下Ik分词器 二.添加文章索引库 1查询所有的文章信息批量导入到es索引库中 2)测试 三.文章搜索功能实现 1导入 heima-leadnews-search 2在heima-leadnews-service的pom中添加依赖   3nacos配置中心leadnews-search 2搜索接口定义 3业务层实现 四.文章自动审核构建索引 文章微服务发送消息 1.把SearchArticleVo放到model工程下   2.文章微服务的ArticleFreemarkerService中的buildArticleToMinIO方法中收集数据并发送消息 3. 在ArticleConstants类中添加新的常量完整代码如下 4.文章微服务集成kafka发送消息 搜索微服务接收消息并创建索引 5.定义监听接收消息,保存索引数据 五.保存用户搜索记录 1.MongoDB安装及集成 2.保存搜索记录 3.用户搜索记录对应的集合对应实体类   4.实现步骤 4.1pom依赖   4.2nacos配置 4.3.创建ApUserSearchService新增insert方法 4.4参考自媒体相关微服务在搜索微服务中获取当前登录的用户 4.5.在ArticleSearchService的search方法中调用保存历史记录 4.6.保存历史记录中开启异步调用添加注解Async 4.7在搜索微服务引导类上开启异步调用 4.8测试搜索后查看结果   六.查询用户搜素记录​编辑 控制器 七.删除用户搜索记录 八.联想词搜索 搜索词-数据来源 功能实现 一.安装elasticsearch 1.拉取镜像 docker pull elasticsearch:7.7.02.创建存放数据及配置文件的文件夹启动时挂载。 mkdir -p /home/elasticsearch/data/ mkdir -p /home/elasticsearch/config/3.编写配置文件 echo http.host: 0.0.0.0 http.cors.enabled: true http.cors.allow-origin: * /home/elasticsearch/config/elasticsearch.yml4.修改文件夹权限 chmod -R 777 /home/elasticsearch/ ls -l # 查看文件权限5.启动容器 docker run --name elasticsearch -p 9200:9200 \-p 9300:9300 \-e discovery.typesingle-node \-e ES_JAVA_OPTS-Xms64m -Xmx128m \-v /home/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \-v /home/elasticsearch/data:/usr/share/elasticsearch/data \-v /home/elasticsearch/plugins:/usr/share/elasticsearch/plugins \-d elasticsearch:7.7.05.1参数解释 --name elasticsearch将容器命名为 elasticsearch -p 9200:9200将容器的9200端口映射到宿主机9200端口 -p 9300:9300将容器的9300端口映射到宿主机9300端口目的是集群互相通信 -e discovery.typesingle-node单例模式 -e ES_JAVA_OPTS-Xms64m -Xmx128m配置内存大小 -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml将配置文件挂载到宿主机 -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data将数据文件夹挂载到宿主机 -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins将插件目录挂载到宿主机(需重启) -d elasticsearch:7.7.0后台运行容器并返回容器ID6.安装ik分词器 下载ik-7.70版本两个版本要一致 上传到插件挂在目录下面 //进入plugins目录 cd /home/elasticsearch/plugins //新建一个名字叫 ik 的文件夹 mkdir ik //将下载的ik分词器插件压缩包上传到ik文件夹下然后解压zip到当前目录 unzip elasticsearch-analysis-ik-7.8.0.zip //最后给一下ik文件夹的权限 chmod -R 777 /home/elasticsearch/plugins/ik //安装好后检查下是否安装成功了 使用docker exec -it elasticsearch /bin/bash 命令 进入容器内部 进入容器的 cd /usr/share/elasticsearch/bin 目录 执行 elasticsearch-plugin list 命令(列出es安装的所有插件) //如果列出了 ik 就说明es的ik中文分词器安装成功了 //装好ik分词器后记得重启ES容器 docker restart elasticsearch6.2测试一下Ik分词器 可以看到ik分词器成功的安装好了! 二.添加文章索引库 使用postman添加映射 put请求 http://192.168.200.130:9200/app_info_article {mappings:{properties:{id:{type:long},publishTime:{type:date},layout:{type:integer},images:{type:keyword,index: false},staticUrl:{type:keyword,index: false},authorId: {type: long},authorName: {type: text},title:{type:text,analyzer:ik_smart},content:{type:text,analyzer:ik_smart}}} } 1查询所有的文章信息批量导入到es索引库中 package com.heima.es;import com.alibaba.fastjson.JSON; import com.heima.es.mapper.ApArticleMapper; import com.heima.es.pojo.SearchArticleVo; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.util.List;SpringBootTest RunWith(SpringRunner.class) public class ApArticleTest {Autowiredprivate ApArticleMapper apArticleMapper;Autowiredprivate RestHighLevelClient restHighLevelClient;/*** 注意数据量的导入如果数据量过大需要分页导入* throws Exception*/Testpublic void init() throws Exception {//1.查询所有符合条件的文章数据ListSearchArticleVo searchArticleVos apArticleMapper.loadArticleList();//2.批量导入到es索引库BulkRequest bulkRequest new BulkRequest(app_info_article);for (SearchArticleVo searchArticleVo : searchArticleVos) {IndexRequest indexRequest new IndexRequest().id(searchArticleVo.getId().toString()).source(JSON.toJSONString(searchArticleVo), XContentType.JSON);//批量添加数据bulkRequest.add(indexRequest);}restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);}} 2)测试 postman查询所有的es中数据 GET请求 http://192.168.200.130:9200/app_info_article/_search 三.文章搜索功能实现 1导入 heima-leadnews-search 2在heima-leadnews-service的pom中添加依赖   !--elasticsearch-- dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.4.0/version /dependency dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-client/artifactIdversion7.4.0/version /dependency dependencygroupIdorg.elasticsearch/groupIdartifactIdelasticsearch/artifactIdversion7.4.0/version /dependency 3nacos配置中心leadnews-search spring:autoconfigure:exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration elasticsearch:host: 192.168.200.130port: 9200 2搜索接口定义 package com.heima.search.controller.v1;import com.heima.model.common.dtos.ResponseResult; import com.heima.model.search.dtos.UserSearchDto; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.io.IOException;RestController RequestMapping(/api/v1/article/search) public class ArticleSearchController {PostMapping(/search)public ResponseResult search(RequestBody UserSearchDto dto) throws IOException {return null;} }UserSearchDto package com.heima.model.search.dtos;import lombok.Data;import java.util.Date;Data public class UserSearchDto {/*** 搜索关键字*/String searchWords;/*** 当前页*/int pageNum;/*** 分页条数*/int pageSize;/*** 最小时间*/Date minBehotTime;public int getFromIndex(){if(this.pageNum1)return 0;if(this.pageSize1) this.pageSize 10;return this.pageSize * (pageNum-1);} } 3业务层实现 创建业务层接口ApArticleSearchService package com.heima.search.service;import com.heima.model.search.dtos.UserSearchDto; import com.heima.model.common.dtos.ResponseResult;import java.io.IOException;public interface ArticleSearchService {/**ES文章分页搜索return*/ResponseResult search(UserSearchDto userSearchDto) throws IOException; } 实现类 package com.heima.search.service.impl;import com.alibaba.fastjson.JSON; import com.heima.model.common.dtos.ResponseResult; import com.heima.model.common.enums.AppHttpCodeEnum; import com.heima.model.search.dtos.UserSearchDto; import com.heima.model.user.pojos.ApUser; import com.heima.search.service.ArticleSearchService; import com.heima.utils.thread.AppThreadLocalUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.query.*; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map;Service Slf4j public class ArticleSearchServiceImpl implements ArticleSearchService {Autowiredprivate RestHighLevelClient restHighLevelClient;/*** es文章分页检索** param dto* return*/Overridepublic ResponseResult search(UserSearchDto dto) throws IOException {//1.检查参数if(dto null || StringUtils.isBlank(dto.getSearchWords())){return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);}//2.设置查询条件SearchRequest searchRequest new SearchRequest(app_info_article);SearchSourceBuilder searchSourceBuilder new SearchSourceBuilder();//布尔查询BoolQueryBuilder boolQueryBuilder QueryBuilders.boolQuery();//关键字的分词之后查询QueryStringQueryBuilder queryStringQueryBuilder QueryBuilders.queryStringQuery(dto.getSearchWords()).field(title).field(content).defaultOperator(Operator.OR);boolQueryBuilder.must(queryStringQueryBuilder);//查询小于mindate的数据RangeQueryBuilder rangeQueryBuilder QueryBuilders.rangeQuery(publishTime).lt(dto.getMinBehotTime().getTime());boolQueryBuilder.filter(rangeQueryBuilder);//分页查询searchSourceBuilder.from(0);searchSourceBuilder.size(dto.getPageSize());//按照发布时间倒序查询searchSourceBuilder.sort(publishTime, SortOrder.DESC);//设置高亮 titleHighlightBuilder highlightBuilder new HighlightBuilder();highlightBuilder.field(title);highlightBuilder.preTags(font stylecolor: red; font-size: inherit;);highlightBuilder.postTags(/font);searchSourceBuilder.highlighter(highlightBuilder);searchSourceBuilder.query(boolQueryBuilder);searchRequest.source(searchSourceBuilder);SearchResponse searchResponse restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//3.结果封装返回ListMap list new ArrayList();SearchHit[] hits searchResponse.getHits().getHits();for (SearchHit hit : hits) {String json hit.getSourceAsString();Map map JSON.parseObject(json, Map.class);//处理高亮if(hit.getHighlightFields() ! null hit.getHighlightFields().size() 0){Text[] titles hit.getHighlightFields().get(title).getFragments();String title StringUtils.join(titles);//高亮标题map.put(h_title,title);}else {//原始标题map.put(h_title,map.get(title));}list.add(map);}return ResponseResult.okResult(list);} } 新建控制器ArticleSearchController package com.heima.search.controller.v1;import com.heima.model.common.dtos.ResponseResult; import com.heima.model.search.dtos.UserSearchDto; import com.heima.search.service.ArticleSearchService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.io.IOException;RestController RequestMapping(/api/v1/article/search) public class ArticleSearchController {Autowiredprivate ArticleSearchService articleSearchService;PostMapping(/search)public ResponseResult search(RequestBody UserSearchDto dto) throws IOException {return articleSearchService.search(dto);} } 需要在app的网关中添加搜索微服务的路由配置 #搜索微服务 - id: leadnews-searchuri: lb://leadnews-searchpredicates:- Path/search/**filters:- StripPrefix 1 启动项目进行测试至少要启动文章微服务用户微服务搜索微服务app网关微服务app前端工程 四.文章自动审核构建索引 文章微服务发送消息 1.把SearchArticleVo放到model工程下   package com.heima.model.search.vos;import lombok.Data;import java.util.Date;Data public class SearchArticleVo {// 文章idprivate Long id;// 文章标题private String title;// 文章发布时间private Date publishTime;// 文章布局private Integer layout;// 封面private String images;// 作者idprivate Long authorId;// 作者名词private String authorName;//静态urlprivate String staticUrl;//文章内容private String content;} 2.文章微服务的ArticleFreemarkerService中的buildArticleToMinIO方法中收集数据并发送消息 完整代码如下 package com.heima.article.service.impl;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.heima.article.mapper.ApArticleContentMapper; import com.heima.article.service.ApArticleService; import com.heima.article.service.ArticleFreemarkerService; import com.heima.common.constants.ArticleConstants; import com.heima.file.service.FileStorageService; import com.heima.model.article.pojos.ApArticle; import com.heima.model.search.vos.SearchArticleVo; import freemarker.template.Configuration; import freemarker.template.Template; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.StringWriter; import java.util.HashMap; import java.util.Map;Service Slf4j Transactional public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {Autowiredprivate ApArticleContentMapper apArticleContentMapper;Autowiredprivate Configuration configuration;Autowiredprivate FileStorageService fileStorageService;Autowiredprivate ApArticleService apArticleService;/*** 生成静态文件上传到minIO中* param apArticle* param content*/AsyncOverridepublic void buildArticleToMinIO(ApArticle apArticle, String content) {//已知文章的id//4.1 获取文章内容if(StringUtils.isNotBlank(content)){//4.2 文章内容通过freemarker生成html文件Template template null;StringWriter out new StringWriter();try {template configuration.getTemplate(article.ftl);//数据模型MapString,Object contentDataModel new HashMap();contentDataModel.put(content, JSONArray.parseArray(content));//合成template.process(contentDataModel,out);} catch (Exception e) {e.printStackTrace();}//4.3 把html文件上传到minio中InputStream in new ByteArrayInputStream(out.toString().getBytes());String path fileStorageService.uploadHtmlFile(, apArticle.getId() .html, in);//4.4 修改ap_article表保存static_url字段apArticleService.update(Wrappers.ApArticlelambdaUpdate().eq(ApArticle::getId,apArticle.getId()).set(ApArticle::getStaticUrl,path));//发送消息创建索引createArticleESIndex(apArticle,content,path);}}Autowiredprivate KafkaTemplateString,String kafkaTemplate;/*** 送消息创建索引* param apArticle* param content* param path*/private void createArticleESIndex(ApArticle apArticle, String content, String path) {SearchArticleVo vo new SearchArticleVo();BeanUtils.copyProperties(apArticle,vo);vo.setContent(content);vo.setStaticUrl(path);kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));}} 3. 在ArticleConstants类中添加新的常量完整代码如下 package com.heima.common.constants;public class ArticleConstants {public static final Short LOADTYPE_LOAD_MORE 1;public static final Short LOADTYPE_LOAD_NEW 2;public static final String DEFAULT_TAG __all__;public static final String ARTICLE_ES_SYNC_TOPIC article.es.sync.topic;public static final Integer HOT_ARTICLE_LIKE_WEIGHT 3;public static final Integer HOT_ARTICLE_COMMENT_WEIGHT 5;public static final Integer HOT_ARTICLE_COLLECTION_WEIGHT 8;public static final String HOT_ARTICLE_FIRST_PAGE hot_article_first_page_; } 4.文章微服务集成kafka发送消息 在文章微服务的nacos的配置中心添加如下配置 kafka:bootstrap-servers: 192.168.200.130:9092producer:retries: 10key-serializer: org.apache.kafka.common.serialization.StringSerializervalue-serializer: org.apache.kafka.common.serialization.StringSerializer 搜索微服务接收消息并创建索引 spring:kafka:bootstrap-servers: 192.168.200.130:9092consumer:group-id: ${spring.application.name}key-deserializer: org.apache.kafka.common.serialization.StringDeserializervalue-deserializer: org.apache.kafka.common.serialization.StringDeserializer 5.定义监听接收消息,保存索引数据 package com.heima.search.listener;import com.alibaba.fastjson.JSON; import com.heima.common.constants.ArticleConstants; import com.heima.model.search.vos.SearchArticleVo; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.action.index.IndexRequest; 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.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component;import java.io.IOException;Component Slf4j public class SyncArticleListener {Autowiredprivate RestHighLevelClient restHighLevelClient;KafkaListener(topics ArticleConstants.ARTICLE_ES_SYNC_TOPIC)public void onMessage(String message){if(StringUtils.isNotBlank(message)){log.info(SyncArticleListener,message{},message);SearchArticleVo searchArticleVo JSON.parseObject(message, SearchArticleVo.class);IndexRequest indexRequest new IndexRequest(app_info_article);indexRequest.id(searchArticleVo.getId().toString());indexRequest.source(message, XContentType.JSON);try {restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();log.error(sync es error{},e);}}} } 五.保存用户搜索记录 展示用户的搜索记录10条按照搜索关键词的时间倒序 可以删除搜索记录 保存历史记录保存10条多余的则删除最久的历史记录 用户的搜索记录需要给每一个用户都保存一份数据量较大要求加载速度快通常这样的数据存储到mongodb更合适不建议直接存储到关系型数据库中   1.MongoDB安装及集成 拉取镜像   docker pull mongo 创建容器 docker run -d --name mongo-service --restartalways -p 27017:27017 -v ~/data/mongodata:/data mongo 2.保存搜索记录 用户输入关键字进行搜索的异步记录关键字   3.用户搜索记录对应的集合对应实体类   package com.heima.search.pojos;import lombok.Data; import org.springframework.data.mongodb.core.mapping.Document;import java.io.Serializable; import java.util.Date;/*** p* APP用户搜索信息表* /p* author itheima*/ Data Document(ap_user_search) public class ApUserSearch implements Serializable {private static final long serialVersionUID 1L;/*** 主键*/private String id;/*** 用户ID*/private Integer userId;/*** 搜索词*/private String keyword;/*** 创建时间*/private Date createdTime;} ApAssociateWords  package com.heima.search.pojos;import lombok.Data; import org.springframework.data.mongodb.core.mapping.Document;import java.io.Serializable; import java.util.Date;/*** p* 联想词表* /p** author itheima*/ Data Document(ap_associate_words) public class ApAssociateWords implements Serializable {private static final long serialVersionUID 1L;private String id;/*** 联想词*/private String associateWords;/*** 创建时间*/private Date createdTime;} 4.实现步骤 4.1pom依赖   dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId /dependency 4.2nacos配置 spring:data:mongodb:host: 192.168.200.130port: 27017database: leadnews-history 4.3.创建ApUserSearchService新增insert方法 public interface ApUserSearchService {/*** 保存用户搜索历史记录* param keyword* param userId*/public void insert(String keyword,Integer userId); } 实现类 Service Slf4j public class ApUserSearchServiceImpl implements ApUserSearchService {Autowiredprivate MongoTemplate mongoTemplate;/*** 保存用户搜索历史记录* param keyword* param userId*/OverrideAsyncpublic void insert(String keyword, Integer userId) {//1.查询当前用户的搜索关键词Query query Query.query(Criteria.where(userId).is(userId).and(keyword).is(keyword));ApUserSearch apUserSearch mongoTemplate.findOne(query, ApUserSearch.class);//2.存在 更新创建时间if(apUserSearch ! null){apUserSearch.setCreatedTime(new Date());mongoTemplate.save(apUserSearch);return;}//3.不存在判断当前历史记录总数量是否超过10apUserSearch new ApUserSearch();apUserSearch.setUserId(userId);apUserSearch.setKeyword(keyword);apUserSearch.setCreatedTime(new Date());Query query1 Query.query(Criteria.where(userId).is(userId));query1.with(Sort.by(Sort.Direction.DESC,createdTime));ListApUserSearch apUserSearchList mongoTemplate.find(query1, ApUserSearch.class);if(apUserSearchList null || apUserSearchList.size() 10){mongoTemplate.save(apUserSearch);}else {ApUserSearch lastUserSearch apUserSearchList.get(apUserSearchList.size() - 1);mongoTemplate.findAndReplace(Query.query(Criteria.where(id).is(lastUserSearch.getId())),apUserSearch);}} } 4.4参考自媒体相关微服务在搜索微服务中获取当前登录的用户 通过在app-gateway中获取用户id存入header中然后在search的拦截器中获取userId存入AppThreadLocalUtil工具类中 4.5.在ArticleSearchService的search方法中调用保存历史记录 Overridepublic ResponseResult search(UserSearchDto dto) throws IOException {//1.检查参数if(dto null || StringUtils.isBlank(dto.getSearchWords())){return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);}ApUser user AppThreadLocalUtil.getUser();//异步调用 保存搜索记录if(user ! null dto.getFromIndex() 0){apUserSearchService.insert(dto.getSearchWords(), user.getId());}//2.设置查询条件SearchRequest searchRequest new SearchRequest(app_info_article);SearchSourceBuilder searchSourceBuilder new SearchSourceBuilder();//布尔查询BoolQueryBuilder boolQueryBuilder QueryBuilders.boolQuery();//关键字的分词之后查询 4.6.保存历史记录中开启异步调用添加注解Async 4.7在搜索微服务引导类上开启异步调用 4.8测试搜索后查看结果   六.查询用户搜素记录 要求当点击搜索框时获取用户的搜索记录 说明接口路径/api/v1/history/load请求方式POST参数无响应结果ResponseResult 在ApUserSearchService中新增方法 /**查询搜索历史return*/ ResponseResult findUserSearch(); 实现方法 /*** 查询搜索历史** return*/ Override public ResponseResult findUserSearch() {//获取当前用户ApUser user AppThreadLocalUtil.getUser();if(user null){return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);}//根据用户查询数据按照时间倒序ListApUserSearch apUserSearches mongoTemplate.find(Query.query(Criteria.where(userId).is(user.getId())).with(Sort.by(Sort.Direction.DESC, createdTime)), ApUserSearch.class);return ResponseResult.okResult(apUserSearches); } 控制器 /*** p* APP用户搜索信息表 前端控制器* /p* author itheima*/ Slf4j RestController RequestMapping(/api/v1/history) public class ApUserSearchController{Autowiredprivate ApUserSearchService apUserSearchService;PostMapping(/load)public ResponseResult findUserSearch() {return apUserSearchService.findUserSearch();}} 七.删除用户搜索记录 说明接口路径/api/v1/history/del请求方式POST参数HistorySearchDto响应结果ResponseResult HistorySearchDto  Data public class HistorySearchDto {/*** 接收搜索历史记录id*/String id; } 在ApUserSearchService中新增方法 /**删除搜索历史param historySearchDtoreturn*/ ResponseResult delUserSearch(HistorySearchDto historySearchDto); 实现方法   /*** 删除历史记录** param dto* return*/ Override public ResponseResult delUserSearch(HistorySearchDto dto) {//1.检查参数if(dto.getId() null){return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);}//2.判断是否登录ApUser user AppThreadLocalUtil.getUser();if(user null){return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);}//3.删除mongoTemplate.remove(Query.query(Criteria.where(userId).is(user.getId()).and(id).is(dto.getId())),ApUserSearch.class);return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); } 修改ApUserSearchController补全方法 PostMapping(/del) public ResponseResult delUserSearch(RequestBody HistorySearchDto historySearchDto) {return apUserSearchService.delUserSearch(historySearchDto); } 打开app可以删除搜索记录 八.联想词搜索 根据用户输入的关键字展示联想词 搜索词-数据来源 通常是网上搜索频率比较高的一些词通常在企业中有两部分来源 第一自己维护搜索词 通过分析用户搜索频率较高的词按照排名作为搜索词 第二第三方获取 关键词规划师百度、5118、爱站网 功能实现 说明接口路径/api/v1/associate/search请求方式POST参数UserSearchDto响应结果ResponseResult 新建接口 package com.heima.search.controller.v1;import com.heima.model.common.dtos.ResponseResult; import com.heima.model.search.dtos.UserSearchDto; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(/api/v1/associate) public class ApAssociateWordsController {PostMapping(/search)public ResponseResult search(RequestBody UserSearchDto userSearchDto) {return null;} } 新建联想词业务层接口 package com.heima.search.service;import com.heima.model.common.dtos.ResponseResult; import com.heima.model.search.dtos.UserSearchDto;/*** p* 联想词表 服务类* /p** author itheima*/ public interface ApAssociateWordsService {/**联想词param userSearchDtoreturn*/ResponseResult findAssociate(UserSearchDto userSearchDto);} 实现类 package com.heima.search.service.impl;import com.heima.model.common.dtos.ResponseResult; import com.heima.model.common.enums.AppHttpCodeEnum; import com.heima.model.search.dtos.UserSearchDto; import com.heima.search.pojos.ApAssociateWords; import com.heima.search.service.ApAssociateWordsService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service;import java.util.List;/*** Description:* Version: V1.0*/ Service public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {AutowiredMongoTemplate mongoTemplate;/*** 联想词* param userSearchDto* return*/Overridepublic ResponseResult findAssociate(UserSearchDto userSearchDto) {//1 参数检查if(userSearchDto null || StringUtils.isBlank(userSearchDto.getSearchWords())){return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);}//分页检查if (userSearchDto.getPageSize() 20) {userSearchDto.setPageSize(20);}//3 执行查询 模糊查询Query query Query.query(Criteria.where(associateWords).regex(.*?\\ userSearchDto.getSearchWords() .*));query.limit(userSearchDto.getPageSize());ListApAssociateWords wordsList mongoTemplate.find(query, ApAssociateWords.class);return ResponseResult.okResult(wordsList);} } 新建联想词控制器 package com.heima.search.controller.v1;import com.heima.model.common.dtos.ResponseResult; import com.heima.model.search.dtos.UserSearchDto; import com.heima.search.service.ApAssociateWordsService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** p* 联想词表 前端控制器* /p* author itheima*/ Slf4j RestController RequestMapping(/api/v1/associate) public class ApAssociateWordsController{Autowiredprivate ApAssociateWordsService apAssociateWordsService;PostMapping(/search)public ResponseResult findAssociate(RequestBody UserSearchDto userSearchDto) {return apAssociateWordsService.findAssociate(userSearchDto);} }

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

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

相关文章

做公众号的网站模板下载wordpress美术馆插件

在 Golang 的日常开发中,往往要面对各种和操作系统相关的操作,例如文件的读写、环境变量的处理、程序参数的获取等等。Golang 的 os 标准库为我们提供了与操作系统打交道的各类工具,能让这些操作变得更加简洁和简单。 基础应用 文件的读写操…

关于网站开发的外文书籍搞笑图片网站源码

简介: minicoredump神也! 继上一篇非典型程序员青囊搞定内存泄露问题后,美美地睡了一觉。睡梦中,突然金光闪闪,万道光芒照进时光隧道,恍惚来到大唐神龙年间。青囊此时化身狄仁杰高级助理,陪同狄…

手机建站程序下载个网上销售网站

从架构的角度看来,Asterisk是由许多不同的模块组成的。在设计基于Asterisk的系统时,这种模块化的特性,提供了几乎无限的灵活必。作为Asterisk系统管理员,你拥有选择加载模块的权利。你所加载的每一个模块,都提供了不同…

所有的网站都要用htmlu做吗超级优化残剑

项目开发经验谈:项目的到底谁说了算 前言:项目到底是为谁而做,一个项目的成功到底是怎么样在评价:是领导阶层肯定,还是客户满意? 系列文章链接 项目开发经验谈:如何成为出色的开发人员盲目的项目…

net网站建设语言小型网站开发需要什么步骤

缘起 我在上一篇文章——《调试实战 —— dll 加载失败之全局变量初始化篇》中,跟大家分享了一个由于全局变量初始化顺序导致的 dll 加载失败的例子。感兴趣的小伙伴儿可以点击阅读。虽然我们知道了是由于全局变量初始化顺序导致的问题,也给出了解决方案…

哈密做网站公众号引流推广平台

先介绍一下本文的作者,本人本科来自于某双非财经类院校,于2019年考入南开大学某应用经济学专业,在2019年考研初始专业课826经济学基础(也就是20年的823)中拿到了138分的成绩,自认为对专业课的复习有一定心得…

自助建设彩票网站在建立网站站点的过程中

经常做的一个操作,列出数据库中一个表的所有列名。方法如下:先从SYSTEMOBJECT系统表中取得数据表的SYSTEMID,然后再从SYSCOLUMN表中取得该表的所有列名。SQL语句如下:use dbname --dbname改为你要…

cms企业网站模板大连微信公众号开发公司

宇宙射线也叫电磁波,其中包含γ射线、X射线、紫外线、可见光、红外线、近红外、远红外,还有无线电和超声波。 无线电波是振荡电路中自由电子作周期性的运动产生的. 红外线、可见光、紫外线是原子外层电子受激发产生的. X射线是原…

免费dede企业网站模板鞍山网站怎么做出来的

电机特性 电机堵转: 电机堵转的原理 玻璃升降器: 工作电压 升降器在 9V~16V 电压下应运行平稳,不允许有异音和卡滞现象。 工作电流 升降器的工作电流不大于 12A,堵转电流不大于 28A。 堵转 力 升降器 堵转 力应 不小于 212N。 玻璃升降器结构 电动车窗…

天津市网站建设 网页制作做汽车配件生意的网站

总结: ①没有byte的字面值,赋值时需要强制转换类型 ②涉及运算,系统自动进行类型升级,由此用final修饰,代表这是一个不会更改值的常量,通过编译 感受:还是用int吧,自动类型转换太复…

青岛 企业网站建站如何去做电商

应用分层 在开发的过程中, 我们会发现, 程序的代码有时会很"杂乱", 如果后面的项目更大了, 那就会更加地杂乱无章(文件乱, 代码内容乱). 也基于此, 接下来让我们来学习一下应用分层. 也类似于公司的组织架构 公司初创阶段, 一个人身兼数职, 既做财务, 又做人事,还有…

jrs直播网站谁做的网站建设所用的工具

初中数学课程标准修改后,教材中四点共圆知识已经删除掉了,但这样一件强悍且使用简单的武器,我们还是有必要去了解的,近年来对于压轴题以几何为核心的考区来说,有时用到解题更为简洁方便,由此应该理解掌握。…

怎么做加盟美容院网站完美代码网站

注: 在《SVD(异值分解)小结 》中分享了SVD原理,但其中只是利用了numpy.linalg.svd函数应用了它,并没有提到如何自己编写代码实现它,在这里,我再分享一下如何自已写一个SVD函数。但是这里会利用到SVD的原理,…

企业建设网站方案建筑网片厂家货源平台

1.定义 trait trait 定义了某个特定类型拥有可能与其他类型共享的功能。可以通过 trait 以一种抽象的方式定义共享的行为。可以使用 trait bounds 指定泛型是任何拥有特定行为的类型。 一个类型的行为由其可供调用的方法构成。如果可以对不同类型调用相同的方法的话&#xff…

做网站的费用wordpress链接在哪里设置

【摘要】数据安全是计算机安全问题的核心,对于很多具有高度保密要求的单位,安全地存储重要数据,并且在不需要这些数据时作彻底销毁不被他人恢复是至关重要的。本文详细介绍了数据安全存储技术的发展现状,并结合典型的企业信息安全…

2013年以前pc网站建设找网页模板的网站好

产品概述: NCV8705 是一款低噪音、低功耗和低泄漏线性电压稳压器。该器件具有卓越的噪音和 PSRR 规格,适用于使用视频接收器、成像传感器、音频处理器或需要外部洁净电源的任何部件的产品。NCV8705 使用创新的自适应接地电流电路 可确保轻负载调节下的超…

建设网站怎么加购物车网站模板模板

SSH密钥文件 Github里面S设置SH公钥有两者选择方式 账号下的每个仓库都设置一个公钥,因为GitHub官方要求每个仓库的公钥都不能相同,所以每个账号都要搞一个密钥(很麻烦)给账号分配一个公钥,然后这个公钥就可以在这个…

网站建设最好的书籍是北京商场打折

作者简介: 目录 1.概述 2.CPU管理 3.内存管理 4.IO管理 1.概述 操作系统可以看作一个计算机的管理系统,对计算机的硬件资源提供了一套完整的管理解决方案。计算机的硬件组成有五大模块:运算器、控制器、存储器、输入设备、输出设备。操作…

做流量任务的试用网站新郑网络推广外包

MySQL是我们经常使用的数据库处理系统(DBMS),不知小伙伴们有没有注意过其中的“存储引擎”(storage_engine)呢?有时候面试题中也会问道MySQL几种常用的存储引擎的区别。这次就简短侃一下存储引擎那些事儿。先去查一下“引擎”概念。引擎(Engine)是电子平…

地方文明网站建设wordpress 文章id修改

导言 在如今的互联网时代,Java接口压力测试是评估系统性能和可靠性的关键一环。一旦接口不能承受高并发量,用户体验将受到严重影响,甚至可能导致系统崩溃。因此,了解如何进行有效的Java接口压力测试以及如何优化接口性能至关重要…