Solr是java開發。java
須要安裝jdk。web
安裝環境Linux。apache
須要安裝Tomcat。windows
第一步:把solr 的壓縮包上傳到Linux系統tomcat
第二步:解壓solr。複製solr.jar包到tomcat/webapps下app
cp solr-4.10.3.war /usr/local/solr-cloud/tomcat-01/webapps/solr.warwebapp
第三步:安裝Tomcat,解壓縮便可。單元測試
第四步:把solr部署到Tomcat下。測試
第五步:解壓縮war包。啓動Tomcat解壓。url
第六步:把/usr/local/solr-cloud/solr-4.10.3/example/lib/ext目錄下的全部的jar包,添加到solr工程中。(啓動須要的日誌文件)
[root@localhost ext]# pwd
/root/solr-4.10.3/example/lib/ext
[root@localhost ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
第七步:建立一個solrhome。/example/solr目錄就是一個solrhome。複製此目錄到/usr/local/solr/solrhome
[root@localhost example]# pwd
/root/solr-4.10.3/example
[root@localhost example]# cp -r solr /usr/local/solr/solrhome
[root@localhost example]#
第八步:關聯solr及solrhome。須要修改solr工程的web.xml文件。
第九步:啓動Tomcat
http://192.168.254.130:8080/solr/
和windows下的配置徹底同樣。
schema.xml中定義
一、商品Id
二、商品標題
三、商品賣點
四、商品價格
五、商品圖片
六、分類名稱
建立對應的業務域。須要制定中文分析器。
建立步驟:
第一步:把中文分析器添加到工程中。
一、把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目錄下
二、把擴展詞典、配置文件放到solr工程的WEB-INF/classes目錄下。
# mkdir /usr/local/solr/apache-tomcat-7.0.47/webapps/solr/WEB-INF/classes
# cp ext_stopword.dic IKAnalyzer.cfg.xml mydict.dic /usr/local/solr/apache-tomcat-7.0.47/webapps/solr/WEB-INF/classes
第二步:配置一個FieldType,制定使用IKAnalyzer
修改schema.xml文件
修改Solr的schema.xml文件,添加FieldType:
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> |
第三步:配置業務域,type制定使用自定義的FieldType。
設置業務系統Field
<field name="item_title" type="text_ik" indexed="true" stored="true"/> <field name="item_sell_point" type="text_ik" indexed="true" stored="true"/> <field name="item_price" type="long" indexed="true" stored="true"/> <field name="item_image" type="string" indexed="false" stored="true" /> <field name="item_category_name" type="string" indexed="true" stored="true" />
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField source="item_title" dest="item_keywords"/> <copyField source="item_sell_point" dest="item_keywords"/> <copyField source="item_category_name" dest="item_keywords"/> |
第四步:重啓tomcat
本地訪問
solr單元測試
1.新建solr索引對象
SerachItem
private String id; private String title; private String sell_point; private long price; private String image; private String category_name;
2.測試solr
public class SerachTest { /** * 添加文檔 * * @throws SolrServerException * @throws IOException */ @Test public void addDocument() throws SolrServerException, IOException { // 建立一個solrService對象,建立一個鏈接。參數爲solr服務的url
HttpSolrServer httpSolrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/collection1"); // 建立一個文檔的對象SolrInputDocument
SolrInputDocument document = new SolrInputDocument(); // 向文檔對象中添加域。文檔中必須包含一個id域,全部域必須在s'chema.xml中定義
document.addField("id", "doc01"); document.addField("item_title", "測試01"); // 把文檔寫入索引庫
httpSolrServer.add(document); // 提交
httpSolrServer.commit(); } /** * 刪除文檔 */
public void deleteDocument() throws Exception { // 建立一個solrservice對象。鏈接solr
HttpSolrServer httpSolrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/collection1"); // 刪除文檔
httpSolrServer.deleteById("doc01"); // 提交
httpSolrServer.commit(); } }
添加操做