day73_淘淘商城项目_06_solr索引库搭建 + solr搜索功能实现 + 图片显示等问题解决_匠心笔记

文章目录 1、搜索工程的搭建 1.1、Solr服务搭建 1.2、配置业务域 1.3、搜索服务层工程的搭建 2、测试使用solrJ管理索引库 2.1、通过SolrJ向索引库中添加/更新索引 2.2、通过SolrJ从索引库中删除索引 2.3、通过SolrJ从索引库中查询索引 3、把商品数据导入到索引库中(后台功能) 3.1、功能分析 3.2、Dao层 3.3、Service层 3.4、表现层 4、商品搜索功能实现(前台功能) 4.1、搜索表现层工程的搭建 4.2、搜索功能分析 4.3、Dao层 4.4、Service层 4.5、表现层 4.6、图片显示处理 课程计划 第六天: 1、搜索工程的搭建 2、linux下solr服务的搭建 3、测试使用solrJ管理索引库 4、把商品数据导入到索引库中(后台功能) 5、商品搜索功能实现(前台功能) 回到顶部 1、搜索工程的搭建 要实现搜索功能,需要搭建solr服务、搜索服务工程、搜索系统(表现层的工程) 1.1、Solr服务搭建 1.1.1、solr的环境   solr是java开发的。   solr的安装文件。   推荐在Linux环境下使用Solr,需要安装环境Linux。   需要安装jdk。参考链接:https://www.cnblogs.com/chenmingjun/p/9931593.html   需要安装tomcat。 1.1.2、solr的搭建步骤 第一步:使用SecureCRT的SFTP功能,把solr-4.10.3.tgz.tgz的压缩包上传到Linux系统。 第二步:解压缩solr后,删除该安装包。 [root@itheima ~]# tar -zxvf solr-4.10.3.tgz.tgz ...... ...... [root@itheima ~]# ll 总用量 146496 drwxr-xr-x. 8 root root 218 11月 20 17:07 solr-4.10.3 -rw-r--r--. 1 root root 150010621 9月 26 23:16 solr-4.10.3.tgz.tgz [root@itheima ~]# rm -rf solr-4.10.3.tgz.tgz [root@itheima ~]# ll 总用量 0 drwxr-xr-x. 8 root root 218 11月 20 17:07 solr-4.10.3 [root@itheima ~]# 第三步:使用SecureCRT的SFTP功能,把apache-tomcat-7.0.47.tar.gz的压缩包上传到Linux系统,解压后删除安装包。 [root@itheima ~]# tar -zxvf apache-tomcat-7.0.47.tar.gz [root@itheima ~]# # rm -rf apache-tomcat-7.0.47.tar.gz 第四步:创建solr存放的目录,复制apache-tomcat-7.0.47目录到/usr/local/solr/tomcat目录下 [root@itheima ~]# mkdir /usr/local/solr [root@itheima ~]# cp -r apache-tomcat-7.0.47/ /usr/local/solr/tomcat 第五步:把solr-4.10.3/dist/solr-4.10.3.war文件部署(复制)到tomcat目录下,并重命名为solr.war。 [root@itheima ~]# cp solr-4.10.3/dist/solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war 注意:复制目录(文件夹)的时候需要加-r,复制文件的时候不需要加-r。 第六步:解压缩solr.war包。启动tomcat即可自动解压war包,并查看tomcat启动日志。 [root@itheima ~]# cd /usr/local/solr/tomcat/bin/ [root@itheima bin]# ./startup.sh Using CATALINA_BASE: /usr/local/solr/tomcat Using CATALINA_HOME: /usr/local/solr/tomcat Using CATALINA_TMPDIR: /usr/local/solr/tomcat/temp Using JRE_HOME: /usr/local/java/jdk1.7.0_80/jre Using CLASSPATH: /usr/local/solr/tomcat/bin/bootstrap.jar:/usr/local/solr/tomcat/bin/tomcat-juli.jar [root@itheima bin]# cd .. [root@itheima tomcat]# tail -f logs/catalina.out 十一月 20, 2018 5:23:24 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory /usr/local/solr/tomcat/webapps/host-manager 十一月 20, 2018 5:23:25 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory /usr/local/solr/tomcat/webapps/manager 十一月 20, 2018 5:23:25 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-8080"] 十一月 20, 2018 5:23:25 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8009"] 十一月 20, 2018 5:23:25 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 11144 ms 第六步:关闭tomcat后,删除掉没用的solr.war包。原则:没用的东西及时删掉。 注意:要是想删掉没用的solr.war包,必须在关闭tomcat的情况下,否则解压缩后的solr包也会一并删除掉。 [root@itheima tomcat]# bin/shutdown.sh Using CATALINA_BASE: /usr/local/solr/tomcat Using CATALINA_HOME: /usr/local/solr/tomcat Using CATALINA_TMPDIR: /usr/local/solr/tomcat/temp Using JRE_HOME: /usr/local/java/jdk1.7.0_80/jre Using CLASSPATH: /usr/local/solr/tomcat/bin/bootstrap.jar:/usr/local/solr/tomcat/bin/tomcat-juli.jar [root@itheima tomcat]# rm -rf webapps/solr.war 第七步:想要启动solr工程,还需要添加solr的扩展服务包。 把/root/solr-4.10.3/example/lib/ext目录下的所有的jar包,添加到solr工程中。 [root@itheima ext]# pwd /root/solr-4.10.3/example/lib/ext [root@itheima ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/ 第八步:创建一个solrhome目录。 我们知道:/root/solr-4.10.3/example/solr目录就是一个solrhome目录。 复制此目录中所有内容到/usr/local/solr/solrhome目录下 [root@itheima example]# pwd /root/solr-4.10.3/example [root@itheima example]# cp -r solr /usr/local/solr/solrhome 第九步:关联solr工程及solrhome。需要修改solr工程的web.xml文件。 [root@itheima ~]# cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/ [root@itheima WEB-INF]# vim web.xml 修改如下图所示: 第九步:再次启动tomcat [root@itheima ~]# cd /usr/local/solr/tomcat/ [root@itheima tomcat]# bin/startup.sh 第十步:修改防火墙配置 CentOS 7.X 默认的防火墙不是iptables,而是firewalld。我们可以试一下systemctl stop firewalld关闭防火墙,但是不推荐该方式。 CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables修改配置即可。 本博主的是CentOS7,防火墙使用的是firewalld,我们使用命令的方式来添加端口(修改后需要重启firewalld服务): [root@itheima ~]# cd /etc/firewalld/zones/ [root@itheima zones]# firewall-cmd --permanent --add-port=8080/tcp success [root@itheima zones]# service firewalld restart Redirecting to /bin/systemctl restart firewalld.service [root@itheima zones]# 第十一步:测试连接 访问地址:http://192.168.25.154:8080/solr/ 其实和在windows下的配置完全一样。 浏览器界面如下: 点击按钮“collection1” 1.1.3、solr的使用 添加文档时必须有id域,其他域必须在solr的schema.xml中进行定义。 1.2、配置业务域 1.2.1、在schema.xml中需要定义以下字段   1、商品id(根据id查询商品描述页)   2、商品标题title   3、商品卖点sell_point   4、商品价格price   5、商品图片image   6、分类名称category_name(不是分类id,我们一般不会根据商品分类id去查询商品,而是根据商品分类名称去查)   7、商品描述item_desc(实际开发中不需要搜索商品描述)   一共涉及到三张表:tb_item、item_cat、item_desc。   创建对应的业务域。同时需要指定中文分析器。 1.2.2、创建业务域步骤 第一步:把中文分析器添加到solr工程中。   0、把文件夹IK Analyzer 2012FF_hf1上传至linux中。   1、把IKAnalyzer2012FF_u1.jar拷贝到solr工程的lib目录下。   2、把扩展词词典、停用词字典、配置文件拷贝到solr工程的WEB-INF/classes目录下。(没有classes目录就先创建该目录) [root@itheima IK Analyzer 2012FF_hf1]# pwd /root/IK Analyzer 2012FF_hf1 [root@itheima IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/ [root@itheima IK Analyzer 2012FF_hf1]# mkdir /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes [root@itheima IK Analyzer 2012FF_hf1]# cp mydict.dic ext_stopword.dic IKAnalyzer.cfg.xml /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes [root@itheima IK Analyzer 2012FF_hf1]# ll /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes 总用量 12 -rw-r--r--. 1 root root 168 11月 20 19:29 ext_stopword.dic -rw-r--r--. 1 root root 419 11月 20 19:29 IKAnalyzer.cfg.xml -rw-r--r--. 1 root root 34 11月 20 19:29 mydict.dic [root@itheima IK Analyzer 2012FF_hf1]# 第二步:配置一个自定义的fieldType,使用指定的中文分词器IKAnalyzer。   修改solr工程下的schema.xml文件,在文件末尾添加一个自定义的fieldType,注意:要在标签里面添加。 [root@itheima conf]# pwd /usr/local/solr/solrhome/collection1/conf [root@itheima conf]# vim schema.xml   添加内容如下: 第三步:配置业务域,type指定使用自定义的fieldType。   设置业务系统的field ...... ......   注意:分类名称是不分词只建立索引。商品描述是分词但是不存储。 第四步:重启tomcat,测试我们自定义的业务域是否好使。 测试结果如下: 1.3、搜索服务层工程的搭建 1.3.1、搜索服务工程的创建可以参考taotao-content的创建 taotao-search(聚合工程pom)   |--taotao-search-interface(jar)   |--taotao-search-Service(war) 这里不再赘图了。 目录结构如下: 1.3.2、pom.xml的配置可以参考taotao-content的配置 /taotao-search/pom.xml 4.0.0 com.taotao taotao-parent 0.0.1-SNAPSHOT taotao-search pom taotao-search-interface taotao-search-service com.taotao taotao-common 0.0.1-SNAPSHOT org.apache.tomcat.maven tomcat7-maven-plugin 8084 / /taotao-search-interface/pom.xml 4.0.0 com.taotao taotao-search 0.0.1-SNAPSHOT taotao-search-interface com.taotao taotao-manager-pojo 0.0.1-SNAPSHOT /taotao-search-service/pom.xml 4.0.0 com.taotao taotao-search 0.0.1-SNAPSHOT taotao-search-service war com.taotao taotao-manager-dao 0.0.1-SNAPSHOT com.taotao taotao-search-interface 0.0.1-SNAPSHOT org.springframework spring-context org.springframework spring-beans org.springframework spring-webmvc org.springframework spring-jdbc org.springframework spring-aspects org.springframework spring-jms org.springframework spring-context-support com.alibaba dubbo spring org.springframework netty org.jboss.netty org.apache.zookeeper zookeeper com.github.sgroschupf zkclient 1.3.3、框架整合的配置文件可以参考taotao-content-service的配置 由于搜索的数据涉及到3张表,所以需要自己定义mapper。 而mapper的使用,只在搜索服务工程中,所以mapper接口及映射文件需要放在taotao-search-service工程中。 applicationContext-dao.xml:
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信