一、搭建maven工程基本框架
1、导入依赖
pom.xml文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.enjoyshop.parent</groupId>
<artifactId>enjoyshop-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.enjoyshop.search</groupId>
<artifactId>enjoyshop-search</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.enjoyshop.common</groupId>
<artifactId>enjoyshop-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.2</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8085</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>enjoyshop-search</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器,以UTF8编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC框架入口 -->
<servlet>
<servlet-name>enjoyshop-search</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/enjoyshop-search-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>enjoyshop-search</servlet-name>
<!-- 伪静态,好处:有利于SEO(搜索引擎优化) -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
3、使用Spring集成Solrj
本项目使用solr来实现搜索功能,并且使用其java客户端Solrj来实现项目集成。
关于Solr的简介和使用请参考这里。点我
关于Solrj的使用请参考这里。点我
Spring项目集成Solrj的配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg index="0" value="${solr.url}"/>
<!-- 设置响应解析器 -->
<property name="parser">
<bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/>
</property>
<!-- 重试次数 -->
<property name="maxRetries" value="${solr.maxRetries}"/>
<property name="connectionTimeout" value="${solr.connectionTimeout}"/>
</bean>
</beans>
4、配置host和nginx
这里不做详述,可参考之前的配置。
5、在Solr中创建enjoyshop core
创建enjoyshop core的步骤如下:
1、 在example目录下创建enjoyshop-solr文件夹;
2、 将example/solr下的solr.xml拷贝到enjoyshop-solr目录下;
3、 在enjoyshop-solr下创建enjoyshop目录,并且在enjoyshop目录下创建conf和data目录;
4、 将example\solr\collection1\core.properties文件拷贝到example\enjoyshop-solr\enjoyshop下,并且修改name=enjoyshop;
5、 将example\solr\collection1\conf下的schema.xml、solrconfig.xml拷贝到example\enjoyshop-solr\enjoyshop\conf下;
6、 修改schema.xml文件,使其配置最小化:
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="example" version="1.5">
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="_root_" type="string" indexed="true" stored="false"/>
<field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="text_ik" indexed="true" stored="true"/>
<field name="sellPoint" type="string" indexed="false" stored="true"/>
<field name="price" type="long" indexed="true" stored="true"/>
<field name="image" type="string" indexed="false" stored="true"/>
<field name="cid" type="long" indexed="true" stored="true"/>
<field name="status" type="int" indexed="true" stored="false"/>
<field name="updated" type="long" indexed="true" stored="true"/>
<uniqueKey>id</uniqueKey>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<solrQueryParser defaultOperator="AND"/>
</schema>
7、 修改solrconfig.xml文件,修改一些配置,大部分配置先保持默认:
a) 将所有的标签注释掉;
b) 搜索<str name="df">text</str>
并将其替换成<str name="df">title</str>
c) 将<searchComponent name="elevator" class="solr.QueryElevationComponent" >
注释掉(这个的功能类似百度的竞价排名);
8、 集成IK分词器
a) 将IKAnalyzer-2012-4x.jar拷贝到example\solr-webapp\webapp\WEB-INF\lib下;
b) 在schema.xml文件中添加fieldType:
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
9、 启动solr:
java -Dsolr.solr.home=enjoyshop-solr -jar start.jar
6、导入商品数据到solr中
首先需要启动后台系统,通过httpclient从后台系统的对外接口中获得商品数据。通过运行如下代码即可导入商品信息。
public class ItemDataImportTest {
private HttpSolrServer httpSolrServer;
private static final ObjectMapper MAPPER = new ObjectMapper();
@Before
public void setUp() throws Exception {
// 在url中指定core名称:enjoyshop
// http://solr.enjoyshop.com/#/enjoyshop -- 界面操作
String url = "http://solr.enjoyshop.com/enjoyshop"; // 服务地址
HttpSolrServer httpSolrServer = new HttpSolrServer(url); // 定义solr的server
httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间
this.httpSolrServer = httpSolrServer;
}
@Test
public void testData() throws Exception {
// 通过后台系统的接口查询商品数据
String url = "http://manage.enjoyshop.com/rest/item?page={page}&rows=100";
int page = 1;
int pageSzie = 0;
do {
String u = StringUtils.replace(url, "{page}", "" + page);
System.out.println(u);
String jsonData = doGet(u);
JsonNode jsonNode = MAPPER.readTree(jsonData);
String rowsStr = jsonNode.get("rows").toString();
List<Item> items = MAPPER.readValue(rowsStr,
MAPPER.getTypeFactory().constructCollectionType(List.class, Item.class));
pageSzie = items.size();
this.httpSolrServer.addBeans(items);
this.httpSolrServer.commit();
page++;
} while (pageSzie == 100);
}
private String doGet(String url) throws Exception {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建http GET请求
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
return null;
}
二、业务逻辑实现
1、搜索功能的实现
- service层
package com.enjoyshop.search.service;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.enjoyshop.search.bean.SearchResult;
import com.enjoyshop.search.pojo.Item;
@Service
public class SearchService {
public static final Integer ROWS = 32;
@Autowired
private HttpSolrServer httpSolrServer;
public SearchResult search(String keyWords, Integer page) throws Exception {
SolrQuery solrQuery = new SolrQuery(); // 构造搜索条件
solrQuery.setQuery("title:" + keyWords + " AND status:1"); // 搜索关键词
// 设置分页 start=0就是从0开始,,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
solrQuery.setStart((Math.max(page, 1) - 1) * ROWS);
solrQuery.setRows(ROWS);
// 是否需要高亮
boolean isHighlighting = !StringUtils.equals("*", keyWords) && StringUtils.isNotEmpty(keyWords);
if (isHighlighting) {
// 设置高亮
solrQuery.setHighlight(true); // 开启高亮组件
solrQuery.addHighlightField("title");// 高亮字段
solrQuery.setHighlightSimplePre("<em>");// 标记,高亮关键字前缀
solrQuery.setHighlightSimplePost("</em>");// 后缀
}
// 执行查询
QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
List<Item> items = queryResponse.getBeans(Item.class);
if (isHighlighting) {
// 将高亮的标题数据写回到数据对象中
Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
for (Item item : items) {
if (!highlighting.getKey().equals(item.getId().toString())) {
continue;
}
item.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
break;
}
}
}
return new SearchResult(queryResponse.getResults().getNumFound(), items);
}
}
- controller层
package com.enjoyshop.search.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.enjoyshop.search.bean.SearchResult;
import com.enjoyshop.search.service.SearchService;
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
@RequestMapping(value = "search", method = RequestMethod.GET)
public ModelAndView search(@RequestParam("q") String keyWords,
@RequestParam(value = "page", defaultValue = "1") Integer page) {
ModelAndView mv = new ModelAndView("search");
try {
//解决中文乱码问题
keyWords=new String(keyWords.getBytes("ISO-8859-1"),"UTF-8");
//搜索结果
SearchResult searchResult = this.searchService.search(keyWords, page);
//搜索关键字
mv.addObject("query", keyWords);
//商品列表
mv.addObject("itemList", searchResult.getData());
//当前页
mv.addObject("page", page);
//计算总页数
int total = searchResult.getTotal().intValue();
int pages = total % searchService.ROWS == 0 ? total / searchService.ROWS : total / searchService.ROWS + 1;
mv.addObject("pages", pages);
} catch (Exception e) {
e.printStackTrace();
}
return mv;
}
}
2、利用RabbitMQ接收后台的消息
- 使用Spring配置RabbitMQ
外部属性文件如下:
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=enjoyshop
rabbitmq.password=enjoyshop
rabbitmq.vhost=/enjoyshop
XML配置文件如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"
virtual-host="${rabbitmq.vhost}" />
<!-- 定义管理 -->
<rabbit:admin connection-factory="connectionFactory"/>
<!-- 定义队列 -->
<rabbit:queue name="enjoyshop-search-item-queue" durable="true" auto-declare="true"/>
<!-- 定义消费者 -->
<bean id="itemMQHandler" class="com.enjoyshop.search.mq.handler.ItemMQHandler"/>
<!-- 消费者监听队列 -->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="itemMQHandler" method="execute" queue-names="enjoyshop-search-item-queue"/>
</rabbit:listener-container>
</beans>
- 消息处理方法
package com.enjoyshop.search.mq.handler;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Autowired;
import com.enjoyshop.search.pojo.Item;
import com.enjoyshop.search.service.ItemService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ItemMQHandler {
private static final ObjectMapper MAPPER = new ObjectMapper();
@Autowired
private HttpSolrServer httpSolrServer;
@Autowired
private ItemService itemService;
public void execute(String msg) {
try {
JsonNode jsonNode = MAPPER.readTree(msg);
Long itemId = jsonNode.get("itemId").asLong();
String type = jsonNode.get("type").asText();
if (StringUtils.equals(type, "insert") || StringUtils.equals(type, "update")) {
// 从后台系统中查询商品数据
Item item = this.itemService.queryItemById(itemId);
if (item != null) {
this.httpSolrServer.addBean(item);
this.httpSolrServer.commit();
}
} else if (StringUtils.equals(type, "delete")) {
// 删除索引数据
this.httpSolrServer.deleteById(String.valueOf(itemId));
this.httpSolrServer.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}