今日目标:
(1)完成solr环境安装、中文解析器和业务域的配置
(2)会使用Spring Data Solr完成增删改查操作
(3)完成批量数据导入
(4)完成按关键字搜索功能
(5)完成高亮显示关键字的功能
(6)完成更新索引库的功能
1、Solr环境搭建和配置
准备工作:安装Solr的环境以及solrhome的基础配置
1.1 在solrhome中的collction1下的conf目录中打开 schema.xml 配置业务域
<field name="item_goodsid" type="long" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="double" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category" type="string" indexed="true" stored="true" />
<field name="item_seller" type="text_ik" indexed="true" stored="true" />
<field name="item_brand" type="string" indexed="true" stored="true" />
1.2 在solrhome中的collction1下的conf目录中打开 schema.xml 配置复制域
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
1.3 在solrhome中的collction1下的conf目录中打开 schema.xml 配置动态域
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
2、批量导入数据到索引库
说明:批量导入数据到索引库这一操作,一般只执行一次,所以我们不需要将其放入后台系统中,我们单独建立一个模块打成jar包,让用户可以通过命令行执行这一操作即可
准备工作:搭建单独的模块,名称:pinyougou-solr-util
2.1 导入商品数据到索引库
(1)导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>pinyougou-parent</artifactId>
<groupId>com.pinyougou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pinyougou-solr-util</artifactId>
<dependencies>
<dependency>
<groupId>com.pinyougou</groupId>
<artifactId>pinyougou-dao</artifactId>
<version>1.0-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>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!--solr相关依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>
</project>
(2)编写配置文件(applicationContext-service.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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.pinyougou.solrutil"/>
</beans>
(3)编写SolrUtil类,先进行数据查询测试,然后再使用solr进行正式导入
package com.pinyougou.solrutil;
import com.pinyougou.mapper.TbItemMapper;
import com.pinyougou.pojo.TbItem;
import com.pinyougou.pojo.TbItemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Solr
* Author xushuai
* Description
*/
@Component
public class SolrUtil {
@Autowired
private TbItemMapper itemMapper;
public static void main(String[] args){
ApplicationContext ac =
new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
SolrUtil solrUtil = ac.getBean(SolrUtil.class);
// 执行导入
solrUtil.importData();
}
/**
* 导入商品数据到索引库
*/
private void importData() {
// 构造查询条件
TbItemExample example = new TbItemExample();
example.createCriteria().andStatusEqualTo(TbItem.STATUS_NORMAL);
// 执行查询
List<TbItem> tbItems = itemMapper.selectByExample(example);
for (TbItem item : tbItems) {
System.out.println(item.getTitle());
}
}
}
(4)输出
(5)修改pojo中的实体类TbItem,将对应业务域加到对应的属性上,只注意加了@Field注解的字段
@Field
private Long id;
@Field("item_title")
private String title;
private String sellPoint;
@Field("item_price")
private BigDecimal price;
private Integer stockCount;
private Integer num;
private String barcode;
@Field("item_image")
private String image;
private Long categoryid;
private String status;
private Date createTime;
private Date updateTime;
private String itemSn;
private BigDecimal costPirce;
private BigDecimal marketPrice;
private String isDefault;
@Field("item_goodsId")
private Long goodsId;
private String sellerId;
private String cartThumbnail;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
private String spec;
@Field("item_seller")
private String seller;
注意:使用Field注解之前,需要在pojo的pom.xml中引入solr的依赖
(6)配置 Solr 配置文件(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:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
(7)修改SolrUtil,注入SolrTemplate,然后修改importData方法,实现数据导入Solr索引库
(8)查看索引库
2.2 导入规格数据到索引库
(1)修改pojo工程中的item实体,新增一个属性用于保存动态域信息,使用 @Dynamic表示为动态域
@Dynamic
@Field("item_spec_*")
private Map<String, String> specMap;
注意:必须加入泛型限定<String, String>,否则保存时,会报错,他在将数据替换到通配符上会出错
(2)修改SolrUtil中的importData方法,使用spec转换为Map集合,保存到索引库
3、搜索服务工程搭建
3.1 搭建工程
导入相关依赖,编写相关配置文件,参考content-interface和content-service,只需要注意导入solr的配合文件即可
3.2 服务层接口
(search-interface),新建ItemSearchService,并添加搜索方法
package com.pinyougou.search.service;
import java.util.Map;
/**
* 搜索服务层接口
* Author xushuai
* Description
*/
public interface ItemSearchService {
/**
* 搜索
*
* @param searchMap 搜索条件
* @return java.util.Map
*/
Map search(Map searchMap);
}
3.3 服务层实现(search-service)
新建ItemSearchServiceImpl,实现ItemSearchService
package com.pinyougou.search.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.pinyougou.pojo.TbItem;
import com.pinyougou.search.service.ItemSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import java.util.HashMap;
import java.util.Map;
/**
* 搜索服务实现
* Author xushuai
* Description
*/
@Service
public class ItemSearchServiceImpl implements ItemSearchService {
@Autowired
private SolrTemplate solrTemplate;
@Override
public Map search(Map searchMap) {
// 构造查询条件
Query query = new SimpleQuery("*:*");
Criteria criteria = new Criteria("item_keywords");
criteria.is(searchMap.get("keywords"));
query.addCriteria(criteria);
// 执行查询
ScoredPage<TbItem> pageInfo = solrTemplate.queryForPage(query, TbItem.class);
// 返回结果
Map resultMap = new HashMap();
resultMap.put("rows", pageInfo.getContent());
return resultMap;
}
}
4、搜索控制层服务
4.1 搭建控制层工程
名称为:pinyougou-search-web,引入相关依赖和相关配置文件,还需要导入相关静态资源
4.2 在search-web中,新建ItemSearchController
package com.pinyougou.search.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.search.service.ItemSearchService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* 搜索控制层
* Author xushuai
* Description
*/
@RestController
@RequestMapping("/itemsearch")
public class ItemSearchController {
@Reference
private ItemSearchService itemSearchService;
@RequestMapping("/search")
public Map search(@RequestBody Map searchMap) {
return itemSearchService.search(searchMap);
}
}
4.3 编写searchService.js和searchController.js
(1)searchService.js
app.service('searchService',function ($http) {
// 搜索
this.search = function (searchMap) {
return $http.post("itemsearch/search.do", searchMap);
}
});
(2)searchController.js
app.controller('searchController',function ($scope,searchService) {
$scope.search = function () {
searchService.search($scope.searchMap).success(
function (rtn) {
$scope.resultMap = rtn;
}
);
}
});
4.4 在页面中引入相关js和基本的angular js指令
4.5 给所有框和搜索按钮绑定变量和单击事件
4.6 循环遍历查询结果,展示搜索结果
4.7 效果