轉載請標明出處:http://www.cnblogs.com/chlde/p/3768733.html
1.如何將solr部署,請參考之前的文章
2.按上述配置好后,在solr_home文件夾中,將包含collection1文件夾,這就是solr的一個實例。下面我們來看看collection1中的文件內容。
collection1中包含conf和data兩個子文件夾。data中包含tlog和index(如果沒有也沒關系,稍后再solr建立索引時,將會被創(chuàng)建)。tlog是記錄日志的文件夾,index是存放索引的文件夾。conf中包含lang文件夾和若干文件。lang文件夾中包含的是詞庫文件,但是solr默認是沒有中文詞庫的,所以之后會將中文詞庫加入該文件夾中。在conf中,包含了若干xml文件,我們針對solr配置,是需要配置solrconfig.xml和schema.xml即可。下面我們講一下如何配置這兩個文件。
3.先配置solrconfig.xml。solrconfig.xml是solr的核心文件。這里包含了jar包引用,數(shù)據(jù)庫讀取路徑配置,操作接口配置。
jar包配置如下
1 < lib dir ="../contrib/extraction/lib" regex =".*\.jar" /> 2 < lib dir ="../dist/" regex ="solr-cell-\d.*\.jar" /> 3 4 < lib dir ="../contrib/clustering/lib/" regex =".*\.jar" /> 5 < lib dir ="../dist/" regex ="solr-clustering-\d.*\.jar" /> 6 7 < lib dir ="../contrib/langid/lib/" regex =".*\.jar" /> 8 < lib dir ="../dist/" regex ="solr-langid-\d.*\.jar" /> 9 10 < lib dir ="../contrib/velocity/lib" regex =".*\.jar" /> 11 < lib dir ="../dist/" regex ="solr-velocity-\d.*\.jar" /> 12 13 < lib dir ="../contrib/dataimporthandler/lib" regex =".*\.jar" /> 14 < lib dir ="../dist/" regex ="solr-dataimporthandler-\d.*\.jar" />
其中,最后兩行是數(shù)據(jù)導入的handler,這包含了從數(shù)據(jù)庫讀取數(shù)據(jù)所需要的jar包。這些jar的目錄都在solr_home\contrib這個文件夾中。
配置dataimporthandler
< requestHandler name ="/dataimport" class ="org.apache.solr.handler.dataimport.DataImportHandler" > < lst name ="defaults" > < str name ="config" > data-config.xml </ str > </ lst > </ requestHandler >
這里需要你創(chuàng)建一個新的xml文件,放在conf文件夾中,命名為data-config.xml。內容如下
1 < dataConfig > 2 < dataSource type ="JdbcDataSource" 3 driver ="com.mysql.jdbc.Driver" 4 url ="jdbc:mysql://localhost/yourDBname" 5 user ="root" 6 password ="root" /> 7 < document >
8 < entity name ="question1" query ="select Guid,title,QuesBody,QuesParse,QuesType from question1 where Guid is not null" > 9 < field column ="Guid" name ="id" /> 10 < field column ="title" name ="question1_title" /> 11 < field column ="QuesBody" name ="question1_body" /> 12 < field column ="QuesParse" name ="question1_parse" /> 13 < field column ="QuesType" name ="question1_type" /> 14 </ entity > 15 < entity name ="question2" query ="select Guid,title,QuesBody,QuesParse,QuesType from question2 where Guid is not null" > 16 < field column ="Guid" name ="id" /> 17 < field column ="title" name ="question2_title" /> 18 < field column ="QuesBody" name ="question2_body" /> 19 < field column ="QuesParse" name ="question2_parse" /> 20 < field column ="QuesType" name ="question2_type" /> 21 </ entity > 22 </ document > 23 </ dataConfig >
如上,包含了datasource和document兩個大標簽。datasource正如其名,包含了數(shù)據(jù)庫的配置信息。document包含了entity。entity就是一個從數(shù)據(jù)庫讀取數(shù)據(jù)的動作。
query就是讀取數(shù)據(jù)所用的sql,field是數(shù)據(jù)庫中的字段與schma中的字段進行匹配的列表。稍后在schma.xml的介紹中,將會詳細說明。
我們回到solrconfig.xml中,requestHandler這里定義了相應http請求的接口。如之前配置的name為/dataimport接口,在中間件啟動后,訪問http://localhost:8080/solr/collection1/dataimport即可查看數(shù)據(jù)導入的狀態(tài)。若執(zhí)行命令,便可執(zhí)行http://localhost:8080/solr/collection1/dataimport
?command=full-import
?即可(這句的含義是全部重新索引,之前的索引將被刪除),其他命令,請參考http://www.cnblogs.com/llz5023/archive/2012/11/15/2772154.html。同理,通過相同的形式,即可實現(xiàn)對solr的增刪改查。這里還能對requestHandler進行一些高級配置,感興趣的同學可以到apache-solr-ref-guide-4.8中閱讀。
4.schma.xml配置。schma.xml完成了對索引數(shù)據(jù)的類型配置和索引一些相關動作的配置(如分詞方法配置)。
solr需要為每條索引定義一個id作為主鍵,而且在查詢中必須要有字段與主鍵id進行對應,否則將會報錯。如在data-config中的Guid與id進行匹配,將guid作為主鍵。
field為solr索引的基本類型,type的值與fieldType對應,即通過type為每個field指定一個fieldType,而fieldType將為field規(guī)定如何進行索引。
例如,我們將用mmseg4j對中文進行索引
1 <!-- Chinese --> 2 < fieldType name ="text_chn_complex" class ="solr.TextField" > 3 < analyzer > 4 < tokenizer class ="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode ="complex" dicPath ="lang/chn.txt" /> 5 </ analyzer > 6 </ fieldType > 7 < fieldType name ="text_chn_maxword" class ="solr.TextField" > 8 < analyzer > 9 < tokenizer class ="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode ="max-word" dicPath ="lang/chn.txt" /> 10 </ analyzer > 11 </ fieldType > 12 < fieldType name ="text_chn_simple" class ="solr.TextField" > 13 < analyzer > 14 < tokenizer class ="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode ="simple" dicPath ="lang/chn.txt" /> 15 </ analyzer > 16 </ fieldType >
如上,我們定義了三個fieldType,這三個表示了對中文進行索引的三種方式。都屬于solr.TextField類。analyzer均為mmseg4j,只是使用的mode不同。dicPath即為詞庫所在位置。
1 < field name ="question1_type" type ="text_chn_maxword" indexed ="true" stored ="true" />
這里定義了一個名為question1_type的field,使用text_chn_maxword方式進行索引。
這里有一點是要注意的,solr中是沒有and的,所以,要在多個字段查詢匹配的關鍵字,要使用到copyField這個類型。
例如
1 < field name ="question2_title" type ="text_chn_maxword" indexed ="true" stored ="true" /> 2 < field name ="question2_body" type ="text_chn_maxword" indexed ="true" stored ="true" /> 3 4 < field name ="question2_text" type ="text_chn_maxword" indexed ="true" stored ="true" multiValued ="true" /> 5 < copyField source ="question2_title" dest ="question2_text" /> 6 < copyField source ="question2_body" dest ="question2_text" />
這里就是將question2_title和question2_body共同索引到question2_text中,這樣只要question2_title或question2_body任意被關鍵字匹配,就會將question2_text返回。注意question2_text的multiValued="true",這點是必須的。
5.遇到的問題
中文詞庫下載
http://download.labs.sogou.com/dl/sogoulabdown/SogouW/SogouW.zip
mmseg4j需要使用2.0以上版本,2.0一下在solr4.8中會有bug
https://code.google.com/p/mmseg4j/
JAVA工程師:chlde2500@gmail.com
?
solr 4.8+mysql數(shù)據(jù)庫數(shù)據(jù)導入 + mmseg4j中文全文索引 配置筆記
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
