版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. 课程计划1、搜索工程的搭建2、linux 下 solr 服务的搭建3、Solrj 使用测试4、把数据库中的数据导入索引库5、搜索功能的实现2. 搜索工程搭建要实现搜索功能,需要搭建 solr 服务、搜索服务工程、搜索系统(表现层的工程)2.1. Solr 服务搭建Solr 的环境Solr 是 java 开发。 solr 的安装文件 需要安装 jdk。安装环境 Linux。需要安装 Tomcat。搭建步骤第一步:把 solr 的压缩包上传到 Linux 系统第二步:解压 solr。第三步:安装 Tomcat,解压缩即可。第四步:把 solr 部署到 Tomcat 下。第五步:解压缩 war
2、 包。可以启动 Tomcat 解压。第六步:把/root/solr-4.10.3/exrootlocalhost ext# pwdle/lib/ext 目录下的所有的 jar 包,添加到 solr 工。/root/solr-4.10.3/exle/lib/extrootlocalhost ext# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/第七步:创建一个 solrhome 。/ex/usr/local/solr/solrhomele/solr 目录就是一个 solrhome 。此目录到rootlocalhost ex/root/
3、solr-4.10.3/ex rootlocalhost ex rootlocalhost exle# pwd lele# cp -r solr /usr/local/solr/solrhomele#第八步:关联 solr 及 solrhome。需要修改 solr 工程的 web.xml 文件。第九步:启动 Tomcat和 windows 下的配置完全一样。2.1.3. Solr 的使用添加文档必须有 id 域,其他域 必须在 solr 的 schema.xml 中定义。2.2. 配置业务域schema.xml 中定义1、商品 Id使用 schema.xml 中的 id 域2、商品标题3、商品
4、卖点4、商品价格5、商品6、分类名称7、商品描述创建对应的业务域。需要制定中文分析器。创建步骤:第一步:把中文分析器添加到 solr 工。1、把 IKyzer2012FF_u1.jar 添加到 solr 工程的 lib 目录下2、把扩展词典、配置文件放到 solr 工程的 WEB-INF/classes 目录下。第二步:配置一个 FieldType,制定使用 IK修改 schema.xml 文件yzer修改 Solr 的 schema.xml 文件,添加 FieldType:第三步:配置业务域,type 制定使用自定义的 FieldType。设置业务系统 Field第四步:重启 tomcat2
5、.3. 搜索服务工程搭建可以参考 taotao-manager 创建。Taotao-search(聚合工程 pom)|-taotao-search-erface(jar)|-taotao-search-Service(war)目录结构如下:2.3.1. taotao-search打包方式为 pom.pom.xml4.0.0com.taotaotaotao-parent0.0.1-SNAPSHOTcom.taotaotaotao-search0.0.1-SNAPSHOTpomtaotao-search-erfacetaotao-search-service2.3.2. taotao-search
6、-打包方式为 jarerface.apache.tomcat.maventomcat7-maven-plugin8084/pom.xml2.3.3. taotao-search-service4.0.0com.taotaotaotao-search0.0.1-SNAPSHOTtaotao-search-erfacecom.taotaotaotao-managojo0.0.1-SNAPSHOTcom.taotaomon0.0.1-SNAPSHOTpom.xml4.0.0com.taotaotaotao-search0.0.1-SNAPSHOTtaotao-search-servicewarcom
7、.alibabadubbo.springframeworkspring.jtynetty.apache.zookeeperzookeepercom.sgroschupfzkcntcom.taotaotaotao-search-erface0.0.1-SNAPSHOT.springframeworkspring-context.springframeworkspring-beans.springframeworkspring-webmvc.springframeworkspring-jdbc.springframeworkspring-aspects配置文件:.springframeworksp
8、ring-jms.springframeworkspring-context-support.apache.solrsolr-solrj.apache.maven.pluginsmaven-surefire-plugin2.12.4trueapplicationContext-.xml:由于搜索的数据涉及到 3,所以 mapper 接口及,需要自己定义 mapper,而 mapper 的使用,只在搜索服务工文件需要放在 taotao-search-service 工。第三步:创建一个文档对象 SolrInput对象。第四步:向文档中添加域。必须有 id 域,域的名称必须在 schema.xml
9、 中定义。第五步:把文档添加到索引库中。第六步:提交。3.2. 删除文档3.2.1. 根据 id 删除第一步:创建一个 SolrServer 对象。第二步:调用 SolrServer 对象的根据 id 删除的方法。第三步:提交。3.2.2. 根据查询删除Testpublic void deleteByQuery() throws Exception SolrServer solrServer = new HttpSolrServer(); solrServer.deleteByQuery(title:change.me);Testpublic void deleteById() throws
10、Exception / 第一步:创建一个SolrServer 对象。SolrServer solrServer = new HttpSolrServer();/ 第二步:调用SolrServer 对象的根据 id 删除的方法。solrServer.deleteById(1);/ 第三步:提交。mit();Testpublic void add() throws Exception / 第一步:把solrJ 的jar 包添加到工。/ 第二步:创建一个SolrServer,使用HttpSolrServer 创建对象。SolrServer solrServer = new HttpSolrServe
11、r();/ 第三步:创建一个文档对象SolrInput对象。SolrInput= new SolrInput();/ 第四步:向文档中添加域。必须有 id 域,域的名称必须在schema.xml 中定义。.addField(id, test001);.addField(item_title, 测试商品);.addField(item_price, 199);/ 第五步:把文档添加到索引库中。solrServer.add();/ 第六步:提交。mit();3.3. 查询索引库查询步骤:第一步:创建一个 SolrServer 对象第二步:创建一个 SolrQuery 对象。第三步:向 SolrQu
12、ery 中添加查询条件、过滤条件。第四步:执行查询。得到一个 Response 对象。第五步:取查询结果。第六步:遍历结果并打印。3.3.1.简单查询3.3.2.带显示Testpublic void queryWithHighLighting() throws Exception / 第一步:创建一个SolrServer 对象SolrServer solrServer = new HttpSolrServer();/ 第二步:创建一个SolrQuery 对象。SolrQuery query = new SolrQuery();Testpublic void query() throws Exc
13、eption / 第一步:创建一个SolrServer 对象SolrServer solrServer = new HttpSolrServer();/ 第二步:创建一个SolrQuery 对象。SolrQuery query = new SolrQuery();/ 第三步:向SolrQuery 中添加查询条件、过滤条件。query.setQuery(*:*);/ 第四步:执行查询。得到一个 Response 对象。QueryResponse response = solrServer.query(query);/ 第五步:取查询结果。SolrList solrList = response.
14、getResults();System.out.prln(查询结果的录数: + solrList.getNumFound();/ 第六步:遍历结果并打印。for (Solrsolr: solrList) System.out.prln(solr.get(id); System.out.prln(solr.get(item_title); System.out.prln(solr.get(item_price);mit();4. 把商品数据导入到索引库中4.1. 功能分析在 schema.xml 中定义以下业务域(已经定义好):商品 Id商品标题商品卖点商品价格商品/ 第三步:向SolrQuer
15、y 中添加查询条件、过滤条件。query.setQuery(测试);/指定默认搜索域query.set(df, item_keywords);/开启显示query.setHighlight(true);/显示的域 query.addHighlightField(item_title); query.setHighlightSimplePre();query.setHighlightSimplet();/ 第四步:执行查询。得到一个 Response 对象。QueryResponse response = solrServer.query(query);/ 第五步:取查询结果。SolrList
16、solrList = response.getResults();System.out.prln(查询结果的录数: + solrList.getNumFound();/ 第六步:遍历结果并打印。for (Solrsolr: solrList) System.out.prln(solr.get(id);System.out.prln(itemTitle);System.out.prln(solr.get(item_price);/取显示MapString, MapString, List highlighting = response.getHighlighting();List list =
17、highlighting.get(solrString itemTitle = null;if (list != null & list.size() 0) itemTitle = list.get(0); else .get(id).get(item_title);itemTitle = (String) solr.get(item_title);分类名称商品描述需要从 tb_item, tb_item_cat, tb_item_desc 表中查询数据。Sql 语句:SELECTid, a.title,a.sell_po, a.price, a.image,NAME category_nam
18、e, c.item_descFROMtb_item a, tb_item_cat b, tb_item_desc cWHEREa.cid = b.id AND a.id = c.item_id AND a.sus = 1;应该在 taotao-manager-web 工调用导入索引库的服务。即可将数据从数据库中导入到索引库)。业务逻辑:点击按钮,表现层调用服务层的工程的导入索引库的方法服务层实现 调用 mapper 接口的方法查询所有的商品的数据3.将数据一条条添加到 solrinput 4.将文档添加到索引库中5.提交,并返回导入成功即可文档中4.2.层4.2.1. 创建POJO创建以下 P
19、OJO 用于存放查询到的用于搜索的商品的数据 并放入mon 中packagemon.pojo;import java.io.Serializable;/*在 taotao-manager-web系统中 做一个导入索引库的功能界面(例个按钮,点击搜索的商品数据POJOtitle SearchItem.javadescriptioncompany:author ljh* ver1.0*/public class SearchItem implements Serializable privaong id;/商品的idprivate String title;/商品标题 private String
20、 sell_po;/商品卖点 privaong price;/价格private String image;/商品的路径 private String category_name;/商品分类名称 private String item_desc;/商品的描述public Long getId() return id;public void setId(Long id) this.id = id;public String getTitle() return title;public void setTitle(String title) this.title = title;public St
21、ring getSell_po() return sell_po;public void setSell_po(String sell_po) thil_po= sell_po;public Long getPrice() return price;public void setPrice(Long price) this.price = price;public String getImage() return image;public void setImage(String image) 4.2.2. 定义mapper 接口接口定义:放入 taotao-search-service 工。
22、4.2.3. Mapper文件 SELECTa.id, a.title, a.sell_po, a.price, a.image,b. NAME category_name, c.item_descFROMtb_item a,publicerfaearchItemMapper public List getSearchItemList();this.image = image;public String getCategory_name() return category_name;public void setCategory_name(String category_name) this.
23、category_name = category_name;public String getItem_desc() return item_desc;public void setItem_desc(String item_desc) this.item_desc = item_desc;4.3. Service 层参数:无业务逻辑:taotao-search 中实现1、查询所有商品数据。2、创建一个SolrServer 对象。3、为每个商品创建一个 SolrInput 4、为文档添加域5、向索引库中添加文档。6、返回 TaotaoResult。对象。4.3.1. 创建application
24、Context-solr.xml在applicationContext-solr.xml 中配置 SolrServer 对象。tb_item_cat b, tb_item_desc cWHEREa.cid = b.id AND a.id = c.item_id AND a.sus = 1注意:以上黄色部分地址改成自己的 solr 所在的 IP 和端口4.3.2. 定义 service 接口接口放在 taotao-search-erface 中4.3.3. service 实现类Servicepublic class SearchItemServiceImpl implements Search
25、ItemService Autowiredprivate SearchItemMapper searchItemMapper; Autowiredprivate SolrServer solrServer;Overridepublic TaotaoResult importAllItems() throws Exception/ 1、查询所有商品数据。List itemList = searchItemMapper.getSearchItemList();/ 2、创建一个SolrServer 对象。/ 3、为每个商品创建一个SolrInput对象。for (SearchItem searchI
26、tem : itemList) SolrInput= new SolrInput();/ 4、为文档添加域.addField(id, searchItem.getId();.addField(item_title, searchItem.getTitle();.addField(item_sell_po, searchItem.getSell_po();package com.taotao.search.service;importmon.pojo.TaotaoResult;publicerfaearchItemService /*导入所有的商品数据到索引库中returnthrows Exce
27、ption*/public TaotaoResult importAllItems() throws Exception;4.3.4. 在 taotao-search-service 中发布服务4.4. 表现层4.4.1.服务在 taotao-manager-web 中服务。4.4.2. 添加 taotao-search-erface 的依赖在 taotao-manager-web 工的 pom.xml 中添加如下:.addField(item_price, searchItem.getPrice();.addField(item_image, searchItem.getImage();.a
28、ddField(item_category_name, searchItem.getCategory_name();.addField(item_desc, searchItem.getItem_desc();/ 5、向索引库中添加文档。 solrServer.add();/提交修改mit();/ 6、返回TaotaoResult。return TaotaoResult.ok();4.4.3. Controller请求的 url:/index/importall参数:无返回值:json 数据。TaotaoResult。4.4.4. 创建 JSP在 taotao-manager-web 中创建一
29、个 jsp:Controllerpublic class SearchItemController Autowiredprivate SearchItemServiearchItemService;RequestMap(/index/importall) ResponseBodypublic TaotaoResult importAllItems() try TaotaoResult result = searchItemService.importAllItems();return result; catch (Exception e) e.prStackTrace();return Tao
30、taoResult.build(500, 导入数据失败);4.4.5. 修改:index.jsp添加如下代码到 index.jsp 中:索引管理导入索引库一键导入商品数据到索引库function importAll() $.t(/index/importall,null,function(data)if (data.sus=200) $.messager.alert(提示,商品数据导入成功!); else $.messager.alert(提示,商品数据导入失败!););4.4.6. 测试测试出错,原因是未将工的文件发布到 classpath 中。在 taotao-search-service
31、 工程的 pom 文件中添加如下配置:还需要在 taotao-manager-service 中加入依赖:junitjunittestsrc/main/java*/*.properties*/*.xmlfalsesrc/main/resour*/*.properties*/*.xmlfalse5. 商品搜索功能实现5.1. 搜索表现层工程搭建搜索结果页面的展示。可以参考 taotao-manager-web 创建。打包方式 war。Taotao-search-web.mybatismybatis.mybatismybatis-springcom.pagehelperpagehelper-con
32、nector-javacom.alibabadruid5.1.1. Pom 文件4.0.0com.taotaotaotao-parent0.0.1-SNAPSHOTcom.taotaotaotao-search-web0.0.1-SNAPSHOTwarcom.taotaomon0.0.1-SNAPSHOTcom.taotaotaotao-search-erface0.0.1-SNAPSHOTcom.alibabadubbo.springframeworkspring.jtynetty.apache.zookeeperzookeepercom.sgroschupfzkcnt.springfram
33、eworkspring-context.springframeworkspring-beans.springframeworkspring-webmvc.springframeworkspring-jdbc.springframeworkspring-aspects.springframeworkspring-jms.springframeworkspring-context-supportjstljstljavax.servletservlet-apiprovidedjavax.servletjsp-apiprovidedjunitjunittest5.1.2.配置文件5.1.3. spri
34、ngmvc.xmlbeans xmlns=xmlns:xsi=xmlns:p=xmlns:context=xmlns:dubbo=/schema/dubboxmlns:mvc=.apache.tomcat.maventomcat7-maven-plugin8085/黄色部分注意改成自己的 IP 地址和端口5.1.4. web.xmltaotao-search-webindex.jspCharacterEncodingFilterxsi:schemaLocation=/schema/dubbo/schema/dubbo/dubbo.xsd!- -5.1.5. 静态页面将静态页面放入到 taota
35、o-search-web 工.springframework.web.filter.CharacterEncodingFilterencodingutf-8CharacterEncodingFilter/*taotao-search-web.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/springmvc.xml1taotao-search-web*.html5.2. 搜索功能分析在首页的搜索框中输入搜索条件, 然后跳转到搜索结果页面。搜索结果页面在taotao-search
36、-web 工。首页搜索框的点击按钮处理函数在:首页的 JS 中,应当改成 8085,如图:请求的 url:/search参数:1、q 查询条件。2、page 页码。默认为 1每页显示多少行 在 controller 中写死即可。比如:60返回值: String。(商品的列表信息,总页数 ,录数,数据回显)业务逻辑:1、接收查询条件。2、创建一个 SolrServer 对象,需要注入。3、创建一个 SolrQuery 对象。4、需要设置查询条件、分页条件、设置默认搜索域、设置。5、执行查询,返回 QueryResponse 对象。6、取返回结果,封装到 List中。7、返回查询结果的录数,计算查
37、询结果的总页数。8、得到查询结果,渲染 jsp.5.3.层5.3.1. 功能分析索引库的类。定义一些通用的数据业务逻辑就是查询索引库。参数:SolrQuery 对象业务逻辑:1、根据 Query 对象进行查询。方法。2、返回查询结果。包括 List、查询结果的录数。需要把返回结果封装到 pojo 中,至少包含两个属性:List、Long recordCount再包含一个总页数。创建如下 SearchResult 对象,放入mon 中5.3.2. 创建Search由于搜索功能只在搜索工用到,可以不写接口,只写类。返回值:SearchResultReitorypublic class Search
38、Autowiredprivate SolrServer solrServer;public SearchResult search(SolrQuery query) throws Exception /根据query 对象查询索引库QueryResponse response = solrServer.query(query);/取商品列表SolrList solrList = response.getResults();/商品列表List itemList = new ArrayList();public class SearchResult implements Serializable
39、privaist itemList;/搜索结果列表privaong recordCount;/录数privaong pageCount;/总页数5.4. Service 层5.4.1. 功能分析参数:queryString:查询条件 Page:页码 Rows:每页显示的业务逻辑:1、创建一个 SolrQuery 对象。2、设置查询条件3、设置分页条件4、需要指定默认搜索域。5、设置数。for (Solrsolr: solrList) SearchItem item = new SearchItem(); item.setId(String) solr.get(id);item.setCateg
40、ory_name(String) solr.get(item_category_name); item.setImage(String) solr.get(item_image); item.setPrice(long) solr.get(item_price);item.setSell_po(String) solr.get(item_sell_po);/取显示MapString, MapString, List highlighting = response.getHighlighting(); List list = highlighting.get(solr.get(id).get(i
41、tem_title); String itemTitle = ;/有显示的内容时。if (list != null & list.size() 0) itemTitle = list.get(0); else itemTitle = (String) solr.get(item_title);item.setTitle(itemTitle);/添加到商品列表 itemList.add(item);SearchResult result = new SearchResult();/商品列表 result.setItemList(itemList);/录数result.setRecordCount
42、(solrList.getNumFound();return result;6、执行查询,调用 Search 7、需要计算总页数。8、返回 SearchResult。得到 SearchResult5.4.2. 定义 service 接口接口已存在,只需要添加方法即可。5.4.3. 定义 service 接口实现类Servicepublic class SearchServiceImpl implements SearchService Autowiredprivate Searchsearch;Overridepublic SearchResult search(String queryStr
43、ing,page,rows) throws Exception / 1、创建一个SolrQuery 对象。SolrQuery query = new SolrQuery();/ 2、设置查询条件 query.setQuery(queryString);/ 3、设置分页条件 query.setStart( 1) * rows); query.setRows(rows);/ 4、需要指定默认搜索域。 query.set(df, item_title);/ 5、设置 query.setHighlight(true);query.addHighlightField(item_title);query.setHighlightSimplePre();publicerfaearchService /*导入数据到索引库return*/public void importAllItems() th
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初级会计实务-《初级会计实务》模考试卷954
- 基于干扰噪声协方差矩阵重构的稳健波束形成算法研究
- 安全防范与电信诈骗应对
- 现代农业产业园发展与建设综合方案
- 科创孵化器项目商业计划书
- 光伏组件回收产业未来机遇与发展报告
- 文化传媒行业编导培训总结
- 2025版高端石材工程采购及售后服务合同协议3篇
- 二零二五年度个人汽车维修贷款合同范本4篇
- 二零二五年度公益广告宣传海报设计与制作合同3篇
- JJG 705-2014液相色谱仪行业标准
- 地雷基本知识课件
- 五年级上册小数除法竖式计算练习200题及答案
- 人教版五年级上册数学简便计算大全500题及答案
- 创新创业教育课程体系
- 包装品质彩盒外箱知识课件
- 神经外科课件:神经外科急重症
- 颈复康腰痛宁产品知识课件
- 2024年低压电工证理论考试题库及答案
- 《民航服务沟通技巧》教案第14课民航服务人员上行沟通的技巧
- MT/T 538-1996煤钻杆
评论
0/150
提交评论