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插件使用示例 ,可以简单地使用插件实现该功能。

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