Solr 3.5 + Tomcat7 + mmseg4j + 搜狗詞庫 -配置并運行
機器上已安裝 : Tomcat 7 jdk1.7 mysql 5.0
- 訪問 http://www.apache.org/dyn/closer.cgi/lucene/solr , 在這個網址里選擇一個路徑 , 下載 solr 3.5 的版本
- solr3.5 在本機解壓縮以后 , 把 apache-solr-3.5.0\example\webapps 目錄下的 solr.war 文件拷貝到 Tomcat 7 的webapps 目錄下
- 啟動Tomcat7,自動解壓solr.war包。然后在 Tomcat 7\webapps\solr 目錄里 新建一個 文件夾 conf
- 把 solr3.5 本機解壓縮文件夾 apache-solr-3.5.0\example 下的 multicore 文件夾 拷貝到 Tomcat 7\webapps\solr\conf 目錄下
- 在 Tomcat 7\conf\Catalina\localhost 目錄下新建一個 solr.xml 文件 , 里面的內容如下 </ol>
- 訪問你的 solr 項目 localhost:1187/solr 會顯示出兩個 core , solr 正常運行
- 配置分詞 , 使用的是 mmseg4j 和 搜狗詞庫
下載地址 : http://code.google.com/p/mmseg4j/ , http://code.google.com/p/mmseg4j/downloads/detail?name=data.zip&can=2&q - 把本地下載的 mmseg4j 解壓縮 , 把里面的mmseg4j-all-1.8.5.jar 文件 拷貝到 Tomcat 7\webapps\solr\WEB-INF\lib 目錄下
- 在 Tomcat 7\webapps\solr 目錄下新建一個 dic 文件夾 , 把 新下載的 詞庫 拷貝到 dic 目錄下
- 在 \Tomcat 7\webapps\solr\conf\multicore\core0\conf\schema.xml 文件的 types 節點里 添加如下節點 : </ol>
- 因為 solr3.5 里有兩個 core , 所以針對 core1 重復 10, 11 兩步
- 對分詞進行測試 , 訪問 http://localhost:1187/solr/core0/admin/analysis.jsp?highlight=on
- Field[Name] 輸入 : complex
- Field Value(index) 輸入 : 中國銀行第一分行 , Field Value(index) 下面的 verbose outpu 點選
- 點擊 Analyze 按鈕 , 查看分詞結果 : 中國銀行 | 第一 | 分行 </ol> </li>
- 此時 Solr3.5 已經可以進行 分詞 , 接下來配置 solr 3.5 連接 mysql 數據庫 , 生成索引 , 進行分詞
- 下載 java 的 mysql 驅動 , 本機解壓 mysql-connector-java-5.1.18-bin.jar, 然后拷貝到 Tomcat 7\webapps\solr\WEB-INF\lib 目錄下
- 在 \Tomcat 7\webapps\solr 目錄下 新建 db 文件夾
- 在 \Tomcat 7\webapps\solr\db 文件夾下面新建一個 db-data-config.xml 文件 , 內容如下 : </ol> </li> </ol>
- 在Tomcat 7\webapps\solr\conf\multicore\core1\conf\solrconfig.xml 路徑里重復 14.4
- 把本地下載解壓的 solr3.5 文件里 , dist 目錄下的 apache-solr-dataimporthandler-3.5.0.jar 和 apache-solr-dataimporthandler-extras-3.5.0.jar Tomcat 7\webapps\solr\WEB-INF\lib 目錄下
- solr3.5 連接 mysql 已經配置完成 , 測試讀取 mysql 生成 索引 , 訪問 : http://localhost:1187/solr/core0/dataimport?command=full-import
- 測試分詞查詢 , 訪問 http://localhost:1187/solr/core0/admin/ 查詢數據庫里索引列里有的詞 </ol>
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password="123"/> <document name="messages"> <entity name="message" transformer="ClobTransformer" query="select * from test"> <field column="ID" name="id" /> <field column="Val" name="text" /> </entity> </document> </dataConfig> <!-- url="jdbc:mysql://localhost:3306/test" user="root" password="123" 這里配置了 mysql 的連接路徑 , 用戶名 , 密碼 <field column="ID" name="id" /><field column="Val" name="text" /> 這里配置的是 數據庫里要索引的字段 , 注意name 是 11 步配置的 -->
在 Tomcat 7\webapps\solr\conf\multicore\core0\conf 目錄下的 solrconfig.xml 文件里 , 添加如下代碼 :<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">D:/Tomcat 7/webapps/solr/db/db-data-config.xml</str> </lst> </requestHandler> <!-- “D:/Tomcat 7/webapps/solr/db/db-data-config.xml” 是 14.3 配置文件的絕對路徑 -->
注意: 這僅僅是配置 solr3.5 連接 mysql 生成索引 , 可以執行正常 詞語 的查詢 , 但是不能執行 對搜索短語的分詞 查詢
multicore 目錄下面多個 core 文件夾 , 每一個都是一個接口 , 有獨立的配置文件 , 處理某一類數據 。
multicore/core0/conf/ 目錄下的 schema.xml 文件 相當于數據表配置文件 , 它定義了加入索引的數據的數據類型 。文件里有一個 <uniqueKey>id</uniqueKey> 的配置 , 這里將 id 字段作為索引文檔的唯一標示符 , 非常重要 。
FieldType 類型 , name 是這個 FieldType 的名稱 , class 指向了 org.apache.solr.analysis 包里面對應的 class 名稱 , 用來定義這個類型的定義 。在 FieldType 定義的時候最重要的就是定義這個類型的數據在建立索引和進行查詢的時候要使用的分析器analyzer,包括分詞和過濾 。
Fields 字段 : 結點內定義具體的字段(類似數據庫中的字段) , 就是 field , 包含 name , type(為之前定義過的各種FieldType) , indexed(是否被索引) , stored(是否被存儲) , multiValued(是否有多個值)
copeField(賦值字段): 建立一個拷貝字段 , 將所有的全文字段復制到一個字段中 , 以便進行統一的檢索 。
來自:http://www.cnblogs.com/hamy/articles/2355413.html
<?xml version="1.0" encoding="UTF-8"?> <Context docBase="${catalina.home}/webapps/solr.war" debug="0" crossContext="true" ><!-- 這里配置的是 Solr 運行的 Home 目錄 -->
<Environment name="solr/home" type="java.lang.String" value="${catalina.home}/webapps/solr/conf/multicore" override="true" /> </Context></pre>
<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="D:/Tomcat 7/webapps/solr/dic"> </tokenizer> </analyzer> </fieldtype> <fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="maxword" dicPath="D:/Tomcat 7/webapps/solr/dic"> </tokenizer> </analyzer> </fieldtype> <fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="D:/Tomcat 7/webapps/solr/dic"> </tokenizer> </analyzer> </fieldtype>
在 \Tomcat 7\webapps\solr\conf\multicore\core0\conf\schema.xml 文件的 fields 節點里 添加如下節點 :<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" type="textMaxWord" indexed="true" stored="true" multiValued="true" />