SSM综合项目实战(TTSC) -- day09 Solr,搜索系统
2017-10-24 11:22
465 查看
一、solr服务的安装
1、为什么要使用solr服务
我们需要实现商品的查询功能,可以使用MySQL进行查询,但是MySQL的模糊查询(like)速度很慢,而且数据量越大,查询速度就越慢。所以数据量大的时候不会使用MySQL实现搜索功能,我们这里使用solr实现搜索功能。
2、安装solr
安装详见:http://blog.csdn.net/wingzhezhe/article/details/733686103、设置使用域名进行访问
(1)、使用switchhost修改host文件(2)、修改nginx.conf配置文件
4、最终测试效果
二、Solr配置和使用
1、配置IK分词器
(1)、上传jar包到 solr/WEB-INF/lib 下面(2)、上传核心配置文件和停止词扩展次配置文件到 solr/WEB-INF/classes 文件夹下
(3)、在 solrHome/collection1/conf/schema.xml 加入一下配置,使IK分词器生效
<!-- IKAnalyzer--> <fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> <!--IKAnalyzer Field--> <field name="content_ik" type="text_ik" indexed="true" stored="true" />
(4)、浏览器测试IK分词器
2、业务域的配置
(1)、在 sorlHome/collection1/conf/schema.xml中配置搜索商品的业务域字段<!-- 为收缩商品配置业务域 indexed="true":表示需要索引 stored="true":表示需要存储--> <!-- title字段需要存储索引以及分词 --> <field name="item_title" type="text_ik" indexed="true" stored="true" /> <!-- price字段需要存储索引不需要分词 --> <field name="item_price" type="long" indexed="true" stored="true" /> <!-- image字段不需要索引和分词,但是需要存储 --> <field name="item_image" type="string" indexed="false" stored="true" /> <!-- cid字段需要存储,但不需要索引以及分词 --> <field name="item_cid" type="long" indexed="false" stored="true" /> <!-- status字段需要索引,但是不需要存储以及分词 --> <field name="item_status" type="int" indexed="true" stored="false" />
(2)、重启tomcat,查看业务域是否生效
3、回顾solrJ的使用
(1)、创建打包方式为jar包的maven工程,继承taotao-parent(2)、创建单机版的solr操作的测试类
package cn.itcast.solr; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.Before; import org.junit.Test; /** * 单机版solr测试类 * * @author Administrator * */ public class SolrTest { // 声明连接对象 private HttpSolrServer solrServer; @Before public void init() { // 声明接口地址 String baseUrl = "http://solr.taotao.com/solr"; this.solrServer = new HttpSolrServer(baseUrl); } /** * 测试新增和更新 * * @throws Exception */ @Test public void testSaveAndUpdate() throws Exception { // 创建solrInputDocument对象,用来存放数据 SolrInputDocument doc = new SolrInputDocument(); // 设置唯一域id,如果id不存在,则执行新增,如果id存在,则执行更新 doc.addField("id", "id_007"); doc.addField("item_title", "第七次向solr索引中添加数据"); // 使用solrServler连接对象执行操作 this.solrServer.add(doc); // 提交事务 this.solrServer.commit(); } /** * 删除索引库 * * @throws Exception */ @Test public void testDelete() throws Exception { // 使用id删除 this.solrServer.deleteById("id_001"); // 删除全部(慎用) this.solrServer.deleteByQuery("*:*"); // 提交事务 this.solrServer.commit(); } @Test public void testQuery() throws Exception { // 创建查询对象 SolrQuery solrQuery = new SolrQuery(); // 设置查询语句 solrQuery.setQuery("item_title:solr"); // 设置分页数据 solrQuery.setStart(0); solrQuery.setRows(5); // 设置高亮数据 // 设置开启高亮 solrQuery.setHighlight(true); // 设置高亮字段 solrQuery.addHighlightField("item_title"); // 设置高亮的前缀 solrQuery.setHighlightSimplePre("<font color='red'>"); // 设置高粱的后缀 solrQuery.setHighlightSimplePost("</font>"); // 发起请求,获取response QueryResponse response = this.solrServer.query(solrQuery); // 获取高亮数据,高亮数据查出来的格式如下 // { // "s0001": {"item_title": ["今天开始使用<em>solr</em>"]}, // "s0002": {"item_title": ["今天开始使用<em>solr</em>实"]} // } Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); // 从response中获取结果集 SolrDocumentList solrDocumentList = response.getResults(); // 打印查询的数据条数 System.out.println("查询到的结果条数是:" + solrDocumentList.getNumFound()); // 解析结果集,打印 for (SolrDocument solrDocument : solrDocumentList) { // 获取高亮数据 List<String> list = highlighting.get(solrDocument.get("id")).get("item_title"); System.out.println("-----------------------------------------"); // 打印id System.out.println(solrDocument.get("id")); // 打印item_title System.out.println(solrDocument.get("item_title")); // 打印高亮字段 if (list != null && list.size() > 0) { System.out.println(list.get(0)); } } } }
三、Solr集群版
1、集群版安装
http://blog.csdn.net/wingzhezhe/article/details/734568852、测试solrJ连接集群
package cn.itcast.solr; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.Before; import org.junit.Test; /** * 测试solr集群版连接 * * @author Administrator * */ public class SolrCloudTest { // 声明连接对象 private CloudSolrServer cloudSolrServer; @Before public void init() { // 声明zookeeper的地址 String zkHost = "192.168.37.161:2281,192.168.37.161:2281,192.168.37.161:2381"; this.cloudSolrServer = new CloudSolrServer(zkHost); // 设置索引库的名字 this.cloudSolrServer.setDefaultCollection("collection2"); } /** * 测试新增和更新 * * @throws Exception */ @Test public void testSaveAndUpdate() throws Exception { // 创建solrInputDocument对象,用来存放数据 SolrInpu 4000 tDocument doc = new SolrInputDocument(); // 设置唯一域id,如果id不存在,则执行新增,如果id存在,则执行更新 doc.addField("id", "id_009"); doc.addField("item_title", "第七次向solr索引中添加数据"); // 使用solrServler连接对象执行操作 this.cloudSolrServer.add(doc); // 提交事务 this.cloudSolrServer.commit(); } /** * 删除索引库 * * @throws Exception */ @Test public void testDelete() throws Exception { // 使用id删除 this.cloudSolrServer.deleteById("id_008"); // 删除全部(慎用) //this.cloudSolrServer.deleteByQuery("*:*"); // 提交事务 this.cloudSolrServer.commit(); } @Test public void testQuery() throws Exception { // 创建查询对象 SolrQuery solrQuery = new SolrQuery(); // 设置查询语句 solrQuery.setQuery("item_title:solr"); // 设置分页数据 solrQuery.setStart(0); solrQuery.setRows(5); // 设置高亮数据 // 设置开启高亮 solrQuery.setHighlight(true); // 设置高亮字段 solrQuery.addHighlightField("item_title"); // 设置高亮的前缀 solrQuery.setHighlightSimplePre("<font color='red'>"); // 设置高粱的后缀 solrQuery.setHighlightSimplePost("</font>"); // 发起请求,获取response QueryResponse response = this.cloudSolrServer.query(solrQuery); // 获取高亮数据,高亮数据查出来的格式如下 // { // "s0001": {"item_title": ["今天开始使用<em>solr</em>"]}, // "s0002": {"item_title": ["今天开始使用<em>solr</em>实"]} // } Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); // 从response中获取结果集 SolrDocumentList solrDocumentList = response.getResults(); // 打印查询的数据条数 System.out.println("查询到的结果条数是:" + solrDocumentList.getNumFound()); // 解析结果集,打印 for (SolrDocument solrDocument : solrDocumentList) { // 获取高亮数据 List<String> list = highlighting.get(solrDocument.get("id")).get("item_title"); System.out.println("-----------------------------------------"); // 打印id System.out.println(solrDocument.get("id")); // 打印item_title System.out.println(solrDocument.get("item_title")); // 打印高亮字段 if (list != null && list.size() > 0) { System.out.println(list.get(0)); } } } }
四、创建搜索系统
1、架构分析
2、创建服务层
(1)、创建聚合父工程 taotao-search
(2)、创建父工程的子模块 taotao-search-interface、taotao-search-service
(3)、加入项目之间的依赖关系
taotao-search-interface 依赖 taotao-manager-pojo
taotao-search-service 依赖 taotao-manager-mapper 和 taotao-search-interface
(4)、加入tomcat插件
(5)、加入配置文件
注意:配置文件按照taotao-manager-service拷贝加入,改名,删除redis的配置,添加solr的配置
env.properties
#配置solr单机版的url solr.baseURL=http://solr.taotao.com/solr/ #配置solr集群版的连接地址 cloud.zkHost=192.168.37.161:2281,192.168.37.161:2281,192.168.37.161:2381 #配置solr集群版索引库的名字 cloud.colleciton=collection2
applicationContext-solr.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置单机版solr --> <bean class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <!-- 配置单机版的solr连接地址 --> <constructor-arg name="baseURL" value="${solr.baseURL}" /> </bean> <!-- 配置集群版solr服务 --> <bean class="org.apache.solr.client.solrj.impl.CloudSolrServer"> <!-- 配置zookeeper的集群地址 --> <constructor-arg name="zkHost" value="${cloud.zkHost}" /> <!-- 配置索引库名字 --> <porperty name="defaultCollection" value="${cloud.colleciton}" /> </bean> </beans>
3、创建表现层
(1)、创建独立的maven工程 taotao-search-web(2)、加入依赖关系
taotao-search-web 依赖 taotao-search-interface
(3)、加入tomcat插件
(3)、加入配置文件
注意:taotao-search-web配置文件参考taotao-portal进行配置
4、加入搜索系统的静态资源文件
5、配置域名访问
(1)、修改host文件(2)、修改nginx.xml配置文件
五、数据准备
1、导入数据库表
注意:由于之前已经在数据库中导入了tb_item和tb_item_desc表,需要先删除,然后将tb_item_jd修改为tb_item表,将tb_item_jd_desc修改为tb_item_desc表
2、使用nginx的虚拟主机功能搭建图片服务器
(1)、将资料中的图片资源解压到无中文无空格的目录下(2)、使用switchhost配置host文件中的图片服务器域名
(3)、在nginx.xml配置文件中配置图片服务器地址
3、重启nginx,启动项目,测试运行
4、将数据库中的商品数据导入到solr索引库中
(1)、在taotao-search-service项目中编写代码导入数据package com.taotao.search.test; import java.util.ArrayList; import java.util.List; import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.common.SolrInputDocument; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.github.pagehelper.PageHelper; import com.taotao.manager.mapper.ItemMapper; import com.taotao.manager.pojo.Item; /** * 将商品数据导入到solr索引库 * * @author Administrator * */ public class Item2SolrTest { // 集群连接对象 private CloudSolrServer cloudSolrServer; // 商品Mapper private ItemMapper itemMapper; @Before public void init() { // 获取spring容器 ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); // 从容器中获取连接对象和Mapper this.cloudSolrServer = app.getBean(CloudSolrServer.class); this.itemMapper = app.getBean(ItemMapper.class); } /** * 将数据库中的商品信息导入solr索引库 * * @throws Exception */ //@Ignore //该注解目的是不运行test测试类 @Test public void mysql2Solr() throws Exception { // 从MySQL中查询出商品数据,可以使用ItemMapper查询 // 一次查询所有数据可能会造成内存溢出,需要分页查询 int page = 1; int pageSize = 0; do { // 设置分页数据 PageHelper.startPage(page, 500); // 执行查询 List<Item> list = this.itemMapper.select(null); // 声明存放SolrInputDocument的容器 List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); // 遍历结果集,向solr索引库中添加数据 for (Item item : list) { // 把商品数据封装到SolrInputDocument中 SolrInputDocument doc = new SolrInputDocument(); // 商品id doc.addField("id", item.getId()); // 商品item_title doc.addField("item_title", item.getTitle()); // 商品item_price doc.addField("item_price", item.getPrice()); // 商品item_image doc.addField("item_image", item.getImage()); // 商品item_cid doc.addField("item_cid", item.getCid()); // 商品item_status doc.addField("item_status", item.getStatus()); docs.add(doc); } // 使用CloudSolrServer连接对象将数据保存到solr索引库中 this.cloudSolrServer.add(docs); // 提交 this.cloudSolrServer.commit(); page ++; //获取查询到的数据条数 pageSize = list.size(); } while (pageSize == 500); } }
(2)、运行测试类,访问solr集群
相关文章推荐
- SSM综合项目实战(TTSC) -- day12 购物车
- SSM综合项目实战(TTSC) -- day03 Nginx,类目选择,新增商品
- SSM综合项目实战(TTSC) -- day02 Dubbo注册中心,通用Mapper,分页插件
- SSM综合项目实战(TTSC) -- day13 订单、定时器Quartz
- SSM综合项目实战(TTSC) -- day05 搭建门户,内容管理,大广告位
- SSM综合项目实战(TTSC) -- day01 项目介绍、工程搭建
- SSM综合项目实战(TTSC) -- day14 项目部署、MyCat
- SSM综合项目实战(TTSC) -- day04 图片上传、商品列表
- SSM综合项目实战(TTSC) -- day07 RESTful,HttpClient,单点登录
- SSM综合项目实战(TTSC) -- day08 单点登录,注册登录,jsonp
- 实战篇:基于SSM的秒杀系统之项目介绍
- SSM项目实战(三)--- 高并发秒杀系统之Web层
- SSM实战——秒杀系统之创建项目、管理依赖、设计数据库
- SSH综合项目实战(快递) -- day09 收派业务、客户在线下单
- SSM项目实战(一)--- 高并发秒杀系统之DAO层
- 带你逐步深入了解SSM框架——淘淘商城项目之solr服务器搭建、搜索功能实现
- SSM项目实战(二)--高并发秒杀系统之Service层
- 【ssm个人博客项目实战06】博客类别的添加修改删除的实现
- Java SSM 商户管理系统 客户管理 库存管理 销售报表 项目源码