1. 程式人生 > >安裝搜尋引擎--Solr 單機部署和api 簡單使用

安裝搜尋引擎--Solr 單機部署和api 簡單使用

簡介 :

Solr 是Apache下的一個頂級開源專案,採用Java開發,它是基於Lucene的全文搜尋伺服器。Solr提供了比Lucene更為豐富的查詢語言,同時實現了可配置、可擴充套件,並對索引、搜尋效能進行了優化。
Solr是一個全文檢索伺服器,只需要進行配置就可以實現全文檢索服務。

Solr的版本:4.10.3

單機安裝:

需要把solr伺服器安裝到linux環境:
第一步:安裝linux、jdk、tomcat。
[[email protected] ~]# ll
total 8044
-rw-r--r--. 1 root root 8234674 Oct 27  2013
apache-tomcat-7.0.47.tar.gz [[email protected] ~]# tar -zxf apache-tomcat-7.0.47.tar.gz [[email protected] ~]# ll total 8048 drwxr-xr-x. 9 root root 4096 Sep 10 17:55 apache-tomcat-7.0.47 -rw-r--r--. 1 root root 8234674 Oct 27 2013 apache-tomcat-7.0.47.tar.gz [[email protected] ~]# mkdir /usr/local/solr
[[email protected] ~]# cp apache-tomcat-7.0.47 /usr/local/solr/tomcat cp: omitting directory `apache-tomcat-7.0.47' [[email protected] ~]# cp apache-tomcat-7.0.47 /usr/local/solr/tomcat -r [[email protected] ~]# cd /usr/local/solr/ [[email protected] solr]# ll total 4 drwxr-xr-x. 9 root root 4096 Sep 10
17:56 tomcat [[email protected] solr]# 第二步:把solr的壓縮包上傳到伺服器。並解壓。 第三步:把/root/solr-4.10.3/dist/solr-4.10.3.war包部署到tomcat下。並改名為solr.war [[email protected] dist]# cp solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war 第四步:解壓war包。啟動tomcat自動解壓。關閉tomcat。刪除solr.war. 第五步:把/root/solr-4.10.3/example/lib/ext 目錄下所有的jar包複製到solr工程中。 [[email protected] ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/ 第六步:建立solrhome。Solrhome是存放solr伺服器所有配置檔案的目錄。 [[email protected] example]# pwd /root/solr-4.10.3/example [[email protected] example]# cp -r solr /usr/local/solr/solrhome [[email protected] example]# 第七步:告訴solr伺服器solrhome的位置。 需要修改solr工程的web.xml檔案。

這裡寫圖片描述

第八步:啟動tomcat

這裡寫圖片描述

2.2.2.1中文分析器的配置
第一步:使用IK-Analyzer。把分析器的資料夾上傳到伺服器。
這裡寫圖片描述

第二步:需要把分析器的jar包新增到solr工程中。

[root@bogon IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
[root@bogon IK Analyzer 2012FF_hf1]# 
第三步:需要把IKAnalyzer需要的擴充套件詞典及停用詞詞典、配置檔案複製到solr工程的classpath。
/usr/local/solr/tomcat/webapps/solr/WEB-INF/classes

//如果沒有classes資料夾 自行建立
[root@bogon IK Analyzer 2012FF_hf1]# cp IKAnalyzer.cfg.xml ext_stopword.dic mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[root@bogon IK Analyzer 2012FF_hf1]# 

注意:擴充套件詞典及停用詞詞典的字符集必須是utf-8。不能使用windows記事本編輯。

第四步:配置fieldType。需要在solrhome/collection1/conf/schema.xml中配置。
技巧:使用vi、vim跳轉到文件開頭gg。跳轉到文件末尾:G
<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

這裡寫圖片描述

2.2.2.2業務欄位配置

業務欄位判斷標準:
1、在搜尋時是否需要在此欄位上進行搜尋。例如:商品名稱、商品的賣點、商品的描述
2、後續的業務是否需要用到此欄位。例如:商品id。

需要用到的欄位:
1、商品id
2、商品title
3、賣點
4、價格
5、商品圖片
6、商品分類名稱
7、商品描述

Solr中的業務欄位:
1、id——》商品id
其他的對應欄位建立solr的欄位。
<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"/>

重新啟動tomcat

這裡寫圖片描述

2.3維護索引庫

新增:新增一個json格式的檔案就可以。
修改:在solr中沒有update,只需要新增一個新的文件,要求文件id和被修改文件的id一致。原理是先刪除後新增。
刪除:使用xml格式。

這裡寫圖片描述

刪除兩種方法:
1、根據id刪除:
<delete>
<id>test001</id>
</delete>
<commit/>
2、根據查詢刪除:
<delete>
<query>*:*</query>
</delete>
<commit/>

3solrJ客戶端

需要依賴solrj的jar包。
<!-- solr客戶端 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
        </dependency>

3.1使用solrj的使用

public class SolrJTest {

    @Test
    public void addDocument() throws Exception {
        //建立一連線
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        //建立一個文件物件
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "test001");
        document.addField("item_title", "測試商品2");
        document.addField("item_price", 54321);
        //把文件物件寫入索引庫
        solrServer.add(document);
        //提交
        solrServer.commit();
    }

    @Test
    public void deleteDocument() throws Exception {
        //建立一連線
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        //solrServer.deleteById("test001");
        solrServer.deleteByQuery("*:*");
        solrServer.commit();
    }
}