1. 需要做的事情
l Solr服務的搭建
l 搜索功能的實現
1.1. 系統架構
2. Solr概述
2.1. 什麼是Solr
Solr 是Apache下的一個頂級開源項目,採用Java開發,它是基於Lucene的全文搜索服務器。Solr提供了比Lucene更爲豐富的查詢語言,同時實現了可配置、可擴展,並對索引、搜索性能進行了優化。
Solr可以獨立運行,運行在Jetty、Tomcat等這些Servlet容器中,Solr 索引的實現方法很簡單,用 POST 方法向 Solr 服務器發送一個描述 Field 及其內容的 XML 文檔,Solr根據xml文檔添加、刪除、更新索引 。Solr 搜索只需要發送 HTTP GET 請求,然後對 Solr 返回Xml、json等格式的查詢結果進行解析,組織頁面佈局。Solr不提供構建UI的功能,Solr提供了一個管理界面,通過管理界面可以查詢Solr的配置和運行情況。
2.2. 下載
從Solr官方網站(http://lucene.apache.org/solr/ )下載Solr4.10.3,根據Solr的運行環境,Linux下需要下載lucene-4.10.3.tgz,windows下需要下載lucene-4.10.3.zip。
Solr使用指南可參考:https://wiki.apache.org/solr/FrontPage。
下載lucene-4.10.3.zip並解壓:
bin:solr的運行腳本
contrib:solr的一些貢獻軟件/插件,用於增強solr的功能。
dist:該目錄包含build過程中產生的war和jar文件,以及相關的依賴文件。
docs:solr的API文檔
example:solr工程的例子目錄:
l example/solr:
該目錄是一個包含了默認配置信息的Solr的Core目錄。
l example/multicore:
該目錄包含了在Solr的multicore中設置的多個Core目錄。
l example/webapps:
該目錄中包括一個solr.war,該war可作爲solr的運行實例工程。
licenses:solr相關的一些許可信息
3. Solr的安裝及配置
3.1. 運行環境
solr 需要運行在一個Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默認提供Jetty(java寫的Servlet容器),本教程使用Tocmat作爲Servlet容器,環境如下:
Solr:Solr4.10.3
Jdk:jdk1.7.0_72
Tomcat:apache-tomcat-7.0.53
3.2. Solr整合tomcat
1. 將dist\solr-4.10.3.war拷貝到Tomcat的webapp目錄下改名爲solr.war
2. 啓動tomcat後,solr.war自動解壓,將原來的solr.war刪除。
3. 拷貝example\lib\ext 目錄下所有jar包到Tomcat的webapp\solr\WEB-INF\lib目錄下
4. 拷貝log4j.properties文件
在 Tomcat下webapps\solr\WEB-INF目錄中創建文件 classes文件夾,
複製Solr目錄下example\resources\log4j.properties至Tomcat下webapps\solr\WEB-INF\classes目錄
5. 創建solrhome及配置solrcore的solrconfig.xml文件
6. 修改Tomcat目錄 下webapp\solr\WEB-INF\web.xml文件,如下所示:
設置Solr home
<!--配置jndi告訴solr工程我們的solrhome的位置-->
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:/temp/solr/solrhome</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
4. Solr界面功能
5. 安裝中文分詞器
5.1. 安裝步驟
5.1.1. 第一步:配置IKAnalyzer的jar包
拷貝IKAnalyzer的文件到Tomcat下Solr目錄中
將IKAnalyzer2012FF_u1.jar拷貝到 Tomcat的webapps/solr/WEB-INF/lib 下。
5.1.2. 第二步:IKAnalyzer的配置文件
在Tomcat的webapps/solr/WEB-INF/下創建classes目錄
將IKAnalyzer.cfg.xml、ext_stopword.dic mydict.dic copy到 Tomcat的
webapps/solr/WEB-INF/classes
注意:ext_stopword.dic 和mydict.dic必須保存成無BOM的utf-8類型。
5.1.3. 第三步:修改schema.xml文件
修改schema.xml文件
修改Solr的schema.xml文件,添加FieldType:
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> |
5.1.4. 第四步:設置業務系統Field
設置業務系統Field
<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"/> |
6. 搭建taotao-search服務
6.1. 創建工程
6.2. 配置工程
工程配置參考taotao-rest,基本相同,不需要引入Jedis 的jar包,需要sorlJ的jar包。其他都相同。
6.2.1. 添加solrj的依賴關係
<!-- solr客戶端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> </dependency> |
6.3. 商品信息導入索引庫
6.3.1. 需求分析
需要把數據庫中的商品信息導入索引庫,需要商品的id、商品的名稱、商品的賣點、商品的價格、商品的圖片、商品的分類名稱。
原則:需要展示給用戶的字段、需要搜索的字段需要添加到索引庫。
6.3.2. Sql語句
SELECT a.id, a.title, a.sell_point, a.price, a.image, b. NAME category_name FROM tb_item a LEFT JOIN tb_item_cat b ON a.cid = b.id |
6.3.3. POJO定義
/** * 商品信息POJO * <p>Title: Item</p> * <p>Description: </p> * <p>Company: www.itcast.com</p> * @author入雲龍 * @date2015年7月28日下午3:06:16 * @version 1.0 */ public class Item { private Long id; private String title; private String sell_point; private Long price; private String image; private String category_name; } |
6.3.4. 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, a.image, b. NAME category_name FROM tb_item a LEFT JOIN tb_item_cat b ON a.cid = b.id </select> </mapper> |
6.3.5. Service
@Service public class ItemServiceImpl implements ItemService {
@Autowired private ItemMapper itemMapper; @Autowired private SolrServer solrServer; @Override public TaotaoResult importItemToIndex() throws Exception { //查詢商品列表 List<Item> itemList = itemMapper.getItemList(); //將商品列表導入solr for (Item item : itemList) { SolrInputDocument document = new SolrInputDocument(); document.addField("id", item.getId()); document.addField("item_title", item.getTitle()); document.addField("item_sell_point", item.getSell_point()); document.addField("item_price", item.getPrice()); document.addField("item_image", item.getImage()); document.addField("item_category_name", item.getCategory_name()); //將文檔寫入索引庫 solrServer.add(document); } //提交修改 solrServer.commit(); return TaotaoResult.ok(); }
} |
6.3.6. Controller
@Controller @RequestMapping("/manager") public class ItemController {
@Autowired private ItemService itemService; @RequestMapping("/importall") @ResponseBody public TaotaoResult importAll() { TaotaoResult result = null; try { result = itemService.importItemToIndex(); } catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } return result; } } |
6.3.7. Pom.xml
需要在pom文件的build節點中添加如下信息,否則mapper映射文件不會被髮布,從而發生異常。
<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> |
6.3.8. applicationContext-dao.xml
掃描包添加taotao-search工程中的包:
<!-- 加載mapper文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.taotao.mapper,com.taotao.search.mapper"></property> </bean> |
7. 實現商品搜索功能
實現商品搜索功能需要兩步實現:
第一步:在taotoa-search工程中發佈服務
第二步:在taotao-portal中調用服務並展示結果。
7.1. 發佈搜索服務
7.1.1. Dao
返回值SearchResult:
public class SearchResult {
private Long recordCount; private List<Item> itemList; private Integer pageCount; private Integer curPage; } |
Dao
@Service public class ItemSearchDaoImpl implements ItemSearchDao { @Autowired private SolrServer solrServer;
@Override public SearchResult searchItem(SolrQuery solrQuery) throws Exception { //根據查詢條件搜索索引庫 QueryResponse response = solrServer.query(solrQuery); //取商品列表 SolrDocumentList documentList = response.getResults(); //商品列表 List<Item> itemList = new ArrayList<>(); for (SolrDocument solrDocument : documentList) { Item item = new Item(); item.setId((Long) solrDocument.get("id")); //取高亮顯示 Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); List<String> list = highlighting.get(solrDocument.get("id")).get("item_title"); String title = ""; if (null != list && !list.isEmpty()) { title = list.get(0); } else { title = (String) solrDocument.get("item_title"); } item.setTitle(title); item.setPrice((Long) solrDocument.get("item_price")); item.setSell_point((String) solrDocument.get("item_sell_point")); item.setImage((String) solrDocument.get("item_image")); item.setCategory_name((String) solrDocument.get("item_category_name")); itemList.add(item); } SearchResult result = new SearchResult(); //商品列表 result.setItemList(itemList); //總記錄數據 result.setRecordCount(documentList.getNumFound()); return result; }
} |
7.1.2. Service
@Service public class ItemSearchServiceImpl implements ItemSearchService {
@Value("${SEARCH_RESULT_PAGE_SIZE}") private Integer PAGE_SIZE; @Autowired private ItemSearchDao itemSearchDao; @Override public SearchResult searchItem(String queryString, Integer page) throws Exception { //創建一個查詢對象 SolrQuery solrQuery = new SolrQuery(); //查詢條件 if (StringUtils.isBlank(queryString)) { solrQuery.setQuery("*:*"); } else { solrQuery.setQuery(queryString); } //分頁條件 if (page == null) { page = 1; } solrQuery.setStart((page -1) * PAGE_SIZE); solrQuery.setRows(PAGE_SIZE); //高亮顯示 solrQuery.setHighlight(true); //設置高亮顯示的域 solrQuery.addHighlightField("item_title"); //高亮顯示前綴 solrQuery.setHighlightSimplePre("<em style=\"color:red\">"); //後綴 solrQuery.setHighlightSimplePost("</em>"); //設置默認搜索域 solrQuery.set("df", "item_keywords"); //執行查詢 SearchResult result = itemSearchDao.searchItem(solrQuery); //計算分頁 Long recordCount = result.getRecordCount(); int pageCount = (int) (recordCount / PAGE_SIZE); if (recordCount % PAGE_SIZE > 0) { pageCount++; } result.setPageCount(pageCount); result.setCurPage(page); return result; }
} |
7.1.3. Controller
@Controller public class ItemSearchController { @Autowired private ItemSearchService itemSearchService;
@RequestMapping("/q") @ResponseBody public TaotaoResult search(@RequestParam(value = "kw") String queryString, @RequestParam(value = "page", defaultValue = "1") Integer page) { if (StringUtils.isBlank(queryString)) { return TaotaoResult.build(400, "查詢條件是必須的參數"); } SearchResult result = null; try { result = itemSearchService.searchItem(queryString, page);
} catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } return TaotaoResult.ok(result); } } |
7.1.4. 掃描dao所在的包
在applicationContext-dao.xml文件中添加如下內容:
7.1.5. 解決get請求亂碼問題
在controller中添加字符串編碼轉換邏輯:
Tomcat默認的編碼爲ISO8859-1,需要轉換成utf-8的編碼。
7.2. 調用服務實現搜索功能
7.2.1. 功能分析
Taotao-portal展示首頁,用戶在首頁輸入查詢內容提交至taotao-portal,taotao-portal調用taotao-search提供的搜索服務,得到商品列表。在taotao-portal中渲染商品列表展示搜索結果頁面。
7.2.2. Dao層
木有
7.2.3. Service層
使用HttpClientUtil工具類調用搜索服務,返回一個json數據,需要把json數據轉換成TaotaoResult對象。
@Service public class SearchServiceImpl implements SearchService {
@Value("${SEARCH_BASE_URL}") private String SEARCH_BASE_URL; @Override public SearchResult searchItemList(String queryString, Integer page) throws Exception { //查詢參數 Map<String, String> param = new HashMap<>(); param.put("kw", queryString); param.put("page", page==null?"1":page.toString()); //調用taotao-search提供的搜索服務 String resultString = HttpClientUtil.doGet(SEARCH_BASE_URL, param); //轉換成taotaoResult對象 TaotaoResult taotaoResult = TaotaoResult.formatToPojo(resultString, SearchResult.class); SearchResult searchResult = null; //查詢成功 if (taotaoResult.getStatus() == 200) { //取查詢結果 searchResult = (SearchResult) taotaoResult.getData(); } return searchResult; }
} |
7.2.4. Controller
@Controller public class SearchController {
@Autowired private SearchService searchService; @RequestMapping("/search") public String searchItemList(@RequestParam(value="q")String queryString, Integer page, Model model) throws Exception { //字符串轉碼 queryString = new String(queryString.getBytes("ISO8859-1"), "UTF-8"); SearchResult searchResult = searchService.searchItemList(queryString, page); model.addAttribute("itemList", searchResult.getItemList()); model.addAttribute("query", queryString); model.addAttribute("totalPages", searchResult.getPageCount()); model.addAttribute("page", searchResult.getCurPage()); model.addAttribute("pages", searchResult.getPageCount()); return "search"; } } |