Solr查询报错:org.apache.solr.common.SolrException: Collection not found 处理

1、报错1 org.apache.solr.common.SolrException: Collection not found

org.apache.solr.common.SolrException: Collection not found: dem6
	at org.apache.solr.client.solrj.impl.CloudSolrServer.getCollectionList(CloudSolrServer.java:655)
	at org.apache.solr.client.solrj.impl.CloudSolrServer.request(CloudSolrServer.java:539)
	at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:91)
	at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
	at com.xc.os.sys.entity.Test.main(Test.java:23)

我solr集群的版本是6.0.0
在这里插入图片描述
我后台solrj的版本是4.7.2,maven中配的是lucene 4.6.0
在这里插入图片描述
solrj 4.7.2 对应的后台代码如下:

public static void main(String[] args) {
		try {
			String zkHost = "192.168.1.21:2181,192.168.1.21:2182,192.168.1.21:2183";
			
			CloudSolrServer server = new CloudSolrServer(zkHost);
	        server.setDefaultCollection("dem6");
	        server.connect();
	        
	        SolrQuery solrQuery = new SolrQuery();
	        solrQuery.setQuery("*:*");
	        
	        QueryResponse queryResponse = server.query(solrQuery);
	        SolrDocumentList solrDocumentList = queryResponse.getResults();
	        System.out.println("数量:" + solrDocumentList.getNumFound());
	        for (SolrDocument solrDocument : solrDocumentList) {
	               System.out.println(solrDocument.get("content"));
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

2、报错1处理方法

2.1 pom.xml修改,将solr的版本由4.6.0改为6.0.0

去掉<lucene.version>4.6.0</lucene.version>

添加

<dependency>
	<groupId>org.apache.solr</groupId>
	<artifactId>solr-solrj</artifactId>
	<version>6.0.0</version>
</dependency>

修改后,maven重新更新一个引用

2.2 修改后台代码

public static void main(String[] args) {
		try {
			String zkHost = "192.168.1.21:2181,192.168.1.21:2182,192.168.1.21:2183";
			
			CloudSolrClient client = new CloudSolrClient(zkHost);
	        client.setZkClientTimeout(30000);
	        client.setZkConnectTimeout(50000);
	        client.setDefaultCollection("dem6");
	        client.connect();
			
	        SolrQuery solrQuery = new SolrQuery();
	        solrQuery.setQuery("*:*");
	        QueryResponse queryResponse = client.query(solrQuery);
	        SolrDocumentList solrDocumentList = queryResponse.getResults();
	        System.out.println("数量:" + solrDocumentList.getNumFound());
	        for (SolrDocument solrDocument : solrDocumentList) {
	               System.out.println(solrDocument.get("content"));
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

运行结果如图:

在这里插入图片描述
与solr页面中查到的数量进行对比
在这里插入图片描述

3、报错2 No live SolrServers available to handle this request

2020-02-23 18:51:40.459  ERROR o.a.s.c.solrj.impl.CloudSolrClient - Request to collection dem6 failed due to (404) org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.1.21:8280/solr/dem6: Expected mime type application/octet-stream but got text/html. <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> &#47;solr&#47;dem6&#47;question_select</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.50</h3></body></html>, retry? 0
org.apache.solr.client.solrj.SolrServerException: No live SolrServers available to handle this request:[http://192.168.1.21:8180/solr/dem6, http://192.168.1.21:8380/solr/dem6, http://192.168.1.21:8480/solr/dem6, http://192.168.1.21:8280/solr/dem6]
	at org.apache.solr.client.solrj.impl.LBHttpSolrClient.request(LBHttpSolrClient.java:352)
	at org.apache.solr.client.solrj.impl.CloudSolrClient.sendRequest(CloudSolrClient.java:1100)
	at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:870)
	at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:806)
	at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)
	at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:942)
	at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:957)
	at com.xc.os.sys.entity.Test.main(Test.java:30)
Caused by: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.1.21:8280/solr/dem6: Expected mime type application/octet-stream but got text/html. <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> &#47;solr&#47;dem6&#47;question_select</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.50</h3></body></html>
	at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:545)
	at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:241)
	at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:230)
	at org.apache.solr.client.solrj.impl.LBHttpSolrClient.doRequest(LBHttpSolrClient.java:372)
	at org.apache.solr.client.solrj.impl.LBHttpSolrClient.request(LBHttpSolrClient.java:325)

4、报错2处理方法

public static void main(String[] args) {
		try {
			String zkHost = "192.168.1.21:2181,192.168.1.21:2182,192.168.1.21:2183";
			
			CloudSolrClient client = new CloudSolrClient(zkHost);
	        client.setDefaultCollection("dem6");
	        client.connect();
	        
	        SolrQuery solrQuery = new SolrQuery();
	        //solrQuery.setRows(Integer.MAX_VALUE);
	        solrQuery.setRows(500);
	        
	        solrQuery.set("qt", "/question_select");
	        //solrQuery.setQuery("*:*");     
	        //solrQuery.set("qt", "/select");
	        
	        QueryResponse queryResponse = client.query(solrQuery);
	        SolrDocumentList solrDocumentList = queryResponse.getResults();
	        System.out.println("数量:" + solrDocumentList.getNumFound());
	        for (SolrDocument solrDocument : solrDocumentList) {
	               System.out.println(solrDocument.get("content"));
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

4.1 如上图 修改solrQuery.set(“qt”, “/question_select”);

solrQuery.setQuery("*:*"); 

或者核心对solr的配制文件solrconfig.xml中有没有question_select,没有的话改为有的,如 下图

solrQuery.set("qt", "/question");

在这里插入图片描述

4.2 solrQuery.setRows(Integer.MAX_VALUE);也会引发报错2

可以设置rows为根据你自已的业务量设置一个合适的数量,我测过20000,是可以的

solrQuery.setRows(20000);

5、小结

3.1 solr使用时,solr的版本要与solrj的版本保持一致。

3.2 solrj各版本之间的差别比较大,solrj4核心类是CloudSolrServer,solrj6核心类是CloudSolrClient。

发布了122 篇原创文章 · 获赞 152 · 访问量 112万+

猜你喜欢

转载自blog.csdn.net/ytangdigl/article/details/104452911