使用 Java 中的 Solrj 连接到 Solr 时出错
Error Connecting to Solr using Solrj from Java
在我的本地机器上,我安装了 Solr v8.4.1 来做一些测试,并创建了一个核心,使用 json 文件添加了一些数据,并且可以连接到“http://127.0.0.1:8983/solr/ 上的管理客户端”。
我现在正在尝试使用 Lotus Domino 代理通过 Solrj (Java) 连接到实例。
solrServer = new HttpSolrServer("http://127.0.0.1:8983/solr/");
solrServer.setSoTimeout(10000);
solrServer.setConnectionTimeout(10000);
solrServer.setDefaultMaxConnectionsPerHost(100);
solrServer.setMaxTotalConnections(100);
solrServer.setFollowRedirects(false);
solrServer.setAllowCompression(true);
solrServer.setMaxRetries(1);
// solrServer.setParser(new XMLResponseParser());
System.out.println(solrServer.getBaseURL());
SolrQuery query = new SolrQuery();
query.setQuery( "*:*" );
try {
System.out.println("queryyyy");
QueryResponse rsp = solrServer.query( query );
Iterator<SolrDocument> iter = rsp.getResults().iterator();
while (iter.hasNext()) {
SolrDocument resultDoc = iter.next();
String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field
System.out.println(id);
// if (rsp.getHighlighting().get(id) != null) {
// List<String> highlightSnippets = rsp.getHighlighting().get(id).get("content");
// }
}
} catch (SolrServerException e1) {
System.out.println("error here");
e1.printStackTrace();
}
然而,当我尝试使用上述代码建立连接时,出现以下错误。
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://127.0.0.1:8983/solr/
at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:562)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:91)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
at JavaAgent.init(Unknown Source)
at JavaAgent.NotesMain(Unknown Source)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(Unknown Source)
Caused by: java.net.UnknownHostException: :
at java.net.InetAddress.getAllByName0(InetAddress.java:1413)
at java.net.InetAddress.getAllByName(InetAddress.java:1322)
at java.net.InetAddress.getAllByName(InetAddress.java:1245)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:260)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:160)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:448)
似乎无法通过此 URL 连接到实例。
如有任何想法,我们将不胜感激。
核心可用数据截图请见
这是从核心获取数据的代码。
public void getResult() throws SolrServerException, IOException {
final SolrClient client = getSolrClient();
SolrQuery params = new SolrQuery();
params.setQuery("*:*");
try {
QueryResponse queryResponse = client.query(params);
Iterator<SolrDocument> iter = queryResponse.getResults().iterator();
while (iter.hasNext()) {
SolrDocument resultDoc = iter.next();
String id = (String) resultDoc.getFieldValue("id");
Collection<Object> manufacturerS = resultDoc.getFieldValues("manufacturer_s");
String siteName = (String) resultDoc.getFieldValue("site_name");
String title = (String) resultDoc.getFieldValue("title");
System.out.println("Solr Response -> id :: " + id + " manufacturerS :: " + manufacturerS
+ " siteName :: " + siteName + " title :: " + title);
}
} catch (SolrServerException e1) {
e1.printStackTrace();
}
}
private SolrClient getSolrClient() {
final String solrUrl = "http://10.224.143.172:8983/solr/knowledge_combined";
return new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
}
代码的输出是:
我设法找出我的问题所在。
我试图连接到 Solr v8.4.1 版本,但使用的是版本 4 的 SolrJ jar。当我将 SolrJ jar 更新到 v8.4.1 时,它开始工作了。
感谢您的回复。
在我的本地机器上,我安装了 Solr v8.4.1 来做一些测试,并创建了一个核心,使用 json 文件添加了一些数据,并且可以连接到“http://127.0.0.1:8983/solr/ 上的管理客户端”。
我现在正在尝试使用 Lotus Domino 代理通过 Solrj (Java) 连接到实例。
solrServer = new HttpSolrServer("http://127.0.0.1:8983/solr/");
solrServer.setSoTimeout(10000);
solrServer.setConnectionTimeout(10000);
solrServer.setDefaultMaxConnectionsPerHost(100);
solrServer.setMaxTotalConnections(100);
solrServer.setFollowRedirects(false);
solrServer.setAllowCompression(true);
solrServer.setMaxRetries(1);
// solrServer.setParser(new XMLResponseParser());
System.out.println(solrServer.getBaseURL());
SolrQuery query = new SolrQuery();
query.setQuery( "*:*" );
try {
System.out.println("queryyyy");
QueryResponse rsp = solrServer.query( query );
Iterator<SolrDocument> iter = rsp.getResults().iterator();
while (iter.hasNext()) {
SolrDocument resultDoc = iter.next();
String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field
System.out.println(id);
// if (rsp.getHighlighting().get(id) != null) {
// List<String> highlightSnippets = rsp.getHighlighting().get(id).get("content");
// }
}
} catch (SolrServerException e1) {
System.out.println("error here");
e1.printStackTrace();
}
然而,当我尝试使用上述代码建立连接时,出现以下错误。
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://127.0.0.1:8983/solr/
at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:562)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:91)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
at JavaAgent.init(Unknown Source)
at JavaAgent.NotesMain(Unknown Source)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(Unknown Source)
Caused by: java.net.UnknownHostException: :
at java.net.InetAddress.getAllByName0(InetAddress.java:1413)
at java.net.InetAddress.getAllByName(InetAddress.java:1322)
at java.net.InetAddress.getAllByName(InetAddress.java:1245)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:260)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:160)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:448)
似乎无法通过此 URL 连接到实例。
如有任何想法,我们将不胜感激。
核心可用数据截图请见
这是从核心获取数据的代码。
public void getResult() throws SolrServerException, IOException {
final SolrClient client = getSolrClient();
SolrQuery params = new SolrQuery();
params.setQuery("*:*");
try {
QueryResponse queryResponse = client.query(params);
Iterator<SolrDocument> iter = queryResponse.getResults().iterator();
while (iter.hasNext()) {
SolrDocument resultDoc = iter.next();
String id = (String) resultDoc.getFieldValue("id");
Collection<Object> manufacturerS = resultDoc.getFieldValues("manufacturer_s");
String siteName = (String) resultDoc.getFieldValue("site_name");
String title = (String) resultDoc.getFieldValue("title");
System.out.println("Solr Response -> id :: " + id + " manufacturerS :: " + manufacturerS
+ " siteName :: " + siteName + " title :: " + title);
}
} catch (SolrServerException e1) {
e1.printStackTrace();
}
}
private SolrClient getSolrClient() {
final String solrUrl = "http://10.224.143.172:8983/solr/knowledge_combined";
return new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
}
代码的输出是:
我设法找出我的问题所在。
我试图连接到 Solr v8.4.1 版本,但使用的是版本 4 的 SolrJ jar。当我将 SolrJ jar 更新到 v8.4.1 时,它开始工作了。
感谢您的回复。