实战项目商城(第八天)

本文介绍如何使用Apache Solr搭建全文搜索服务,包括Solr的安装配置、中文分词器配置、业务字段定义及索引库维护等内容,并通过Java程序实现商品数据的导入和搜索服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第七天内容复习:

redis集群。连接redis,使用jedis客户端。单机版吗,集群版。

业务逻辑中添加缓存,添加缓存不能影响正常的业务逻辑。

今天的内容:

商品搜索功能:

1,使用solr实现

2,搭建搜索服务层

3,使用portal调用搜索服务,实现商品搜索。

Solr实现全文搜索

solr是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。 
Solr是一个全文检索服务器,只需要进行配置就可以实现全文检索服务。

Solr的安装及配置

安装步骤:

第一步:安装linux,jdk,tomcat

1)先安装tomcat

tar -zxf apache-tomcat-7.0.47.tar.gz

mkdir /usr/local/solr

cp apache-tomcat-7.0.47 /usr/local/solr/tomcat -r

cd /usr/local/solr/

第二步:把solr的压缩包上传到服务器。并解压。

第三步:把/root/solr-4.10.3/dist/solr-4.10.3.war包部署到tomcat下,并改名为solr.war

[root@bogon dist]#cp solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war

第四步:解压war包。启动tomcat自动解压。关闭tomcat。删除solr.war

第五步:把root/solr-4.10.3/example/lib/ext目录下所有的jar包复制到solr工程中。

[root@bogon ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/

第六步:创建solrhome。solrhome是存放solr服务器所有配置文件的目录。

[root@bogon example]#cp -r solr /usr/local/solr/solrhome

第七步:告诉solr服务器solrhome的位置。

1)需要修改solr工程的web.xml文件。


第八步:启动tomcat

配置业务字段

1.在solr中默认没有中文分析器,需要手工配置。配置一个FieldType,在FieldType中指定中文分析器。

2.solr中的字段必须是先定义后使用

中文分析器的配置

第一步:使用IK-Analyzer文件夹。把分析器的文件夹上传到服务器。

第二步:需要把分析器的jar包添加到solr工程中。

1)cp IKAnaylyzer2012FF_ul.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/

第三步:需要把IKAnalyzer需要的扩展词典及停用次词词典,配置文件复制到solr工程的classpath

1)/usr/local/solr/tomcat/webapps/solr/WEB-INF/classes文件夹下面

2)cp IKAnalyzer.cfg.xml ext_stopword.dic mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes

注意:扩展词典及停用词词典的字符集必须是utf-8,不能使用windows记事本编辑。

第四步:配置fieldType。需要在solrhome/collection1/conf/schema.xml中配置。

技巧:使用vi,vim跳转到文档开头gg。跳转到文档结尾shift+g

<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
业务字段配置

业务字段判断标准:
1、在搜索时是否需要在此字段上进行搜索。例如:商品名称、商品的卖点、商品的描述
2、后续的业务是否需要用到此字段。例如:商品id。

需要用到的字段:
1、商品id
2、商品title
3、卖点
4、价格
5、商品图片
6、商品分类名称
7、商品描述

<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="item_price"  type="long" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category_name" type="string" indexed="true" stored="true" />
<field name="item_desc" type="text_ik" indexed="true" stored="false" />

<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>
<copyField source="item_desc" dest="item_keywords"/>

维护索引库

添加:添加一个json格式的文件就可以。

修改:在solr中没有update,只需要添加一个新的文档,要求文档id和被修改文档的id一致。原理是先删除后添加。

删除:使用xml格式。

删除两种方法:

1.根据id删除:

<delete>
<id>test001</id>
</delete>
<commit/>
2.根据查询删除:

<delete>
<query>*:*</query>
</delete>
<commit/>

solrJ客户端
需要添加依赖

<!-- solr客户端 -->
<dependency>
	<groupId>org.apache.solr</groupId>
	<artifactId>solr-solrj</artifactId>
</dependency>
使用solrj的使用

@Test
	public void testSolrJ() throws Exception{
		//创建一个链接
		SolrServer solrServer = new HttpSolrServer("http://192.168.126.133:8080/solr");
		//创建一个文档对象
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", "test001");
		document.addField("item_title", "测试商品3");
		document.addField("item_price", 123456);
		//把文档对象写入到索引
		solrServer.add(document);
		//提交
		solrServer.commit();
	}
	@Test
	public void deleteDocument() throws Exception{
		SolrServer solrServer = new HttpSolrServer("http://192.168.126.133:8080/solr");
		//solrServer.deleteById("test001");
		solrServer.deleteByQuery("*:*");
		solrServer.commit();
	}
把商品信息导入到索引库

使用java程序读取mysql数据库中的商品信息,然后创建solr文档对象,把商品信息写入索引库。

需要发布一个服务。

为了灵活的进行分布式部署需要创建一搜素的服务工程发布 搜素服务。Taotao-search。

系统架构

创建taotao-search工程


pom包:

需要依赖taotao-common工程

需要依赖的jar包:

spring的jar包,springmvc的jar包,solrj的jar包,mybatis的jar包

参考taotao-rest工程

<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</groupId>
    <artifactId>taotao-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.taotao</groupId>
  <artifactId>taotao-search</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<dependency>
  		<groupId>com.taotao</groupId>
  		<artifactId>taotao-manager-mapper</artifactId>
  		<version>0.0.1-SNAPSHOT</version>	
  	</dependency>
  	<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
			
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
				
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</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>
				<!-- solr客户端 -->
			<dependency>
				<groupId>org.apache.solr</groupId>
				<artifactId>solr-solrj</artifactId>
				
			</dependency>
  </dependencies>
   
  <!-- 配置插件 -->
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<configuration>
				<port>8083</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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="taotao" version="2.5">
	<display-name>taotao-search</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

<!-- 加载spring容器 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 解决post乱码 -->
<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
</filter>
<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- springmvc前端控制器 -->
<servlet>
	<servlet-name>taotao-rest</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的,如果不配置contextConfigLocation,springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/springmvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
	
</servlet>
<servlet-mapping>
	<servlet-name>taotao-search</servlet-name>
	<url-pattern>/search/*</url-pattern>
</servlet-mapping>


</web-app>

框架整合


导入商品数据

需要使用的表:

tb_item,tb_item_cat,tb_item_desc

查询的sql语句:

SELECT
	a.id,
	a.title,
	a.sell_point,
	a.price,
	b.`name` category_name,
	c.item_desc
FROM
	tb_item a
LEFT JOIN tb_item_cat b ON a.cid = b.id
LEFT JOIN tb_item_desc c ON a.id = c.item_id

Dao层

需要创建一个mapper接口和mapper映射文件。名称相同且在同一个目录下。

POJO

创建一个sql语句对应的pojo

public class Item {

	private String id;
	private String title;
	private String sell_point;
	private long price;
	private String image;
	private String category_name;
	private String item_des;

接口的定义

public interface ItemMapper {

	public List<Item> getItemList();
}

Mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.taotao.search.mapper.ItemMapper" >
	<select id="getItemList" resultType="com.taotao.search.pojo.Item">
		SELECT
			a.id,
			a.title,
			a.sell_point,
			a.price,
			b.`name` category_name,
			c.item_desc
		FROM
			tb_item a
		LEFT JOIN tb_item_cat b ON a.cid = b.id
		LEFT JOIN tb_item_desc c ON a.id = c.item_id
	</select>
</mapper>

Service层

功能:导入所有的商品数据。没有参数。返回结果TaotaoResult。从数据库中查询出所有的商品数据。创建一个SolrInputDocument对象,把对象写入索引库。

@Service
public class ItemServiceImpl implements ItemService{

	@Autowired
	private ItemMapper itemMapper;
	
	@Autowired
	private SolrServer solrService;
	@Override
	public TaotaoResult importAllItems() {
		try {
		List<Item> list = itemMapper.getItemList();
		for (Item item : list) {
			SolrInputDocument document = new SolrInputDocument();
			document.setField("id", item.getId());
			document.setField("item_title", item.getTitle());
			document.setField("item_sell_point", item.getSell_point());
			document.setField("item_price", item.getPrice());
			document.setField("item_image", item.getImage());
			document.setField("item_category_name",item.getCategory_name());
			document.setField("item_desc", item.getItem_des());
			
			solrService.add(document);
		}
		solrService.commit();
			} catch (Exception e) {
				
				e.printStackTrace();
				return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
			
		}
		return TaotaoResult.ok();
	}
applicationContext-solr.xml

<!-- 配置SolrServer对象 -->
	<!-- 单机版 -->
	<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
		<constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg>
	</bean>
Controller层
功能:发布一个rest形式的服务。调用Service的服务方法,把数据导入到索引库中,返回TaotaoResult。
Url:/search/manager/importall

@Controller
@RequestMapping("/manager")
public class ItemController {
	
	@Autowired
	private ItemService itemService;

	/**
	 * 导入商品数据到索引库
	 */
	@RequestMapping("/importall")
	@ResponseBody
	public TaotaoResult importAllItems() {
		TaotaoResult result = itemService.importAllItems();
		return result;
	}
}
修改pom文件,添加如下配置:
<build>	
	<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
	</build>

搜索服务发布

需求分析
http形式的服务。对外提供搜索服务是一个get形式的服务。调用此服务时需要查询条件,分页条件可以使用page(要显示第几页)、rows(每页显示的记录数)。返回一个json格式的数据。可以使用TaotaoResult包装一个商品列表转换成json。

请求的url:/search/query/{查询条件}/{page}/{rows}
           /search/query?q={查询条件}&page={page}&rows={rows}
返回的结果:TaotaoResult包装商品列表。

Dao层
分析:尽可能的做的通用一些。参数应该是SolrQuery。返回商品列表、查询结果总记录数
查询测试:

@Test
	public void queryDocument() throws Exception {
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
		//创建一个查询对象
		SolrQuery query = new SolrQuery();
		//设置查询条件
		query.setQuery("*:*");
		query.setStart(20);
		query.setRows(50);
		//执行查询
		QueryResponse response = solrServer.query(query);
		//取查询结果
		SolrDocumentList solrDocumentList = response.getResults();
		System.out.println("共查询到记录:" + solrDocumentList.getNumFound());
		for (SolrDocument solrDocument : solrDocumentList) {
			System.out.println(solrDocument.get("id"));
			System.out.println(solrDocument.get("item_title"));
			System.out.println(solrDocument.get("item_price"));
			System.out.println(solrDocument.get("item_image"));
			
		}
	}

返回结果pojo:

public class SearchResult {

	//商品列表
	private List<Item> itemList;
	//总记录数
	private long recordCount;
	//总页数
	private long pageCount;
	//当前页
	private long curPage;
}
@Repository
public class SearchDaoImpl implements SearchDao {
	
	@Autowired
	private SolrServer solrServer;

	@Override
	public SearchResult search(SolrQuery query) throws Exception {
		//返回值对象
		SearchResult result = new SearchResult();
		//根据查询条件查询索引库
		QueryResponse queryResponse = solrServer.query(query);
		//取查询结果
		SolrDocumentList solrDocumentList = queryResponse.getResults();
		//取查询结果总数量
		result.setRecordCount(solrDocumentList.getNumFound());
		//商品列表
		List<Item> itemList = new ArrayList<>();
		//取高亮显示
		Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
		//取商品列表
		for (SolrDocument solrDocument : solrDocumentList) {
			//创建一商品对象
			Item item = new Item();
			item.setId((String) solrDocument.get("id"));
			//取高亮显示的结果
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			String title = "";
			if (list != null && list.size()>0) {
				title = list.get(0);
			} else {
				title = (String) solrDocument.get("item_title");
			}
			item.setTitle(title);
			item.setImage((String) solrDocument.get("item_image"));
			item.setPrice((long) solrDocument.get("item_price"));
			item.setSell_point((String) solrDocument.get("item_sell_point"));
			item.setCategory_name((String) solrDocument.get("item_category_name"));
			//添加的商品列表
			itemList.add(item);
		}
		result.setItemList(itemList);
		return result;
	}

}

Service层

@Service
public class SearchServiceImpl implements SearchService{

	@Autowired
	private SearchDao searchDao;
	@Override
	public SearchResult search(String queryString, int page, int rows) throws Exception {
		//创建查询对象
		SolrQuery query = new SolrQuery();
		//设置查询条件
		query.setQuery(queryString);
		//设置分页
		query.setStart((page-1)*rows);
		query.setRows(rows);
		//设置默认搜索域
		query.set("fd", "item_keywords");
		//设置高亮显示
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\"color:red\">");
		query.setHighlightSimplePost("</em>");
		//执行查询
		SearchResult searchResult = searchDao.search(query);
		long recordCount = searchResult.getRecordCount();
		long pageCount = recordCount / rows;
		if(recordCount % rows >0){
			pageCount++;
		}
		searchResult.setPageCount(pageCount);
		searchResult.setCurPage(page);
		return searchResult;	
		
		
	}
Controller层

接收查询参数:查询条件、page、rows

调用Service执行查询返回一个查询结果对象。

把查询结果包装到TaotaoResult中返回,结果是json格式的数据。


如果查询条件为空,返回状态吗:400,消息:查询条件不能为空。

Page为空:默认为1

Rows为空:默认返回为60

@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;
	
	@RequestMapping(value="/query",method=RequestMethod.GET)
	@ResponseBody
	public TaotaoResult search(@RequestParam("q")String queryString,
							@RequestParam(defaultValue="1")Integer page,
							@RequestParam(defaultValue="60")Integer rows){
		//查询条件不能为空
		if(StringUtils.isEmpty(queryString)){
			return TaotaoResult.build(400, "查询条件不能为空");
		}
		SearchResult searchResult = null;
		try {
			queryString = new String(queryString.getBytes("iso8859-1"),"utf-8");
			searchResult = searchService.search(queryString, page, rows);
		} catch (Exception e) {
			
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}						
		return TaotaoResult.ok(searchResult);
	}
}

扫描dao配置:

<!-- 扫描包加载service实现类 -->
<context:component-scan base-package="com.taotao.search">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
解决get乱码问题:



















                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值