今日目標:
(1)完成solr環境安裝、中文解析器和業務域的配置
(2)會使用Spring Data Solr完成增刪改查操作
(3)完成批量數據導入
(4)完成按關鍵字搜索功能
(5)完成高亮顯示關鍵字的功能
(6)完成更新索引庫的功能
目錄
1.1 在solrhome中的collction1下的conf目錄中打開 schema.xml 配置業務域
1.2 在solrhome中的collction1下的conf目錄中打開 schema.xml 配置複製域
1.3 在solrhome中的collction1下的conf目錄中打開 schema.xml 配置動態域
4.2 在search-web中,新建ItemSearchController
4.3 編寫searchService.js和searchController.js
4.4 在頁面中引入相關js和基本的angular js指令
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 效果