標籤:style http ar os 使用 java sp for 檔案
1. 編寫設定檔
1)編寫data-config-comment.xml,此檔案用於描述如何查詢MySQL資料,如何將資料變換匯入索引。
假設有一個資料庫叫mooc,其中有個表叫comment,代表學生的評論
其中:
DATE(updatetime) >= ‘${dih.last_index_time}‘ OR DATE(writetime) >= ‘${dih.last_index_time}
表示comment的更新時間updatetime,或者comment的寫入時間writetime比上一次的匯入時間$(dih.last_index_time)還大。
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/mooc" user="root" password="root"/> <document> <entity name="comment" query="SELECT id, DATE_FORMAT(writetime, ‘%Y-%m-%dT%TZ‘) as ‘writetime‘, title from comment"deltaImportQuery="SELECT id, DATE_FORMAT(writetime, ‘%Y-%m-%dT%TZ‘) as ‘writetime‘, title from comment where id=‘${dih.delta.id}‘"deltaQuery="SELECT id FROM comment WHERE DATE(updatetime) >= ‘${dih.last_index_time}‘ OR DATE(writetime) >= ‘${dih.last_index_time}‘"> <field column="id" name="id"/> <field column="writetime" name="writetime"/> <field column="title" name="title"/> </entity> </document></dataConfig>
2)假設要建立一個名為mooc的solr核,在其conf目錄中的schema.xml檔案中編寫fields,加入id,writetime,title,其中text_cn,需要使用我上一則部落格寫的中文分詞外掛程式。
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="writetime" type="tdate" indexed="true" stored="true"/><field name="title" type="text_cn" indexed="true" stored="true"/>
3) 配置Solr的solrconfig.xml
在D:\libs\solr-4.10.2\example\solr\mooc\conf目錄中,建立data-config-comment.xml
在solrconfig.xml中建立資料匯入handler用來匯入comment表,如下編寫,其中的data-config-comment.xml即是第1步寫的
<requestHandler name="/dataimportcomment" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config-comment.xml</str> </lst> </requestHandler>
2.配置使用到的JAVA庫檔案
建立D:\libs\solr-4.10.2\example\solr\mooc\lib,拷貝solr-dataimporthandler-4.10.2和mysql-connector-java-5.1.26-bin到此,這兩個庫用於匯入和查詢資料庫
3.啟動Solr
進入solr的example目錄
java -jar start.jar
3.匯入為索引資料
在瀏覽器運行如下命令做全庫匯入,表示將資料,匯入到Solr核mooc中
http://localhost:8983/solr/mooc/dataimportcomment?command=full-import&commit=y
如果帶clean=false參數,則表示不刪除原資料
增量匯入
http://localhost:8983/solr/mooc/dataimportcomment?command=delta-import
4.刪除索引檔案
編寫一個XML檔案,內容為
<delete><query>*:*</query></delete>
執行命令
java -Durl=http://localhost:8983/solr/mooc/update -jar post.jar delete_docs.xml
5. 查詢資料
1)使用Solr的edismax query,查詢“的”字,返回ID,title,和writetime,以json格式,可以改成wt=xml,表示使用xml格式,但為了程式處理方便,用json
http://localhost:8983/solr/mooc/select?q=的&wt=json&indent=true&defType=edismax&qf=title&omitHeader=true&fl=id,title,writetime
2)精簡的,只查出來ID
http://localhost:8983/solr/mooc/select?q=的&wt=json&defType=edismax&qf=title&omitHeader=true&fl=id
omitHeader=true表示取出資料資訊
3)查詢日期,使用如下參數
到目前為止:
fq=writetime:[* TO NOW]
從2011年8月19號到2014年8月19號:
fq=writetime:[2011-08-19T11:50:23Z TO 2014-08-19T11:50:23Z]
從2011年8月19號以後:
fq=writetime:[2011-08-19T11:50:23Z TO *]
4)高亮顯示關鍵字
http://localhost:8983/solr/mooc/select?q=好的&wt=json&indent=true&defType=edismax&qf=title&omitHeader=true&fl=id,title,writetime&hl=true
hl=true表示開啟高亮顯示
6.前端搜尋操作流程
前端java script 發請求給後端的servlet,servlet向搜尋引擎發出查詢請求,返回結果給前端
7.定時增量匯入資料
1)先寫匯入指令碼sh_delta_import.sh
#!/bin/shcurl "http://localhost:8983/solr/mooc/dataimportcomment?command=delta-import"curl "http://localhost:8983/solr/mooc/dataimportcomment?command=delta-import"
重複一次,是因為Solr可能會在第一次返回一個警告,再執行一次就匯入了。
2)配置/etc/crontab,添加一行,表示每天4點執行
00 04 * * * root /mooc/solr-4.10.2/example/sh_delta_import.sh
3)重啟定時程式
/etc/rc.d/init.d/crond restart
Solr配置匯入MySQL資料