solrcloud

时间:2021-05-20 06:02:09
 @Test
public void querySolrCloud(){
String zkHost = "127.0.0.1:2181";
String defaultCollection = "test";
try {
CloudSolrServer cloudServer = new CloudSolrServer(zkHost);
cloudServer.setDefaultCollection(defaultCollection);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "*:*");
params.set("start", "0");
QueryResponse response = cloudServer.query(params);
SolrDocumentList results = response.getResults();
for(int i=0; i<results.size(); i++){
System.out.println(results.get(i));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

如下,出现 类似 Exception in thread "main" org.apache.solr.common.SolrException: Unknown document router '{name=compositeId}' 异常时,一般是java客户客户版本不一致导致的错误

 Exception in thread "main" org.apache.solr.common.SolrException: Unknown document router '{name=compositeId}'
at org.apache.solr.common.cloud.DocRouter.getDocRouter(DocRouter.java:46)
at org.apache.solr.common.cloud.ClusterState.collectionFromObjects(ClusterState.java:242)
at org.apache.solr.common.cloud.ClusterState.load(ClusterState.java:219)
at org.apache.solr.common.cloud.ClusterState.load(ClusterState.java:195)
at org.apache.solr.common.cloud.ZkStateReader.createClusterStateWatchersAndUpdate(ZkStateReader.java:286)
at org.apache.solr.client.solrj.impl.CloudSolrServer.connect(CloudSolrServer.java:144)
at org.apache.solr.client.solrj.impl.CloudSolrServer.request(CloudSolrServer.java:168)
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
at com.teshehui.SolrTest.main(SolrTest.java:69)