一般要搜索的信息都是被存儲在數(shù)據(jù)庫里面的夯膀,但是我們不能直接搜數(shù)據(jù)庫哨查,所以只有借助Solr將要搜索的信息在搜索服務器上進行索引,然后在客戶端供客戶使用镊绪。
一、鏈接數(shù)據(jù)庫
1. SQL配置
拿SQL Server 為例洒忧,需要先下載Sql Server的jar包蝴韭,下載地址:
http://msdn.microsoft.com/en-us/data/aa937724.aspx
解壓縮之后將sqljdbc4.jar復制到webapps\solr\WEB-INF\lib下,也就是本例的:
D:\apache-tomcat-7.0.57\webapps\solr\WEB-INF\lib
2. 建立查詢
然后在D:\apache-tomcat-7.0.57\webapps\solr\solr_home\collection1\conf下面新建一個配置文件:data-config.xml
然后在本目錄下編輯配置文件:solrconfig.xml
找到很多個requestHandler節(jié)點熙侍,在最下面增加:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"><lst name="defaults"><str name="config">D:\apache-tomcat-7.0.57\webapps\solr\solr_home\collection1\conf\data-config.xml</str></lst></requestHandler>
要配置的內(nèi)容就是剛才新建文件的路徑榄鉴。
3. 將初次下載的solr-4.10.2文件夾下的dist, contrib文件夾復制到TomCat根目錄下:
4. 然后再次修改剛才編輯過的solrconfig.xml增加兩個文件夾的配置并修改一些參數(shù):
大約在75行默認有8個路徑,但是路徑不一定全對核行,順著這個配置的路徑去找文件夾牢硅,根據(jù)查找的層數(shù)修改這個路徑配置蹬耘,另增加一個新的配置芝雪,全文如下:
<lib dir="D:\apache-tomcat\contrib\extraction\lib" regex=".*\.jar" />
? <lib dir="D:\apache-tomcat\dist\" regex="solr-cell-\d.*\.jar" />
? <lib dir="D:\apache-tomcat\contrib\clustering\lib\" regex=".*\.jar" />
? <lib dir="D:\apache-tomcat\dist\" regex="solr-clustering-\d.*\.jar" />
? <lib dir="D:\apache-tomcat\contrib\langid\lib\" regex=".*\.jar" />
? <lib dir="D:\apache-tomcat\dist\" regex="solr-langid-\d.*\.jar" />
? <lib dir="D:\apache-tomcat\contrib\velocity\lib" regex=".*\.jar" />
? <lib dir="D:\apache-tomcat\dist\" regex="solr-velocity-\d.*\.jar" />
? <lib dir="D:\apache-tomcat\dist\" regex="solr-dataimporthandler-\d.*\.jar" />
這個配置是根據(jù)本機推導出來的,大家根據(jù)自己的實際情況去修改综苔。
5. 將dist文件夾下的
復制到webapps\solr\WEB-INF\lib下惩系。
6. 然后編輯
D:\apache-tomcat-7.0.57\webapps\solr\solr_home\collection1\conf\data-confing.xml文件,以本機MyBookShop數(shù)據(jù)庫的Books表為例
配置如下:
<?xml version="1.0" encoding="UTF8"?><dataConfig><dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=MyBookShop" user="sa" password="111"/><document name="Info" pk="id"><entity name="zpxx"? transformer="ClobTransformer" pk="id"? ? ? ? ? ? ? ? query="SELECT? [ID],[Title] as name? FROM [MyBookShop].[dbo].[Books]"? ? ?
? ? ? ? ? ? ? ? deltaImportQuery="SELECT? [Id],[Title] as name? FROM [MyBookShop].[dbo].[Books] where [PublishDate] > '${dataimporter.last_index_time}'"?
? ? ? ? ? ? ? ? deltaQuery="SELECT? id FROM [MyBookShop].[dbo].[Books] where [PublishDate] > '${dataimporter.last_index_time}'"><field column="id"? ? ? name="id"/><field column="name"? ? ? name="name"/></entity></document></dataConfig>
此配置千萬注意如筛。
上述配置說明如下:
???? query是獲取全部數(shù)據(jù)的SQL(solr從sql中獲取那些數(shù)據(jù))堡牡,多列
???? deltaImportQuery是獲取增量數(shù)據(jù)時使用的SQL(數(shù)據(jù)庫新增數(shù)據(jù)追加到solr的數(shù)據(jù)),多列
???? deltaQuery是獲取pk的SQL(數(shù)據(jù)庫新增數(shù)據(jù)是,追加到solr的數(shù)據(jù)時的條件,根據(jù)id ,條件是最后一次獲取的時間,${dataimporter.last_index_time,最后獲取的時間})杨刨,一列
如需配置多列晤柄,步驟如:
1.先配置數(shù)據(jù)庫查詢信息data config:
<entity?name="zpxx"? transformer="ClobTransformer" pk="id"
?????????query="SELECT [ProductID] as ID,[ShopProductTitle] as title1,[SecondTitle] as title2,[ShopProductDescription] as descr? FROM [dbo].[ShopProduct]"??????
?????????deltaImportQuery="SELECT [ProductID] as ID,[ShopProductTitle] as title1,[SecondTitle] as title2,[ShopProductDescription] as descr? FROM [dbo].[ShopProduct] where [PublishDate] > '${dataimporter.last_index_time}'"??
?????????deltaQuery="SELECT [ProductID] as ID FROM [dbo].[ShopProduct] where [PublishDate] > '${dataimporter.last_index_time}'">???????????
????????<field?column="id"????? name="id"????? />
<field?column="title1"????? name="title1"????? />
<field?column="title2"????? name="title2"????? />
????????<field?column="descr"????? name="descr"????? />
?</entity>
?2. 配置schema信息(約120多行處):
<field?name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field?name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
<field?name="title1" type="text_ik" indexed="true" stored="true"/>
<field?name="title2" type="text_ik" indexed="true" stored="true"/>
<field?name="descr" type="text_ik" indexed="true" stored="true"/>
<field?name="cat" type="string" indexed="true" stored="true" multiValued="true"/>
?這樣查處的結果就如:
"docs": [
??{
????"title1": "櫻桃",
????"title2": "櫻桃櫻桃",
????"id": "1004",
????"descr": "<img?alt=\"\" src=\"9f531.jpg\"><img?alt=\"\" src=\"5.jpg\">",
????"_version_": 1503960451691577300
??},
保證SQL SERVER配置了外網(wǎng)訪問即可。
配置到此基本結束妖胀。
二芥颈、建立索引
啟動Solr惠勒,刪除全部索引數(shù)據(jù):
http://localhost:8080/solr/update/?stream.body=<delete><query>*:*</query></delete>&stream.contentType=text/xml;charset=utf-8&commit=true
停掉Solr,檢查下是不是清空了:
開始創(chuàng)建新的索引爬坑,瀏覽器執(zhí)行方式:
終止跑索引:http://localhost:8080/solr/collection1/dataimport?command=abort
開始索引:http://localhost:8080/solr/collection1/dataimport?command=full-import
增量索引 :http://localhost:8080/solr/collection1/dataimport?command=delta-import
建議學習時使用UI的方式進行更新纠屋,能看清運行過程:
需要等執(zhí)行結束,可以點擊【Refrush status】看結果:
Indexing completed. Added/Updated: 1076 documents. Deleted 0 documents. (Duration: 03s)
表示結束盾计。
然后點擊左邊的查詢檢驗索引結果:
出現(xiàn)查詢結果表示創(chuàng)建成功售担。
三、更新索引署辉、增加索引族铆、刪除索引
1. 更新數(shù)據(jù):
我們先來搜索一個關鍵字“土豆”,這里全是計算機圖書哭尝,不應該有土豆的骑素。
然后將其中一個書籍的標題更新為“論土豆的栽培技術”
不更新索引是搜不到的。
執(zhí)行增量更新:
http://localhost:8080/solr/dataimport?command=delta-import&clean=false&commit=true
然后去查詢:
已經(jīng)可以查詢得到了刚夺。
2. 新增一個數(shù)據(jù):“論蘋果的藝術”
insert into [MyBookShop].[dbo].[Books]
(
[Title]
,[Author]
,[PublishDate]
,[WordsCount]
,[UnitPrice]
,[ContentDescription]
,[AurhorDescription]
,[EditorComment]
,[TOC]
)
SELECT '論蘋果的藝術'
,[Author]
,[PublishDate]
,[WordsCount]
,[UnitPrice]
,[ContentDescription]
,[AurhorDescription]
,[EditorComment]
,[TOC]
FROM [MyBookShop].[dbo].[Books] where id=4942
新增結果如下:?
然后執(zhí)行增量索引:
http://localhost:8080/solr/dataimport?command=delta-import&clean=false&commit=true
再查詢:
發(fā)現(xiàn)新結果被查詢出來了献丑。
3. 刪除數(shù)據(jù)
? 將蘋果這條數(shù)據(jù)刪除掉,仍然能夠查詢出來侠姑,執(zhí)行單條索引刪除:
再次查詢就查不到了:
數(shù)據(jù)庫下載