sorl學習筆記
sorl是基于luncene開發的一個架構,具體就不多解釋了,主要是說一下怎麼實作
首先簡單說一下内容吧!
- solr:可視化的背景管理器
- solrj:提供給java程式設計的
一、solr
solr安裝和基本配置步驟:
- 去官網下載下傳solr壓縮包;
- 環境要求:tomcat,jdk,是以也要下載下傳這兩個東西,并安裝配置好;
- 開始整合solr和tomcat,下面就是整合的步驟:
- 把\solr-4.10.3\dist\solr-4.10.3.war複制到tomcat的webapps裡面,在目前路徑建立一個solr檔案夾,把war包的内容解壓到solr檔案夾裡,删除war包;
- 把\solr-4.10.3\example\lib\ext下所有的jar包copy到tomcat的solr工程裡面的lib檔案夾裡;
- 建立一個solrhome檔案夾,例如:C:\temp\solrhome\;
- 把\solr-4.10.3\example\solr\路徑下的所有内容拷貝到C:\temp\solrhome\裡面;
- 為了讓solr伺服器知道solrhome的位置,要修改tomcat裡面solr的配置,就是修改web.xml檔案,如下
<!--這段代碼本來是被注解的,要自己把注解去掉,修改env-entry-value那裡-->
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>C:\temp\solrhome</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
solrhome檔案夾解釋
- 在solrhome下有一個檔案夾叫做collection1這就是一個solrcore。就是一個solr的執行個體。一個solrcore相當于mysql中一個資料庫。Solrcore之間是互相隔離。
- 在collection1中有一個檔案夾叫做conf,包含了索引solr執行個體的配置資訊。
- 在conf檔案夾下有一個solrconfig.xml。配置執行個體的相關資訊。如果使用預設配置可以不用做任何修改。
- 在collection1中有一個lib檔案夾,沒有就手動建立,是solr服務依賴的擴充包。
- 在collection1中有一個data檔案夾,沒有的話程式會自動建立,配置了索引庫的存放路徑。
建立一個Sorlcore:隻要複制collection1,再重命名粘貼到同一目錄下就可以了,比如重命名為collection2,并且還要修改collection2\core.properties檔案内容為
name=collection2
這時啟動tomcat通路http://localhost:8080/solr/就可以通路solr背景管理界面了。
solr背景管理界面的基本使用
說在前面:因為我們通常都是在進行中文檔案,是以一般都是需要用到中文分析器的,這裡推薦使用IK分析器,下面簡單說一下配置IK分析器的步驟。
首先把IK分析器的jar包丢到tomcat裡面的solr工程裡,同時也複制IK分析器的配置檔案和自定義詞典和停用詞詞典到solr的classpath下,然後在solrhome裡面找到collection1\conf\schema.xml檔案,在該檔案的後面
</schema>
标簽之前,添加以下代碼:
<!-- IKAnalyzer,其實就是自定義fieldType-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!--IKAnalyzer Field,其實就是用上面的fieldType定義field-->
<field name="title_ik" type="text_ik" indexed="true" stored="true" />
<field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>
在sorl裡面field的名字是不可以随便取的,要符合schema.xml配置檔案的要求,這裡就不多說這方面的事情了。
在Core selector那裡找到collection1,選中他就可以進入下面的操作了,主要說三個操作Document,query和Dataimport
1.Document:建立索引,更新索引和删除索引
建立和更建立議使用json,删除則用xml
1.建立索引的格式:
就直接按json的格式就可以了,例如{“id”:”123456”,”title_ik”:”測試标題”}。
2.更新索引的格式:
更新類似添加,其實就是先删除再添加,如果id存在就是更新,不存在就是添加。
3.删除索引的格式如下:
a.删除制定ID的索引
<delete>
<id>1</id>
</delete>
<commit/>
b.删除查詢到的索引資料
<delete>
<query>product_catalog_name:衣服</query>
</delete>
<commit/>
c.删除所有索引資料
<delete>
<query>*:*</query>
</delete>
<commit/>
2.Dataimport:導入外部資料建立索引,例如mysql資料庫的資料
- 把\solr-4.10.3\dist\下的兩個名字帶dataimport的jar包複制到collection1\lib中;
- 再把mysql的驅動jar也複制到collection1\lib中;
- 在collection1\conf\solrconfig.xml檔案中添加以下代碼,其實就是添加一個requestHandler:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
- 在collection1\conf\下建立一個data-config.xml檔案,并且在裡面添加以下代碼:
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/solrData"
user="root"
password="root"/>
<document>
<entity name="product" query="SELECT p_id,p_name,p_catalog_name,p_price,p_description,p_picture FROM products ">
<field column="p_id" name="id"/>
<field column="p_name" name="product_name"/>
<field column="p_catalog_name" name="product_catalog_name"/>
<field column="p_price" name="product_price"/>
<field column="p_description" name="product_description"/>
<field column="p_picture" name="product_picture"/>
</entity>
</document>
5.這時候就可以導入mysql裡面的資料了嗎?還不行,還要為上面的業務系統自定義相關的field,也就是還需要在schema.xml配置檔案中加入下面代碼:
<!--product-->
<field name="product_name" type="text_ik" indexed="true" stored="true"/>
<field name="product_price" type="float" indexed="true" stored="true"/>
<field name="product_description" type="text_ik" indexed="true" stored="false" />
<field name="product_picture" type="string" indexed="false" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="true" />
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="product_name" dest="product_keywords"/>
<copyField source="product_description" dest="product_keywords"/>
到這裡就差不多了,确實配置挺多東西的。
3.query:查詢索引
- q - 查詢字元串,必須的,如果查詢所有使用
。*:*
- fq - (filter query)過慮查詢,作用:在q查詢符合結果中同時是fq查詢符合的,例如:
product_price:[1 TO 15]
也可以使用“*”表示無限,例如:
15以上:product_price:[15 TO *]
15以下:product_price:[* TO 15]
- sort - 排序,格式:sort=
。示例:<field name>+<desc|asc>[,<field name>+<desc|asc>]…
product_price desc
- start - 分頁顯示使用,開始記錄下标,從0開始
- rows - 指定傳回結果最多有多少條記錄,配合start來實作分頁。
- fl - 指定傳回那些字段内容,用逗号或空格分隔多個。
- df-指定一個預設搜尋Field,其實也可以在conf/solrconfig.xml檔案中指定預設搜尋Field。
- wt - (writer type)指定輸出格式,可以有 xml, json, php, phps
- hl 是否高亮 ,設定高亮Field,設定格式字首和字尾。
二、solrj
solrj是通路Solr服務的java用戶端,提供索引和搜尋的請求方法,SolrJ通常在嵌入在業務系統中,通過SolrJ的API接口操作Solr服務。
1.增、改、删
基本步驟:
1. 依賴的jar包:
solr-4.10.3\dist\
路徑下的solr-solrj-4.10.3.jar和solrj-lib檔案夾裡面的所有jar包。
2. 需要的jar包:
solr-4.10.3\example\lib\ext\
路徑下所有的jar包也需要。
3. 和Solr伺服器建立連接配接。HttpSolrServer對象建立連接配接。
4. 建立一個SolrInputDocument對象,然後添加域。
5. 将SolrInputDocument添加到索引庫。
6. 送出。
實作這些操作的代碼:
//通過sorlj插入一個文檔
@Test
public void addDocument() throws SolrServerException, IOException{
//1.和solr伺服器連接配接,建立一個SolrServer對象,傳一個solr伺服器位址進去作為參數
SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr");
//2.建立一個文檔對象
SolrInputDocument document=new SolrInputDocument();
//3.向文檔中添加域
document.addField("id", "c5luo45678");
document.addField("title_ik", "使用solrj添加的文檔");
document.addField("content_ik", "這就是它的那些無聊的内容,無聊透頂了,沒什麼好看的。");
document.addField("product_name", "這是一個商品名稱");
//4.把document對象添加到索引庫
solrServer.add(document);
//5.送出修改
solrServer.commit();
}
//根據id删除文檔
@Test
public void deleteDocumentById() throws SolrServerException, IOException{
//1.和solr伺服器連接配接,建立一個SolrServer對象,傳一個solr伺服器位址進去作為參數
SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr");
solrServer.deleteById("c5luo45678");
solrServer.commit();
}
//根據條件删除
@Test
public void deleteDocumentByQuery() throws SolrServerException, IOException{
//1.和solr伺服器連接配接,建立一個SolrServer對象,傳一個solr伺服器位址進去作為參數
SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr");
solrServer.deleteByQuery("*:*");
solrServer.commit();
}
//修改文檔
//solrj裡面的修改文檔使用的是添加文檔的方法,隻要id存在就是修改(删除存在的,添加新的),不存在就添加
@Test
public void updateDocument() throws SolrServerException, IOException{
//1.和solr伺服器連接配接,建立一個SolrServer對象,傳一個solr伺服器位址進去作為參數
SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr");
//2.建立一個文檔對象
SolrInputDocument document=new SolrInputDocument();
//3.向文檔中添加域
document.addField("id", "c5luo45678");
document.addField("title_ik", "我已經不是當初的我了");
document.addField("content_ik", "我還是那麼的無聊");
document.addField("product_name", "換了個無聊的名字");
//4.把document對象添加到索引庫
solrServer.add(document);
//5.送出修改
solrServer.commit();
}
2.查詢
大概步驟:
- 建立一個和solr伺服器的連結,new一個HttpSolrServer對象;
- 建立一個query對象;
- 往這個query對象裡面添加各種各樣的查詢條件等;
- 然後用HttpSolrServer對象來執行這個query的查詢,其實就是抛給solr伺服器查詢;
- 最後根據自己的需求取查詢結果。
//先來個簡單查詢
@Test
public void testQueryIndex() throws SolrServerException{
//1.和solr伺服器連接配接,建立一個SolrServer對象,傳一個solr伺服器位址進去作為參數
SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr");
//2.建立一個query對象
SolrQuery query = new SolrQuery();
//3.設定查詢條件
query.setQuery("*:*");
//4.執行查詢
QueryResponse queryResponse = solrServer.query(query);
//5.取查詢結果
SolrDocumentList solrDocumentList = queryResponse.getResults();
//6.輸出查詢到的商品總數目
System.out.println("一共查詢到的商品數量為:"+solrDocumentList.getNumFound());
//7.周遊查詢結果
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("product_name"));
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_picture"));
}
}
//來個複雜查詢,包含查詢、過濾、分頁、排序、高亮顯示等處理
@Test
public void testQueryIndex2() throws SolrServerException{
//1.和solr伺服器連接配接,建立一個SolrServer對象,傳一個solr伺服器位址進去作為參數
SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr");
//2.建立一個query對象
SolrQuery query = new SolrQuery();
//3.設定查詢條件
query.setQuery("台燈");
//4.設定過濾條件
query.setFilterQueries("product_price:[10 TO 200]");
//5.設定排序規則
query.setSort("product_price", ORDER.asc);
//6.分頁處理
query.setStart();
query.setRows();
//7.設定結果需要包含的域
query.setFields("id","product_name","product_price");
//8.設定預設搜尋域
query.set("df", "product_keywords");
//9.高亮顯示搜尋的關鍵詞,隻是product_name
query.setHighlight(true);
query.addHighlightField("product_name");
query.setHighlightSimplePre("<span color:'red'>");
query.setHighlightSimplePost("<span>");
//*****************************************************
//10.執行查詢
QueryResponse queryResponse = solrServer.query(query);
//11.擷取結果
SolrDocumentList solrDocumentList = queryResponse.getResults();
System.out.println("結果的數量:"+solrDocumentList.getNumFound());
for (SolrDocument solrDocument : solrDocumentList) {
String id = (String)solrDocument.get("id");
System.out.println(id);
//擷取高亮内容
String product_name="";
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
List<String> list = highlighting.get(id).get("product_name");
//判斷是否有高亮内容,因為預設查詢的域是product_keywords,是以有可能關鍵字不一定在product_name裡面,是以就會有些沒有高亮
if(list!=null){
product_name=list.get();
}else{
product_name=(String) solrDocument.get("product_name");
}
System.out.println(product_name);
System.out.println(solrDocument.get("product_price"));
System.out.println("-------------------------------------------");
}
}