天天看點

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

本次案例示範:環境

1、solr-4.10.3.tgz.tgz

2、apache-tomcat-7.0.88.tar.gz

3、IKAnalyzer2012FF_hf1.zip

資源下載下傳:Download  提取碼:syj6

1):下載下傳檔案資源上傳到指定目錄下(自定義路徑)

2):解壓到指定目錄下

tar -zxvf solr-4.10.3.tgz.tgz -C ./install/      

3):切換到 解壓後的檔案 dist 下

cd solr-4.10.3/dist/      
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

3-1):拷貝 solr-4.10.3.war 到 tomcat/webapp/ 下  (請提前解壓 所需要的的 tomcat)

cp solr-4.10.3.war /opt/solr/tomcat/apache-tomcat-7.0.88/webapps/solr.war      

4):啟動 tomcat 進行 solr 自動解壓縮

cd /opt/tomcat/apache-tomcat-7.0.88/bin/

./startup.sh       
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

5):複制 log 日志jar 到 solr 中

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
cd /opt/solr/solr-4.10.3/example/lib/ext/

cp * /opt/tomcat/apache-tomcat-7.0.88/webapps/solr/WEB-INF/lib/      

6):配置 solr home

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
cd /opt/solr/install/solr-4.10.3/example/

      
cp -r solr /opt/solr/install/solrhome      

7):solr 與 solrhome 建立關系   【修改 solr 中的 web.xml】

cd /opt/solr/tomcat/apache-tomcat-7.0.88/webapps/solr/WEB-INF/      
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

8):啟動 tomcat 進行通路 solr 測試【 ip:tomcat端口/solr】

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

以上 我們的 solr 服務就已經 搭建成功了。

9):配置 IK 中文分詞器

9-1):解壓分詞器包 (unzip 指令需要 yum安裝)

unzip  IKAnalyzer2012FF_hf1.zip       

9-2):重命名

mv IK\ Analyzer\ 2012FF_hf1/ IKAnalyzer2012FF_hf1      

9-3):将 IKAnalyzer2012FF_u1.jar 添加到 solr 工程中去

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
cp IKAnalyzer2012FF_u1.jar /opt/tomcat/apache-tomcat-7.0.88/webapps/solr/WEB-INF/lib/      

 首先建立 classes 目錄:

mkdir /opt/tomcat/apache-tomcat-7.0.88/webapps/solr/WEB-INF/classes      

拷貝:

cp IKAnalyzer.cfg.xml ext_stopword.dic mydict.dic /opt/tomcat/apache-tomcat-7.0.88/webapps/solr/WEB-INF/classes/      

檢視:

ll  /opt/tomcat/apache-tomcat-7.0.88/webapps/solr/WEB-INF/classes      
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

10):定義 fieldType 指定 使用中文分詞器

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
cd /opt/solr/solrhome/collection1/
cd conf/      
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

編輯 schema.xml  添加如下配置:

<!-- text_ik -->
    <fieldType name="text_ik" class="solr.TextField">
        <analyzer type="index" useSmart="false"
            class="org.wltea.analyzer.lucene.IKAnalyzer" />
        <analyzer type="query" useSmart="true"
            class="org.wltea.analyzer.lucene.IKAnalyzer" />
    </fieldType>      

儲存後 重新開機 tomcat 浏覽器檢視:

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

11):dataimport 導入資料庫資料  連接配接 mysql 

solr 預設是沒有開啟dataimport這個功能的,是以我們要經過一點配置來開啟它。

11-1):找到不要的 jar 檔案  及 mysql 驅動jar (所需要的的檔案 我會單獨提出來  文章頭部有下載下傳連結)

cd /opt/solr/install/solr-4.10.3/dist/      
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

把這個三個 JAR 複制到 tomcat/webapp/solr/WEB-INF/lib/ 下。

11-2):找到 solr-4.10.3/example/example-DIH/solr/db/conf/db-data-config.xml,把其複制到  solrHome/core1/conf/下,并改名為data-config.xml.

11-3):找到solrHome/core1/conf/solrconfig.xml,并打開,在裡面添加一段内容,如下

<requestHandler name="/dataimport" class="solr.DataImportHandler">
      <lst name="defaults">
            <str name="config">data-config.xml</str>
      </lst>
  </requestHandler>      

11-4):打開并編輯data-config.xml,完整的配置檔案如下

<dataConfig>
    <dataSource dataSource="data_source"
            driver="com.mysql.jdbc.Driver"
                url="jdbc:mysql://IP:3306/db-solr?serverTimezone=UTC&amp;characterEncoding=utf8&amp;useUnicode=true&amp;useSSL=false"
                user="admin"
                password="admin" />
    <document>
        <!-- name屬性,就代表着一個文檔,可以随便命名 -->
        <!-- 
            transformer 格式轉化:HTMLStripTransformer 索引中忽略HTML标簽
            query:查詢資料庫表符合記錄資料 query是一條sql,代表在資料庫查找出來的資料
            deltaQuery:增量索引查詢主鍵ID  注意這個隻能傳回ID字段
            deletedPkQuery:增量索引删除主鍵ID查詢 注意這個隻能傳回ID字段 deletedPkQuery="select id from meta_solr where 1=1 and isDeleted=1"
            deltaImportQuery:增量索引查詢導入的資料 
          
        -->
       <!--deletedPkQuery="select id from students  where del_flag=0"-->
        <entity name="students"
                pk="id"
                query="select id,carded,name,sex,telePhone,fraction,schools,familyAddress,arrangement,college,major,del_flag as delFlag
                        ,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%S') as createTime,DATE_FORMAT(update_time,'%Y-%m-%d %H:%i:%S') as updateTime from students where del_flag=0"
                deltaQuery="select id from students where update_time > '${dataimporter.last_index_time}'"               
                deletedPkQuery="select id from students  where del_flag=1"               
                deltaImportQuery="select id,carded,name,sex,telePhone,fraction,schools,familyAddress,arrangement,college,major,del_flag as delFlag
                                  ,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%S') as createTime,DATE_FORMAT(update_time,'%Y-%m-%d %H:%i:%S') as updateTime from students  where id='${dih.delta.id}'" >
       
            <!-- 每一個field映射着資料庫中列與文檔中的域,column是資料庫列,name是solr的域(必須是在  managed-schema 或 schema 檔案中配置過的域才行 版本的不同檔案名有差別 4版本是 schema) -->
            <field column="id" name="id"/>
            <field column="carded" name="carded"/>
            <field column="name" name="name"/>           
            <field column="sex" name="sex"/>
            <field column="telePhone" name="telePhone"/>
            <field column="fraction" name="fraction"/>
            <field column="schools" name="schools"/>
            <field column="familyAddress" name="familyAddress"/>           
            <field column="arrangement" name="arrangement"/>
            <field column="college" name="college"/>
            <field column="major" name="major"/>
            <field column="delFlag" name="delFlag"/>
            <field column="createTime" name="createTime"/>
            <field column="updateTime" name="updateTime"/>

        </entity>
    </document>
</dataConfig>      

schema.xml 必須要配置 field:要有對應關系

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
   <!--測試用例-->
   <field name="carded" type="string" indexed="true" stored="true"/>
   <field name="name" type="string" indexed="true" stored="true"/>
   <field name="sex" type="string" indexed="true" stored="true"/>
   <field name="telePhone" type="string" indexed="true" stored="true"/>
   <field name="fraction" type="int" indexed="true" stored="true"/>
   <field name="schools" type="text_ik" indexed="true" stored="true"/>
   <field name="familyAddress" type="text_ik" indexed="true" stored="true"/>
   <field name="arrangement" type="string" indexed="true" stored="true"/>
   <field name="college" type="string" indexed="true" stored="true"/>
   <field name="major" type="string" indexed="true" stored="true"/>

   <field name="delFlag" type="string" indexed="true" stored="true"/>
   <field name="createTime" type="string" indexed="true" stored="true"/>
   <field name="updateTime" type="string" indexed="true" stored="true"/>      

重新開機tomcat,然後會看到如下頁面

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

點選藍色的按鈕,則開始導入,導入過程依據數量量的大小,需要的時間也不同,可以點選右邊的Refresh status來重新整理狀态,可以檢視目前導入了多少條。

導入成功如下:

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署

12):mysql 資料發生變更 同步 solr 配置:

方案一:使用 solr 配置定時器進行 增量更新

方案二:使用 spring定時器等技術 調用接口進行增量更新

方案三:使用腳本定時 進行增量更新

12-1):打開tomcat下面solr的web.xml配置檔案,路徑:/usr/local/solr/tomcat/apache-tomcat-7.0.30/webapps/solr/WEB-INF 添加如下内容:

<!-- solr資料導入監聽器 -->
  <listener>
        <listener-class>
         org.apache.solr.handler.dataimport.scheduler.ApplicationListener
        </listener-class>
  </listener>      

12-2):導入相關jar 到 solr/tomcat/apache-tomcat-7.0.30/webapps/solr/WEB-INF/lib

提示:

  solr 4.10.3 :所需要的jar為  apache-solr-dataimportscheduler-1.0.jar  其他則報錯

  其他高版本的可能要重新編譯源代碼 重新生成jar包  

  這是個 solr 的 bug

導入完jar檔案後還需要添加如下配置:

  在solrhome目錄下,建立配置檔案: mkdir conf檔案夾,并在conf中建立mkdir dataimport.properties檔案,

  注意,一定要在solrhome目錄下,以solr執行個體core1要在同級目錄,并在dataimport.properties添加以下内容:

#################################################
#                                               #
#       dataimport scheduler properties         #
#                                               #
#################################################
 
#  to sync or not to sync 是否同步功能
#  1 - active; anything else - inactive 1 - 開啟; 否則不開啟
syncEnabled=1
#  which cores to schedule
#  in a multi-core environment you can decide which cores you want syncronized
#  leave empty or comment it out if using single-core deployment
#syncCores=game,resource 需要同步的solr core
syncCores=collection1
#  solr server name or IP address solr server 名稱或IP位址
#  [defaults to localhost if empty] 預設為localhost
server=localhost
#  solr server port solr server端口
#  [defaults to 80 if empty] 預設為80
#  安裝solr的tomcat端口,如果你使用的是預設的端口,就不用改了,否則改成自己的端口就好了
port=8081
#  排程區間
#  預設為30分鐘
interval=1
#  application name/context
#  [defaults to current ServletContextListener's context (app) name]
webapp=solr
#  URL params [mandatory]
#  remainder of URL
params=/dataimport?command=delta-import&clean=false&commit=true
#  schedule interval
#  number of minutes between two runs
#  [defaults to 30 if empty]
#  重做索引的時間間隔,機關分鐘,預設7200,即5天; 
#  為空,為0,或者注釋掉:表示永不重做索引
reBuildIndexInterval=1
 
#  重做索引的參數
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
 
#  重做索引時間間隔的計時開始時間,第一次真正執行的時間=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
#  兩種格式:2012-04-11 03:10:00 或者  03:10:00,後一種會自動補全日期部分為服務啟動時的日期
reBuildIndexBeginTime=03:10:00      

正常情況下:重新開機tomcat,然後再資料庫添加一條資料,就可以在solr背景查詢到添加的結果了

但是在 solr.4.10.3  版本當中發現并不生效 也可能是我才疏學淺 。

如有大佬發現此問題的原因:麻煩在留言區留言糾正  感謝。

建議使用高版本 solr。

針對上述定時更新不生效問題;本人采用 spring定時任務排程方案替代。

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
package cn.com.solr.utils;

import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.MapSolrParams;

import java.util.HashMap;
import java.util.Map;

public class SolrBuildRequest {

    public static SolrRequest<QueryResponse> buildRequest(Command command, String entity) {
        Map<String, String> map = new HashMap<>();
        switch (command) {
            case DELTA_IMPORT:
                map.put("command", "delta-import");
                map.put("commit", "true");
                map.put("wt", "json");
                map.put("indent","true");
                map.put("entity", entity);
                map.put("verbose","false");
                map.put("clean", "false");
                map.put("optimize", "false");
                map.put("index", "false");
                map.put("debug", "false");
                return new QueryRequest(new MapSolrParams(map));
            case FULL_IMPORT:
                map.put("command", "full-import");
                map.put("clean", "true");
                map.put("commit", "true");
                map.put("optimize", "false");
                map.put("entity", entity);
                map.put("index", "false");
                map.put("debug", "false");
                map.put("wt", "json");
                return new QueryRequest(new MapSolrParams(map));
        }
        return null;
    }

}      

SolrBuildRequest 工具類

Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
Ali_Cloud++:阿裡雲-單機版 solr4.10.3 安裝部署
package cn.com.solr.utils;

public enum Command {
    FULL_IMPORT,DELTA_IMPORT;
}      

Command 枚舉