一、异常描述
通过solrj的(solr-core-4.4.0.jar、solr-solrj-4.4.0.jar)org.apache.solr.client.solrj.impl.HttpSolrServer连接solr服务器,并通过其org.apache.solr.common.SolrInputDocument提交索引更新,测试代码前后分别报出“org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at http://127.0.0.1:8080/solr returned non ok status:404, message:Not Found”、“org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at http://127.0.0.1:8080/solr/#/subject returned non ok status:405, message:Method Not Allowed”异常,具体日志如下
Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at http://127.0.0.1:8080/solr returned non ok status:404, message:Not Found@b@ at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:385)@b@ at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:180)@b@ at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:117)@b@ at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:116)@b@ at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:102)@b@ at com.xwood.pms.core.search.solrj.SolrServerIndexSubmitTest.main(SolrServerIndexSubmitTest.java:31) @b@ ...@b@Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at http://127.0.0.1:8080/solr/#/subject returned non ok status:405, message:Method Not Allowed@b@ at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:385)@b@ at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:180)@b@ at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:117)@b@ at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:116)@b@ at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:102)@b@ at com.xwood.pms.core.search.solrj.SolrServerIndexSubmitTest.main(SolrServerIndexSubmitTest.java:31)
二、解决办法
1. 404 问题一,地址写错了,应该配置具体core的地址“http://127.0.0.1:8080/solr/subject”(请参考solr.xml配置),并不是部署solr服务地址http://127.0.0.1:8080/solr
<?xml version="1.0" encoding="UTF-8" ?> @b@<solr persistent="false"> @b@ <cores adminPath="/admin/cores" host="${host:}" hostContext="${hostContext:solr}">@b@ <core name="subject" instanceDir="subject" />@b@ </cores>@b@</solr>
2.405的问题,也是地址写错了,应该是“http://127.0.0.1:8080/solr/subject”,不应该是“http://127.0.0.1:8080/solr/#/subject”(多了个#)
三、代码示例
public class SolrServerIndexSubmitTest {@b@@b@ @b@ public static void main(String[] args) throws Exception{@b@ @b@ String url = "http://127.0.0.1:8080/solr/subject"; @b@ HttpSolrServer server = new HttpSolrServer(url); @b@ server.setMaxRetries(1); // defaults to 0. > 1 not recommended. @b@ server.setConnectionTimeout(5000); // 5 seconds to establish TCP @b@ @b@ server.setParser(new XMLResponseParser()); @b@ @b@ server.setSoTimeout(1000); // socket read timeout @b@ server.setDefaultMaxConnectionsPerHost(100); @b@ server.setMaxTotalConnections(100); @b@ server.setFollowRedirects(false); // defaults to false @b@ server.setAllowCompression(true); @b@ @b@ SolrInputDocument doc1 = new SolrInputDocument(); @b@ doc1.addField( "id", "410279", 1.0f ); @b@ doc1.addField( "img_icon", "", 1.0f ); @b@ doc1.addField( "folder_id", "5874", 1.0f ); @b@ doc1.addField( "ori_url", "", 1.0f ); @b@ doc1.addField( "title", "222通过修改AJAXasfjksalfj的点点滴滴多connectionTimeout、worker.master.socket_timeout及Timeout 的连接超时限制", 1.0f ); @b@ doc1.addField( "keywords", "Ext.Ajax,$ajax,apache,jk,tomcat,Timeout,socket_timeout,connectionTimeout,jquery,超时", 1.0f ); @b@ doc1.addField( "cdn_url", "", 1.0f ); @b@ doc1.addField( "zhaiyao", "最近通过前端Ext.Ajax.request请求等待大量数据同步到Mysql..........x.request默认配置如下", 1.0f ); @b@ doc1.addField( "create_time", "2017-09-10 00:41:44", 1.0f ); @b@ doc1.addField( "air_url", "", 1.0f ); @b@ doc1.addField( "url", "/_root/5870/5874/t_c268099.html", 1.0f ); @b@ @b@ server.add(doc1); @b@ server.commit(); @b@@b@ }@b@@b@}
运行后,可以在solr中查找到上面的索引数据,如下图所示