创建搜索系统taotao-search
导入依赖
<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.taotao.parent</groupId>
<artifactId>taotao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.taotao.search</groupId>
<artifactId>taotao-search</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<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>
</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>
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>taotao-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>taotao-search</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/taotao-search-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-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>
导入界面
Solr和Spring的整合
配置nginx和hosts
配置nginx:
配置启动tomcat:
Controller
package com.taotao.search.controller;
import java.io.UnsupportedEncodingException;
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.taotao.search.bean.SearchResult;
import com.taotao.search.service.ItemSearchService;
@Controller
public class SearchController {
public static final Integer ROWS = 5;
@Autowired
private ItemSearchService itemSearchService;
@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");
// 搜索关键字
mv.addObject("query", keyWords);
SearchResult searchResult = null;
try {
searchResult = this.itemSearchService.search(keyWords, page, ROWS);
} catch (Exception e) {
e.printStackTrace();
searchResult = new SearchResult(0L, null);
}
// 搜索结果数据
mv.addObject("itemList", searchResult.getList());
// 页数
mv.addObject("page", page);
int total = searchResult.getTotal().intValue();
int pages = total % ROWS == 0 ? total / ROWS : total / ROWS + 1;
// 总页数
mv.addObject("pages", pages);
return mv;
}
}
Service
package com.taotao.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.taotao.search.bean.Item;
import com.taotao.search.bean.SearchResult;
@Service
public class ItemSearchService {
@Autowired
private HttpSolrServer httpSolrServer;
public SearchResult search(String keyWords, Integer page, Integer rows) 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);
}
}
SearchResult
package com.taotao.search.bean;
import java.util.List;
public class SearchResult {
private Long total;
private List<?> list;
public SearchResult() {
}
public SearchResult(Long total, List<?> list) {
this.total = total;
this.list = list;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}![这里写图片描述](https://img-blog.csdn.net/20180512224035957?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nja2V2aW5jeWg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
测试
分页JS逻辑
具体方法:
效果:
下一页的逻辑:
解决中文乱码问题
测试:
多词搜索的逻辑关系
测试: