Solr安裝及使用示例

翻譯自Validating the Deployment with the Solr REST API,在CDH集羣上進行如下步驟。


Solr安裝

在Cloudera Manager Server的控制檯界面,直接點擊“Add Service”,找到Solr,將其添加進來即可。當然,前提是ZooKeeper得安裝好。


初始化collection

以下操作在隨便一臺運行sorl進程的主機(以下用$SOLRHOST來表示,當然一般也就是安裝Solr Server的那臺主機)上進行,

  1. 創建測試目錄

    mkdir solr-test
    cd solr-test
    
  2. 產生collection所需的配置文件

    solrctl instancedir --generate $HOME/solr_configs
    
  3. 上傳實例目錄到ZooKeeper

    solrctl instancedir --create collection1 $HOME/solr_configs
    
  4. 創建新的collection

    solrctl collection --create collection1 -s 2 -c collection1
    

爲數據創建索引

採用CDH自帶的示例數據建立索引。

  1. 若採用Parcel安裝的CDH

    cd /opt/cloudera/parcels/CDH/share/doc/solr-doc*/example/exampledocs
    
    java -Durl=http://$SOLRHOST:8983/solr/collection1/update -jar post.jar *.xml
    

運行查詢

打開Solr的Web界面--http://$SOLRHOST:8983/solr/#/的“Core Selector ”,在上面選中剛創建的“collection1_shard1_replica1”。注意:使用火狐瀏覽器並不能選中collection,請用Chrome打開。

solr-initial-test


附加

以上的collection創建成功後,可以在HUE的Search界面直接查到。比如,在HUE的search界面–http://hue-server:8888/search可以查到剛剛創建的collection1,據此創建相應的Dashboard。見HUE中集成Solr.md。


錯誤解決

在運行第四步--”4. 創建新的collection”時,報錯

 <?xml version="1.0" encoding="UTF-8"?> <response> <lst name="responseHeader"> <int name="status"> 400</int> <int name="QTime"> 126</int> </lst> <str name="Operation createcollection caused exception:"> org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Cannot create collection collection1. Value of maxShardsPerNode is 1, and the number of live nodes is 1. This allows a maximum of 1 to be created. Value of numShards is 2 and value of replicationFactor is 1. This requires 2 shards to be created (higher than the allowed number)</str> <lst name="exception"> <str name="msg"> Cannot create collection collection1. Value of maxShardsPerNode is 1, and the number of live nodes is 1. This allows a maximum of 1 to be created. Value of numShards is 2 and value of replicationFactor is 1. This requires 2 shards to be created (higher than the allowed number)</str> <int name="rspCode"> 400</int> </lst> <lst name="error"> <lst name="metadata"> <str name="error-class"> org.apache.solr.common.SolrException</str> <str name="root-error-class"> org.apache.solr.common.SolrException</str> </lst> <str name="msg"> Cannot create collection collection1. Value of maxShardsPerNode is 1, and the number of live nodes is 1. This allows a maximum of 1 to be created. Value of numShards is 2 and value of replicationFactor is 1. This requires 2 shards to be created (higher than the allowed number)</str> <int name="code"> 400</int> </lst> </response>

改爲

solrctl collection --create collection1 -s 1 -c collection1

其它

我們可以在查詢條件中輸入關鍵字進行查詢,得到搜索結果後,我們往往會需要實現搜索結果中關鍵字的高亮, 參見 jQuery的highlight插件使用示例 ,可以簡單地使用插件實現該功能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章