Linux下solr的安裝與使用

一、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"));
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章