当前位置:文档之家› 【黑马程序员】面试题-利用solr实现商品的搜索功能

【黑马程序员】面试题-利用solr实现商品的搜索功能

【黑马程序员】面试题-利用solr实现商品的搜索功能问题提出:当我们访问购物网站的时候,我们可以根据我们随意所想的内容输入关键字就可以查询出相关的内容,这是怎么做到呢?这些随意的数据不可能是根据数据库的字段查询的,那是怎么查询出来的呢,为什么千奇百怪的关键字都可以查询出来呢?答案就是全文检索工具的实现,luncence采用了词元匹配和切分词。

举个例子:北京天安门------luncence切分词:北京京天天安安门等等这些分词。

所以我们搜索的时候都可以检索到。

有一种分词器就是ik中文分词器,它有细粒度切分和智能切分,即根据某种智能算法。

这就使用solr的最大的好处:检索功能的实现。

使用步骤;(1)solr服务器搭建,因为solr是用java5开发的,所以需要jdk和tomcat。

搭建部署(2)搭建完成后,我们需要将要展示的字段引入solr的库中。

配置sring与solr结合,工程启动的时候启动solr(3)将数据库中的查询内容导入到solr索引库,这里使用的是solrj的客户端实现的。

具体使用可以参考api(4)建立搜索服务,供客户端调用。

调用solr,查询内容,这中间有分页功能的实现。

solr高亮显示的实现。

(5)客户端接收页面的请求参数,调用搜索服务,进行搜索。

业务字段判断标准:1、在搜索时是否需要在此字段上进行搜索。

例如:商品名称、商品的卖点、商品的描述(这些相当于将标签给了solr,导入商品数据后,solr对这些字段的对应的商品的具体内容进行分词切分,然后,我们就可以搜索到相关内容了)2、后续的业务是否需要用到此字段。

例如:商品id。

需要用到的字段:1、商品id2、商品title3、卖点4、价格5、商品图片6、商品分类名称7、商品描述Solr中的业务字段:1、id——》商品id其他的对应字段创建solr的字段。

[AppleScript] 纯文本查看复制代码?01 02 03 04 05 06 07<field name="item_title" type="text_ik" indexed="true" stored="true"/><field name="item_sell_point" type="text_ik" indexed="true" stored="true"/><field name="item_price" type="long" indexed="true"08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23stored="true"/><field name="item_image" type="string" indexed="false" stored="true" /><field name="item_category_name" type="string" indexed="true" stored="true" /><field name="item_desc" type="text_ik" indexed="true" stored="false" /><field name="item_keywords" type="text_ik" indexed="true"stored="false" multiValued="true"/><copyField source="item_title" dest="item_keywords"/><copyField source="item_sell_point" dest="item_keywords"/><copyField source="item_category_name" dest="item_keywords"/><copyField source="item_desc" dest="item_keywords"/>重新启动tomcatSolr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。

Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。

Solr是一个全文检索服务器,只需要进行配置就可以实现全文检索服务。

有效降低频繁访问数据库对数据库造成的压力。

第一步:将solr部署在linux系统下。

第二步:solrJ是solr的客户端,使用它需要依赖solrJ的jar包。

第三步:将数据库的内容添加到solr的索引库,这样查询就在索引库查询,而不是数据库了。

controller层:[AppleScript] 纯文本查看复制代码?01 02 03 04 05 06 07 08 09 10 11 12@Controller@RequestMapping("/manager")public class ItemController {@Autowiredprivate ItemService itemService;@RequestMapping("/importall")@ResponseBodypublic TaotaoResult importAllItem(){TaotaoResult result=itemService.importAllItem();return result;}}<br>service层编写:<br>多表查询商品,显示在页面的逻辑编写:<br>mapper.java[AppleScript] 纯文本查看复制代码?01 02 03 04 05 06 07 08 09 10 11package com.taotao.search.mapper;import java.util.List;import com.taotao.search.pojo.Item;public interface ItemMapper {List<item> getItemList();}</item>mapper.xml [AppleScript] 纯文本查看复制代码?01 02 03 04<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.taotao.search.mapper.ItemMapper">05 06 07 08 09 10 11 12 13 14 15 16 17<select id="getItemList" resultType="com.taotao.search.pojo.Item"> SELECTa.id,a.title,a.sell_point,a.price,a.image,b. NAME category_nameFROMtb_item aLEFT JOIN tb_item_cat b ON a.cid = b.id</select></mapper>四步:从索引库查询的逻辑编写:[AppleScript] 纯文本查看复制代码?01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24//从索引库里面获取商品信息,现在这个dao层是从索引库获取信息,因为之前的写的逻辑是将db里面的数据导入到索引库。

后面的查询都是从索引库中进行,而不从数据库了@Repositorypublic class SearchDaoImpl implements SearchDao {@Autowiredprivate SolrServer solrServer;@Overridepublic SearchResult search(SolrQuery query) throws Exception {//这是从索引库里面,直接执行查询QueryResponse response = solrServer.query(query);//获取查询的结果SolrDocumentList documentList=response.getResults();SearchResult result=new SearchResult();//这是获取总记录数result.setRecordCount(documentList.getNumFound());List<Item> itemList=new ArrayList<>();//商品的高亮显示,即当鼠标移到字上时,该字体变色,这是从QueryResponse中获取的25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();for (SolrDocument solrDocument : documentList) {//每个SolrDocument都是一个商品pojo的内容,所以这里要创建一个商品的pojo对象,来获取详细的字段Item item=new Item();item.setId((String)solrDocument.get("id"));//高亮显示是title的高亮显示List<String> list =highlighting.get(solrDocument.get("id")).get("item_title");String title="";if (list!=null && list.size()>0) {title=list.get(0);}else{title=(String)solrDocument.get("item_title");}item.setTitle(title);item.setPrice((Long)solrDocument.get("item_price"));item.setImage((String)solrDocument.get("item_image"));item.setCategory_name((String) solrDocument.get(" item_category_name"));item.setSell_point((String)solrDocument.get("item_sell_point"));itemList.add(item);}result.setItemList(itemList);return result;}}第五步:索引库内容建立好后,开始编写对外的服务接口,即通过条件搜索具体的商品,比如手机,会显示出总共的手机列表信息,第几页,总共多少页,总共多少个搜索结果请求的url:file:///C:/Users/ADMINI~1.WIN/AppData/Local/Temp/V7(XMW RN]%7BG8~CI%7DBCCR3QC.gif arch/query?q={查询条件}&page={page}&rows={rows}返回的结果:TaotaoResult包装商品列表。

相关主题