天天看點

solr環境配置、中文分詞、資料庫

一、配置solr環境

1、 下載下傳solr http://www.apache.org/dyn/closer.cgi/lucene/solr/

此處用的是3.5 解壓到D盤D:/solr/apache-solr-3.5.0

2、修改tomcat conf/server.xml

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

添加編碼的配置 URIEncoding="UTF-8" (如不添加,中文檢索時因為亂碼搜尋不到)

3、 配置solr

Tomcat conf/Catalina/localhost下添加solr.xml

内容:

<Context docBase="D:/solr/apache-solr-3.5.0/dist/apache-solr-3.5.0.war" debug="0" crossContext="true" >

<Environment name="solr/home" type="java.lang.String" value="D:/solr/apache-solr-3.5.0/example/solr" override="true" />

</Context>

4、 啟動tomcat 輸入http://localhost:8080/solr/ 出現歡迎界面表示成功

二、中文分詞配置

1、 下載下傳分詞器: http://code.google.com/p/mmseg4j/

2、下載下傳詞庫:http://code.google.com/p/mmseg4j/downloads/detail?name=data.zip&can=2&q

3、将解壓後的mmseg4j-1.8.5目錄下的mmseg4j-all-1.8.5.jar拷貝到Tomcat的webapps/solr/WEB-INF/lib目錄下。

4、添加詞庫:在D:/solr/apache-solr-3.5.0/example/solr目錄下建立dic檔案夾,将解壓後的data目錄下的words.dic拷貝到D:/solr/apache-solr-3.5.0/example/solr/dic目錄下。

5、 更改schema.xml(D:/solr/apache-solr-3.5.0/example/solr/conf/)檔案,使分詞器起到作用。

在schema.xml的<types>、<fields>和部分新增如下配置:

注:dicPath=" "是你的詞庫路徑。

<!–mmseg4j field types–>

<fieldType name="textComplex" class="solr.TextField" positionIncrementGap="100" >

<analyzer>

<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="D:/solr/apache-solr-3.5.0/example/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="D:/solr/apache-solr-3.5.0/example/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="D:/solr/apache-solr-3.5.0/example/solr/dic"/>

<filter class="solr.LowerCaseFilterFactory"/>

</analyzer>

</fieldType>

<!– mmseg4j field –>

<field name="simple" type="textSimple" indexed="true" stored="true" multiValued="true"/>

<field name="complex" type="textComplex" indexed="true" stored="true" multiValued="true"/>

<field name="text_mmseg4j" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>

<!– mmseg4j copyField –>

<copyField source="simple" dest="text_mmseg4j"/>

<copyField source="complex" dest="text_mmseg4j"/>

重新開機你的tomcat。

通路:http://localhost:8080/solr/admin/analysis.jsp可以看 mmseg4j 的分詞效果。在 Field 的下拉菜單選擇 name,然後在應用輸入 complex。分詞的結果,如下圖:

好了,可以運作起來了,那就添加個文檔試下,在 解壓後的D:/solr/apache-solr-3.5.0/example/exampledocs目錄下建立 mmseg4j-solr-demo-doc.xml 文檔,内容如下:

<add>

<doc>

<field name="id">1</field>

<field name="text">高效、靈活的緩存功能,垂直搜尋功能。</field>

</doc>

<doc>

<field name="id">2</field>

<field name="text"> Solr是一個高性能,采用Java5開發,基于Lucene的全文搜尋伺服器。</field>

</doc>

<doc>

<field name="id">3</field>

<field name="text"> 獨立的企業級搜尋應用伺服器</field>

</doc>

</add>

注意:使用的字段name必須在solr的配置檔案schem.xml增加

下(id、title)一樣

然後在 cmd 下運作 post.jar,如下:

D:\solr\apache-solr-3.5.0\example\exampledocs>java -Durl=http://localhost:8080/

solr/update -Dcommit=yes -jar post.jar mmseg4j-solr-demo-doc.xml

注意:mmseg4j-solr-demo-doc.xml 要是 UTF-8 格式,不然送出後會亂碼。還有在查詢中文時需要把tomcat設定成URIEncoding="UTF-8";

檢視是否有資料,通路:http://localhost:8080/solr/admin/ 在Query String: 中輸入“高性能”

顯示如下圖所示:

三、Solr将資料庫做成索引資料源(以mysql為例)

1、 在solrconfig.xml中配置導入資料功能(D:\solr\apache-solr-3.5.0\example\solr\conf下)

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">

<lst name="defaults">

<str name="config">D:\solr\apache-solr-3.5.0\example\solr\conf\db\db-data-config.xml</str>

</lst>

</requestHandler>

2、 添加一個資料源db-data-config.xml (D:\solr\apache-solr-3.5.0\example\solr\conf\db下)

<dataConfig>

<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr" user="root" password="root"/>

<document name="messages">

<entity name="user" transformer="ClobTransformer" query="select * from t_user">

<field column="id" name="id" />

<field column="name" name="name"/>

<field column="title" name="title"/>

</entity>

</document>

</dataConfig>

CREATE TABLE t_user

(

id VARCHAR(255) PRIMARY KEY,

name VARCHAR(10) NOT NULL,

title VARCHAR(10)

);

3、放入mysql驅動包 (Tomcat的webapps/solr/WEB-INF/lib目錄下)

4、建立索引

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

5、 檢視是否成功,通路:http://localhost:8080/solr/admin/ 在Query String: 中輸入“sunshan”、“孫閃”

如下圖:

注:保證與資料庫連接配接正确

四、Solr多核(MultiCore)配置

1、 拷貝multicore(D:\solr\apache-solr-3.5.0\example下)下的core0、core1、exampledocs到solr(D:\solr\apache-solr-3.5.0\example\solr下)目錄下

2、 更改solr.xml(D:\solr\apache-solr-3.5.0\example\solr下)配置

添加:

<core name="core0" instanceDir="core0" dataDir="D:/solr/apache-solr-3.5.0/example/solr/core0/data"/>

<core name="core1" instanceDir="core1" dataDir="D:/solr/apache-solr-3.5.0/example/solr/core1/data"/>

配置後:

<cores adminPath="/admin/cores">

<core name="collection1" instanceDir="." />

<core name="core0" instanceDir="core0" dataDir="D:/solr/apache-solr-3.5.0/example/solr/core0/data"/>

<core name="core1" instanceDir="core1" dataDir="D:/solr/apache-solr-3.5.0/example/solr/core1/data"/>

</cores>

3、 啟動tomcat 通路:

http://localhost:8080/solr/

五、多核資料庫索引:

同上配置

注:出現錯誤Error loading class ‘org.apache.solr.handler.dataimport.DataImportHandler’

需配置(solrconfig.xml裡)

<!– start –>

<lib dir="D:/solr/apache-solr-3.5.0/contrib/extraction/lib" />

<lib dir="D:/solr/apache-solr-3.5.0/dist/" regex="apache-solr-cell-\d.*\.jar" />

<lib dir="D:/solr/apache-solr-3.5.0/dist/" regex="apache-solr-clustering-\d.*\.jar" />

<lib dir="D:/solr/apache-solr-3.5.0/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />

<lib dir="D:/solr/apache-solr-3.3.0/contrib/clustering/lib/" />

<lib dir="/total/crap/dir/ignored" />

<!– end –>

dir注意路徑

重新開機tomcat

建立索引:http://localhost:8080/solr/core1/dataimport?command=full-import

http://localhost:8080/solr/dataimport?command=full-import 這 url 告訴 solr 做全量索引,做索引中會删除所有資料。當然也可以用 clean=false 參數來告訴它不删除,但也會删除相同id的(在 scheam.xml 的uniqueKey 聲明的)。http://localhost:8080/solr/dataimport?command=full-import&clean=false

批量導入command=delta-import&commit=y

重新裝載配置檔案command=reload-config

終止導入command=abort

繼續閱讀