【SSM分散式架構電商專案-24】Solr實現商品的搜尋
匯入資料
資料來源,京東:
資料:
修改表結構
匯入圖片資料
測試:
Solr的安裝
下載得到zip壓縮包,下載的版本為4.10.2. 檔案大小148MB左右。
1、 將solr-4.10.2.zip檔案拷貝到C盤;(或者其他盤都可以,只要目錄中不要出現中文就行。)
2、 解壓solr-4.10.2.zip檔案,得到solr-4.10.2目錄。
3、 執行-> cmd 執行命(進入D:\solr\solr-4.10.2\example):
4、 執行命令:java -jar start.jar
5、 開啟瀏覽器,輸入地址:
至此,Solr已經安裝成功。
插入資料
solr安裝完成後是沒有資料的,需要匯入一些資料進去方便我們學習。
執行->cmd -> 輸入命令:cd D:\solr\solr-4.10.2\example\exampledocs
再輸入命令:java -jar post.jar solr.xml monitor.xml
已經查詢到資料了。
通過域名訪問solr服務
通過solr.taotao.com訪問。
配置nginx:
測試:
發現,請求域名後面依然得有/solr.
預設啟動的web應用伺服器是jetty。
修改配置檔案:
測試:
Solrj
建立taotao core
1、 在example目錄下建立taotao-solr資料夾;
2、 將./solr下的solr.xml拷貝到taotao-solr目錄下;
3、 在taotao-solr下建立taotao目錄,並且在taotao目錄下建立conf和data目錄;
4、 將example\solr\collection1\core.properties檔案拷貝到example\taotao-solr\taotao下,並且修改name=taotao;
5、 將example\solr\collection1\conf下的schema.xml、solrconfig.xml拷貝到example\taotao-solr\taotao\conf下;
6、 修改schema.xml檔案,使其配置最小化:
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="example" version="1.5">
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="_root_" type="string" indexed="true" stored="false"/>
<field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="string" indexed="true" stored="true"/>
<field name="sellPoint" type="string" indexed="false" stored="true"/>
<field name="price" type="long" indexed="true" stored="true"/>
<field name="image" type="string" indexed="false" stored="true"/>
<field name="cid" type="long" indexed="true" stored="true"/>
<field name="status" type="int" indexed="true" stored="false"/>
<field name="created" type="long" indexed="true" stored="false"/>
<field name="updated" type="long" indexed="true" stored="false"/>
<uniqueKey>id</uniqueKey>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
</schema>
7、 修改solrconfig.xml檔案,修改一些配置,大部分配置先保持預設:
a) 將所有的<lib>
標籤註釋掉;
b) 搜尋<str name="df">text</str>
替換成<str name="df">title</str>
,搜尋,然後全部替換掉。
c) 將<searchComponent name="elevator" class="solr.QueryElevationComponent" >
註釋掉(這個的功能類似百度的競價排名):
8、 啟動solr:
java -Dsolr.solr.home=taotao-solr -jar start.jar
測試:
新增IK中文分詞器的支援
1、 將IKAnalyzer-2012-4x.jar拷貝到example\solr-webapp\webapp\WEB-INF\lib下;
2、 在schema.xml檔案中新增fieldType:
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
測試:
匯入商品資料到solr中
匯入itcast-solrj
匯入依賴
使用Solrj完成索引資料的CRUD
package cn.itcast.solrj.test;
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.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Before;
import org.junit.Test;
import cn.itcast.solrj.pojo.Item;
public class ItemDataTest {
private HttpSolrServer httpSolrServer;
@Before
public void setUp() throws Exception {
// 在url中指定core名稱:taotao
//http://solr.taotao.com/#/taotao -- 介面地址
String url = "http://solr.taotao.com/taotao"; //服務地址
HttpSolrServer httpSolrServer = new HttpSolrServer(url); //定義solr的server
httpSolrServer.setParser(new XMLResponseParser()); // 設定響應解析器
httpSolrServer.setMaxRetries(1); // 設定重試次數,推薦設定為1
httpSolrServer.setConnectionTimeout(500); // 建立連線的最長時間
this.httpSolrServer = httpSolrServer;
}
@Test
public void testInsert() throws Exception{
Item item = new Item();
item.setCid(1L);
item.setId(999L);
item.setImage("image");
item.setPrice(100L);
item.setSellPoint("很好啊,趕緊來買吧.");
item.setStatus(1);
item.setTitle("飛利浦 老人手機 (X2560) 深情藍 移動聯通2G手機 雙卡雙待");
this.httpSolrServer.addBean(item);
this.httpSolrServer.commit();
}
@Test
public void testUpdate() throws Exception{
Item item = new Item();
item.setCid(1L);
item.setId(999L);
item.setImage("image");
item.setPrice(100L);
item.setSellPoint("很好啊,趕緊來買吧. 豪啊");
item.setStatus(1);
item.setTitle("飛利浦 老人手機 (X2560) 深情藍 移動聯通2G手機 雙卡雙待");
this.httpSolrServer.addBean(item);
this.httpSolrServer.commit();
}
@Test
public void testDelete() throws Exception{
this.httpSolrServer.deleteById("999");
this.httpSolrServer.commit();
}
@Test
public void testQuery() throws Exception{
int page = 2;
int rows = 1;
String keywords = "手機";
SolrQuery solrQuery = new SolrQuery(); //構造搜尋條件
solrQuery.setQuery("title:" + keywords); //搜尋關鍵詞
// 設定分頁 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;
}
}
}
for (Item item : items) {
System.out.println(item);
}
}
}
將商品資料到入到solr
測試: