轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://www.cnblogs.com/chlde/p/3768733.html
1.如何將solr部署,請(qǐng)參考之前的文章
2.按上述配置好后,在solr_home文件夾中,將包含collection1文件夾,這就是solr的一個(gè)實(shí)例。下面我們來(lái)看看collection1中的文件內(nèi)容。
collection1中包含conf和data兩個(gè)子文件夾。data中包含tlog和index(如果沒(méi)有也沒(méi)關(guān)系,稍后再solr建立索引時(shí),將會(huì)被創(chuàng)建)。tlog是記錄日志的文件夾,index是存放索引的文件夾。conf中包含lang文件夾和若干文件。lang文件夾中包含的是詞庫(kù)文件,但是solr默認(rèn)是沒(méi)有中文詞庫(kù)的,所以之后會(huì)將中文詞庫(kù)加入該文件夾中。在conf中,包含了若干xml文件,我們針對(duì)solr配置,是需要配置solrconfig.xml和schema.xml即可。下面我們講一下如何配置這兩個(gè)文件。
3.先配置solrconfig.xml。solrconfig.xml是solr的核心文件。這里包含了jar包引用,數(shù)據(jù)庫(kù)讀取路徑配置,操作接口配置。
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ù)導(dǎo)入的handler,這包含了從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)所需要的jar包。這些jar的目錄都在solr_home\contrib這個(gè)文件夾中。
配置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)建一個(gè)新的xml文件,放在conf文件夾中,命名為data-config.xml。內(nèi)容如下
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兩個(gè)大標(biāo)簽。datasource正如其名,包含了數(shù)據(jù)庫(kù)的配置信息。document包含了entity。entity就是一個(gè)從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)的動(dòng)作。
query就是讀取數(shù)據(jù)所用的sql,field是數(shù)據(jù)庫(kù)中的字段與schma中的字段進(jìn)行匹配的列表。稍后在schma.xml的介紹中,將會(huì)詳細(xì)說(shuō)明。
我們回到solrconfig.xml中,requestHandler這里定義了相應(yīng)http請(qǐng)求的接口。如之前配置的name為/dataimport接口,在中間件啟動(dòng)后,訪問(wèn)http://localhost:8080/solr/collection1/dataimport即可查看數(shù)據(jù)導(dǎo)入的狀態(tài)。若執(zhí)行命令,便可執(zhí)行http://localhost:8080/solr/collection1/dataimport
?command=full-import
?即可(這句的含義是全部重新索引,之前的索引將被刪除),其他命令,請(qǐng)參考http://www.cnblogs.com/llz5023/archive/2012/11/15/2772154.html。同理,通過(guò)相同的形式,即可實(shí)現(xiàn)對(duì)solr的增刪改查。這里還能對(duì)requestHandler進(jìn)行一些高級(jí)配置,感興趣的同學(xué)可以到apache-solr-ref-guide-4.8中閱讀。
4.schma.xml配置。schma.xml完成了對(duì)索引數(shù)據(jù)的類型配置和索引一些相關(guān)動(dòng)作的配置(如分詞方法配置)。
solr需要為每條索引定義一個(gè)id作為主鍵,而且在查詢中必須要有字段與主鍵id進(jìn)行對(duì)應(yīng),否則將會(huì)報(bào)錯(cuò)。如在data-config中的Guid與id進(jìn)行匹配,將guid作為主鍵。
field為solr索引的基本類型,type的值與fieldType對(duì)應(yīng),即通過(guò)type為每個(gè)field指定一個(gè)fieldType,而fieldType將為field規(guī)定如何進(jìn)行索引。
例如,我們將用mmseg4j對(duì)中文進(jìn)行索引
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 >
如上,我們定義了三個(gè)fieldType,這三個(gè)表示了對(duì)中文進(jìn)行索引的三種方式。都屬于solr.TextField類。analyzer均為mmseg4j,只是使用的mode不同。dicPath即為詞庫(kù)所在位置。
1 < field name ="question1_type" type ="text_chn_maxword" indexed ="true" stored ="true" />
這里定義了一個(gè)名為question1_type的field,使用text_chn_maxword方式進(jìn)行索引。
這里有一點(diǎn)是要注意的,solr中是沒(méi)有and的,所以,要在多個(gè)字段查詢匹配的關(guān)鍵字,要使用到copyField這個(gè)類型。
例如
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任意被關(guān)鍵字匹配,就會(huì)將question2_text返回。注意question2_text的multiValued="true",這點(diǎn)是必須的。
5.遇到的問(wèn)題
中文詞庫(kù)下載
http://download.labs.sogou.com/dl/sogoulabdown/SogouW/SogouW.zip
mmseg4j需要使用2.0以上版本,2.0一下在solr4.8中會(huì)有bug
https://code.google.com/p/mmseg4j/
JAVA工程師:chlde2500@gmail.com
?
solr 4.8+mysql數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)入 + mmseg4j中文全文索引 配置筆記
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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