宜立方商城——搜索功能的实现

news/2025/3/16 6:06:47/

搜索功能的实现

效果图

模块划分

需要的我们自己写dao层mapper层

dao层存在我们搜索的结果

 

/*** 商品搜索dao*/
@Repository
public class SearchDao {@Autowiredprivate SolrServer solrServer;/*** 根据查询条件查询索引库* @param solrQuery* @return*/public SearchResult search(SolrQuery solrQuery) throws SolrServerException {//根据solrQuery查询索引库QueryResponse query = solrServer.query(solrQuery);//取查询结果SolrDocumentList results = query.getResults();//取查询结果总记录数long numFound = results.getNumFound();//创建一个SearchResullt对象SearchResult searchResult=new SearchResult();searchResult.setRecordCount(numFound);//取商品列表,需要取高亮的显示Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();//创建一个存储商品列表的集合List<SearchItem> itemList =new ArrayList<>();//遍历文档列表,从域中去内容取高亮中的需要的字段id必须要有for (SolrDocument document :results) {//创建一个SearchItem对象SearchItem searchItem=new SearchItem();//设置需要SearchItem对象的属性searchItem.setId((String) document.get("id"));searchItem.setCategory_name((String) document.get("item_category_name"));searchItem.setImage((String) document.get("item_image"));searchItem.setPrice((Long) document.get("item_price"));searchItem.setSell_point((String) document.get("item_sell_point"));//取高亮显示List<String> list = highlighting.get(document.get("id")).get("item_title");//创建一个title空字符串String title="";//判断title数据中是否有高度数据if (list !=null && list.size()>0){//有高亮数据title=list.get(0);}else {//没有高亮数据就取文档中的数据title= (String) document.get("item_title");}//将标题添加到searchItem对象中searchItem.setTitle(title);//添加到商品列表itemList.add(searchItem);}//添加商品列表到SearchResullt对象searchResult.setItemList(itemList);//返回结果return searchResult;}
}

Itemmapper.xml配置

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.e3mall.search.mapper.ItemMapper"><!--查询商品信息--><select id="getItemList" resultType="com.e3mall.common.pojo.SearchItem">SELECTa.id,a.title,a.sell_point,a.price,a.image,b.name category_nameFROMtb_item aLEFT JOIN tb_item_cat bon a.cid=b.idWHERE a.status=1;</select><!--通过id查询SearchItem pojo中的属性数据--><select id="getItemById" resultType="com.e3mall.common.pojo.SearchItem" parameterType="long">SELECTa.id,a.title,a.sell_point,a.price,a.image,b.name category_nameFROMtb_item aLEFT JOIN tb_item_cat bon a.cid=b.idWHERE a.status=1 and a.id=#{itemId};</select></mapper>

依照我红色的字体写对应ItemMapper接口类即可附图依照

SearchItemServiceImpl.java

 

/*** 商品数据索引库Service*/
@Service
public class SearchItemServiceImpl implements SearchItemService {@Autowiredprivate ItemMapper itemMapper;@Autowiredprivate SolrServer solrServer;/*** 将删商品数据导入索引库* @return*/@Overridepublic E3Result importItems() {try {//查询商品列表List<SearchItem> itemList = itemMapper.getItemList();//导入到索引库for (SearchItem item :itemList) {//创建文档对象SolrInputDocument document=new SolrInputDocument();//向文档添加域document.addField("id",item.getId());document.addField("item_title",item.getTitle());document.addField("item_sell_point",item.getSell_point());document.addField("item_price",item.getPrice());document.addField("item_image",item.getImage());document.addField("item_category_name",item.getCategory_name());//写入索引库solrServer.add(document);}//提交solrServer.commit();//返回成功return E3Result.ok();}catch (Exception e){e.printStackTrace();return E3Result.build(500,"商品导入失败!");}}
}

applicationContent-service.xml配置

代码

 

<dubbo:service interface="com.e3mall.search.service.SearchItemService" ref="searchItemServiceImpl" timeout="600000"/>

表现层

 

/*** 商品搜索Controller*/
@Controller
public class SearchController {@Autowiredprivate SearchService searchService;@Value("${SEACHER_RESULT_ROWS}")private Integer SEACHER_RESULT_ROWS;/*** 分页查询功能* @param keyword 查询添加* @param page 结果从第几条记录开始显示这里我们设置了默认值1* @param model* @return*/@RequestMapping("/search")public  String search(String keyword, @RequestParam(defaultValue = "1") Integer page, Model model) throws Exception {keyword=new String(keyword.getBytes("ISO-8859-1"),"utf-8");//调用服务查询商品列表SearchResult result = searchService.search(keyword, page, SEACHER_RESULT_ROWS);//把结果传递给页面model.addAttribute("query",keyword);model.addAttribute("totalPages",result.getTotalPages());model.addAttribute("page",page);model.addAttribute("recourdCount",result.getRecordCount());model.addAttribute("itemList",result.getItemList());//返回逻辑页面return "search";}
}

springmvc.xml引入服务

 

<!-- 引用dubbo服务 -->
<dubbo:application name="e3-manager-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>
<!--调用搜索服务-->
<dubbo:reference interface="com.e3mall.search.service.SearchService" id="searchService" />

 


http://www.ppmy.cn/news/859341.html

相关文章

宜搭和钉钉审批流的比较

一、宜搭 版本介绍 使用场景 在宜搭的应用中&#xff0c;通过以下几类页面来配合完成完整的业务场景需求的解决&#xff1a; 表单&#xff1a;存储或收集数据&#xff0c;通过权限配置实现不同角色人员能够拥有不同的数据增删改查、导入、导出等权限&#xff1b;流程表单&…

ACM搜索题目总结

转载自https://www.cnblogs.com/AbandonZHANG/archive/2012/07/27/2612415.html ACM搜索题目总结 格式说明&#xff1a;题目名后面列出个人此题的大致难度&#xff08;对菜鸟而言&#xff09; POJ 1069 -The Bermuda Triangle(难) http://acm.pku.edu.cn/JudgeOnline/problem?…

搜索引擎爬虫

搜索引擎爬虫&#xff08;优质引流&#xff1f;&#xff1f;&#xff1f;&#xff09; 最近发现服务器日志上多了一些奇怪的日志 {"remote_addr":"203.208.60.66","remote_user":"","time_local":"25/Oct/2021:14:34…

宜搜冲刺港交所:年营收4.3亿 软银与盛大是股东

雷递网 雷建平 3月1日 宜搜科技控股有限公司&#xff08;简称&#xff1a;“宜搜”&#xff09;日前递交招股书&#xff0c;准备在港交所上市。 宜搜创始人汪溪是行业老兵&#xff0c;曾在2018年准备在A股上市&#xff0c;但最后撤回了上市计划。宜搜也曾酝酿美股上市&#xff…

宜搜宣讲(10月17号 )

下午赶去宜搜&#xff0c;有了上次的经验&#xff0c;我们先在中科院的饭馆了吃好饭再去教室的&#xff0c;去的时候教室还没几个人&#xff0c;正在放歌&#xff1a;我要飞的更高。。。 宜搜是一家小公司&#xff0c;来宣讲的几个技术人员也不怎么会说话&#xff0c;而且幻灯…

宜搜将涉水购物搜索平台 尝试建立020平台

有消息称宜搜将于下月发布旗下移动应用宜搜的新版本,并同时上线购物搜索功能,与此同时有媒体指出宜搜或正在布局移动020领域。记者随即致电宜搜CEO汪溪,汪溪回应称:下月底发布的宜搜新版本确实增加了购物搜索功能,但并未涉及020领域。 汪溪指出,新版本的购物搜索功能主要通过与…

宜搜将免费为中小企业开通移动电商营销平台

国里面文挪动寻找办事供应商宜搜日前推出移动电子商务“暖春”举止。始末该活动&#xff0c;宜搜将免费为国内中小企业开通移动电子商务营销平台。寰宇各地的中小企业可玩弄该平台&#xff0c;举行移动汇集上的B2B、B2C的电子商务生意&#xff0c;通过无限搜索来进行营销。 宜搜…

深圳宜搜2013校园招聘 笔试回忆录

分C/C 和java、 hadoop、android、数据分析几大类&#xff0c;笔试题各不相同。后来java和hadoop做的相同的题。 我做的c/C试题。两道选择题很简单。剩下的是大题&#xff1a; 1&#xff0c;写出你知道的排序方法&#xff0c;及时间复杂度及稳定性。至少五个 2&#xff0c;写…