Linux下安裝使用Solr

來源:互聯網
上載者:User

1、首先下載Solr、mmseg4j分詞包、tomcat並解壓,這用google、百度都可以搜尋得到。

2、因為要使用到中文分詞,所以要設定編碼,進入tomcat安裝目錄,使用vi修改confserver.xml配置

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> 

增加 URIEncoding="UTF-8"設定編碼為utf-8。

 3、將下載的solr包下面dist檔案夾中的apache-solr-*.war 拷貝到 tomcat的webapps 並且改名為 solr.war

cp /opt/apache-solr-3.6.1/dist/apache-solr-3.6.1.war /opt/apache-tomcat-6.0.35/webapps/solr.war

4、將下載的solr包下面example檔案夾中的solr檔案拷貝到/opt目錄下

cp -r /opt/apache-solr-3.6.1/example/solr/ /opt

5、配置環境變數solr.home,使用vi修改/etc/profile,增加以下代碼,配置永久變數,重啟reboot

export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/opt/solr"

6、啟動tomcat服務,然後訪問http://127.0.0.1:8080/solr/

出現如下頁面,表示配置成功

7、配置中文分詞,將mmseg4j目錄下的一個jar包複製到solr/WEB-INF/lib/目錄下讓solr使用,這裡必須先運行一遍才會產生solr檔案夾

cp /opt/mmseg4j/mmseg4j-all-1.8.3.jar /opt/apache-tomcat-6.0.35/webapps/solr/WEB-INF/lib/

8、配置中文詞庫
cp /opt/mmseg4j/data/words.dic /opt/solr/dic/

9、更改schema.xml (/opt/solr/conf/schema.xml) 檔案,使分詞器起到作用。

<!--mmseg4j field types-->  <fieldType name="textComplex" class="solr.TextField" positionIncrementGap="100" >         <analyzer>            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="/opt/solr/dic"/>            <filter class="solr.LowerCaseFilterFactory"/>        </analyzer>     </fieldType>     <fieldType name="textMaxWord" class="solr.TextField" positionIncrementGap="100" >     <analyzer>         <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="/opt/solr/dic"/>         <filter class="solr.LowerCaseFilterFactory"/>     </analyzer>     </fieldType>        <fieldType name="textSimple" class="solr.TextField" positionIncrementGap="100" >     <analyzer>        <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="/opt/solr/dic"/>     <filter class="solr.LowerCaseFilterFactory"/>     </analyzer>    </fieldType> 

同時增加

<!--mmseg4j copyField-->  <copyField source="simple" dest="text"/>   <copyField source="complex" dest="text"/> 

10、http://127.0.0.1:8080/solr/admin/analysis.jsp

點擊Analyze查看分詞效果

11、配置mysql資料庫

首先要匯入串連mysql的jar包mysql-connector-java-5.1.7-bin.jar

我是放在tomcat的lib目錄下

touch /opt/solr/conf/mysql.xml

將下面內容寫入mysql.xml中

<?xml version="1.0" encoding="UTF-8" ?>  <dataConfig>   <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://10.10.150.116/travel_main" user="new_travel_u" password="123045"/>    <document name="user_core">      <entity name="user_core" query="select * from user_core" pk="userId" deltaQuery ="select userId from user_core where editTime > '${dataimporter.last_index_time}'">          <field column="userId" name="id" />          <field column="nickname" name="nickname" />     </entity>  </document></dataConfig> 

solrconfig.xml的<fields>必須有和資料庫欄位相對應的子項目,我這裡新增了個nickname

<field name="nickname" type="string" indexed="true" stored="true" />

再配置solrconfig.xml檔案,在<config>節點下新增一個節點
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  <lst name="defaults">    <str name="config">/opt/solr/conf/mysql.xml</str>        </lst></requestHandler>

再把<lib>標籤的路徑修改下,不然可能會找不到相應的jar包

<lib dir="/opt/apache-solr-3.6.1/dist/" regex="apache-solr-cell-\d.*\.jar" /><lib dir="/opt/apache-solr-3.6.1/contrib/extraction/lib" regex=".*\.jar" /><lib dir="/opt/apache-solr-3.6.1/dist/" regex="apache-solr-clustering-\d.*\.jar" /><lib dir="/opt/apache-solr-3.6.1/contrib/clustering/lib/" regex=".*\.jar" /><lib dir="/opt/apache-solr-3.6.1/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" /><lib dir="/opt/apache-solr-3.6.1/contrib/dataimporthandler/lib/" regex=".*\.jar" /><lib dir="/opt/apache-solr-3.6.1/dist/" regex="apache-solr-langid-\d.*\.jar" /><lib dir="/opt/apache-solr-3.6.1/contrib/langid/lib/" regex=".*\.jar" /><lib dir="/opt/apache-solr-3.6.1/dist/" regex="apache-solr-velocity-\d.*\.jar" /><lib dir="/opt/apache-solr-3.6.1/contrib/velocity/lib" regex=".*\.jar" />

12、重啟tomcat

/opt/apache-tomcat-6.0.35/bin/shutdown.sh/opt/apache-tomcat-6.0.35/bin/startup.sh

13、瀏覽器輸入網址 

http://127.0.0.1:8080/solr/dataimport?command=full-import

新增全量索引,然後進入

http:// 127.0.0.1:8080/solr/admin/

查詢便得出結果

可以定時調度增加增量索引

http:// 127.0.0.1:8080/solr/dataimport?command=delta-import

本文轉自http://my.oschina.net/eatsuger/blog/82192?from=20121014

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.