Linux下solr的安裝與使用
- 一、solr介紹
-
- 1.1 背景
- 1.2 solr實作
- 1.3 什麼是solr
- 1.4 solr與Lucene的差別
- 二、Linux下solr安裝過程
-
- 2.1 solr的環境
- 2.2 解壓solr安裝包
- 2.3 将solr部署到tomcat上
- 2.4 啟動Tomcat
- 2.5 拷貝solr啟動需要的jar包
- 2.6 拷貝solrhome
- 2.7 關聯solr及solrhome
- 2.8 啟動Tomcat
- 三、配置業務域
-
- 3.1 把中文分析器添加到工程中
- 3.2 配置一個FieldType,指定使用IKAnalyzer
- 3.3 重新開機tomcat,使業務域生效
- 四、在項目中操作索引庫
-
- 4.1 添加索引庫
- 4.2 更新索引庫
- 4.3 删除索引庫
- 4.4 查詢索引庫
一、solr介紹
1.1 背景
在一些大型門戶網站、電子商務網站等都需要站内搜尋功能,使用傳統的資料庫查詢方式實作搜尋無法滿足一些進階的搜尋需求,比如:搜尋速度要快、搜尋結果按相關度排序、搜尋内容格式不固定等,這裡就需要使用全文檢索技術實作搜尋功能
1.2 solr實作
基于Solr實作站内搜尋擴充性較好并且可以減少程式員的工作量,因為Solr提供了較為完備的搜尋引擎解決方案,是以在門戶、論壇等系統中常用此方案
1.3 什麼是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的配置和運作情況
1.4 solr與Lucene的差別
Lucene是一個開放源代碼的全文檢索引擎工具包,它不是一個完整的全文檢索引擎,Lucene提供了完整的查詢引擎和索引引擎,目的是為軟體開發人員提供一個簡單易用的工具包,以友善的在目标系統中實作全文檢索的功能,或者以Lucene為基礎建構全文檢索引擎。
Solr的目标是打造一款企業級的搜尋引擎系統,它是一個搜尋引擎服務,可以獨立運作,通過Solr可以非常快速的建構企業的搜尋引擎,通過Solr也可以高效的完成站内搜尋功能。

二、Linux下solr安裝過程
2.1 solr的環境
solr是Java開發的,需要安裝jdk和tomcat
2.2 解壓solr安裝包
2.3 将solr部署到tomcat上
為了便于區分,将tomcat放在了/usr/local/solr/檔案加下
将solr的war包部署到Tomcat上
2.4 啟動Tomcat
此時solr的war包已經解壓縮了
然後關閉tomcat
删除solr.war
2.5 拷貝solr啟動需要的jar包
jar包位置:example/lib/ext/下的所有jar包
2.6 拷貝solrhome
/example/solr目錄就是一個solrhome。複制此目錄到/usr/local/solr/solrhome
2.7 關聯solr及solrhome
需要修改solr工程的web.xml檔案
注釋去掉,路徑修改
2.8 啟動Tomcat
啟動成功
三、配置業務域
3.1 把中文分析器添加到工程中
把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目錄下
把擴充詞典、配置檔案放到solr工程的WEB-INF/classes目錄下
首先建立classes檔案夾
3.2 配置一個FieldType,指定使用IKAnalyzer
在solrhome檔案下找到schema.xml檔案
編輯
在檔案末尾添加FieldType,并配置自定義的業務域
3.3 重新開機tomcat,使業務域生效
四、在項目中操作索引庫
4.1 添加索引庫
導入對應的solej架包之後,就可以往索引庫中添加索引了
@Test
public void addDocument() throws Exception{
//建立一個solrservice對象,建立一個連接配接,參數solr服務的url
SolrServer solrServer = new HttpSolrServer("http://192.168.12.128:8080/solr/collection1");
//建立一個文檔對象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
//向文檔對象中添加域,文檔中必須包含一個id域,所有域的名稱必須在shema.xml中定義
document.addField("id", "doc01");
document.addField("item_title", "測試商品01");
document.addField("item_price", 1000);
//把文檔寫入索引庫
solrServer.add(document);
//送出
solrServer.commit();
}
4.2 更新索引庫
更新與添加一樣 隻要id一樣,就會先删除再添加
@Test
public void editDocument() throws Exception{
//建立一個solrservice對象,建立一個連接配接,參數solr服務的url
SolrServer solrServer = new HttpSolrServer("http://192.168.12.128:8080/solr/collection1");
//建立一個文檔對象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
//向文檔對象中添加域,文檔中必須包含一個id域,所有域的名稱必須在shema.xml中定義
document.addField("id", "doc01");
document.addField("item_title", "修改索引商品01");
document.addField("item_price", 1000);
//把文檔寫入索引庫
solrServer.add(document);
//送出
solrServer.commit();
}
4.3 删除索引庫
@Test
public void deleteDocument() throws Exception{
//建立一個solrservice對象,建立一個連接配接,參數solr服務的url
SolrServer solrServer = new HttpSolrServer("http://192.168.12.128:8080/solr/collection1");
//删除文檔 兩種方法一樣,根據id删、查詢删
//solrServer.deleteById("doc01");
solrServer.deleteByQuery("id:doc01");
//送出
solrServer.commit();
}
4.4 查詢索引庫
簡單查詢
@Test
//簡單查詢
public void queryIndex() throws Exception{
//建立一個solrServer對象
SolrServer solrServer = new HttpSolrServer("http://192.168.12.128:8080/solr/collection1");
//建立一個solrQuery對象
SolrQuery solrQuery = new SolrQuery();
//設定查詢條件
solrQuery.setQuery("*:*");
solrQuery.set("q", "*:*");
//執行查詢,QueryResponse對象
QueryResponse queryResponse = solrServer.query(solrQuery);
//取文檔清單,取查詢結果的總記錄數
SolrDocumentList solrDocumentList = queryResponse.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_sell_point"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_image"));
System.out.println(solrDocument.get("item_category_name"));
}
}
複雜查詢
@Test
//複雜查詢
public void queryIndexFuZa() throws Exception{
//建立一個solrServer對象
SolrServer solrServer = new HttpSolrServer("http://192.168.12.128:8080/solr/collection1");
//建立一個solrQuery對象
SolrQuery solrQuery = new SolrQuery();
//設定查詢條件
solrQuery.setQuery("手機"); //查詢條件
solrQuery.setStart(0); //分頁條件
solrQuery.setRows(20);
solrQuery.set("df", "item_title"); //搜尋域
solrQuery.setHighlight(true); //高亮顯示
solrQuery.addHighlightField("item_title"); //高亮顯示的域
solrQuery.setHighlightSimplePre("<em>"); //字首
solrQuery.setHighlightSimplePost("</em>"); //字尾
//執行查詢,QueryResponse對象
QueryResponse queryResponse = solrServer.query(solrQuery);
//取文檔清單,取查詢結果的總記錄數
SolrDocumentList solrDocumentList = queryResponse.getResults();
System.out.println("查詢結果總記錄數:" + solrDocumentList.getNumFound());
//周遊文檔清單,從取域的内容
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
//取高亮顯示
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
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");
}
System.out.println(title);
System.out.println(solrDocument.get("item_sell_point"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_image"));
System.out.println(solrDocument.get("item_category_name"));
}
}