使用solrj访问solr4.10.3增删改失败

春风AA 发布于 2017/04/30 22:10
阅读 791
收藏 0

java 运行环境

tomcat7.0

后台可以添加并查询

 

使用solrj 查询成功

@Test
	public void searchDocumet() throws Exception {
		//创建一个SolrServer对象
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.88:8080/solr/collection1");
	//	SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
		//创建一个SolrQuery对象
		SolrQuery query = new SolrQuery();
		//设置查询条件、过滤条件、分页条件、排序条件、高亮
		query.set("q", "*:*");
		//query.setQuery("手机");
		//分页条件
		query.setStart(0);
		query.setRows(10);
		//设置默认搜索域
		query.set("df", "item_keywords");
		//设置高亮
		query.setHighlight(true);
		//高亮显示的域
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<div>");
		query.setHighlightSimplePost("</div>");
		//执行查询,得到一个Response对象
		QueryResponse response = solrServer.query(query);
		//取查询结果
		SolrDocumentList solrDocumentList = response.getResults();
		//取查询结果总记录数
		System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
		for (SolrDocument solrDocument : solrDocumentList) {
			System.out.println(solrDocument.get("id"));
			//取高亮显示
			Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			String itemTitle = "";
			if (list != null && list.size() >0) {
				itemTitle = list.get(0);
			} else {
				itemTitle = (String) solrDocument.get("item_title");
			}
			System.out.println(itemTitle);
			System.out.println(solrDocument.get("item_sell_point"));
			System.out.println(solrDocument.get("item_price"));
			System.out.println(solrDocument.get("item_image"));
			System.out.println(solrDocument.get("item_category_name"));
			System.out.println("=============================================");
		}
		
	}

控制台也有输出

 

测试添加方法抛异常

@Test
	public void testAddDocument() throws Exception {
		//创建一个SolrServer对象。创建一个HttpSolrServer对象
		//需要指定solr服务的url
		SolrServer solrServer = new HttpSolrServer("http://192.168.0.113:8080/solr/collection1");
	//	SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
		//创建一个文档对象SolrInputDocument  
		SolrInputDocument document = new SolrInputDocument();
		//向文档中添加域,必须有id域,域的名称必须在schema.xml中定义
		document.addField("id", "1823483");
		document.addField("item_title", "LALALA");
		document.addField("item_price", 1645600);
		//把文档对象写入索引库
		solrServer.add(document);
		//提交
		solrServer.commit();
	}
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://192.168.0.113:8080/solr/collection1
	at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:566)
	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.AbstractUpdateRequest.process(AbstractUpdateRequest.java:124)
	at org.apache.solr.client.solrj.SolrServer.commit(SolrServer.java:168)
	at org.apache.solr.client.solrj.SolrServer.commit(SolrServer.java:146)
	at com.taotao.solrj.TestSolrJ.testAddDocument(TestSolrJ.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.http.NoHttpResponseException: 192.168.0.113:8080 failed to respond
	at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
	at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
	at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
	at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
	at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
	at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:197)
	at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
	at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
	at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
	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)
	... 29 more

没有添加成功

控制台日志

DEBUG [main] - Creating new http client, config:maxConnections=128&maxConnectionsPerHost=32&followRedirects=false
DEBUG [main] - Connection request: [route: {}->http://192.168.0.113:8080][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 128]
DEBUG [main] - Connection leased: [id: 0][route: {}->http://192.168.0.113:8080][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 128]
DEBUG [main] - Connecting to 192.168.0.113:8080
DEBUG [main] - CookieSpec selected: best-match
DEBUG [main] - Auth cache not set in the context
DEBUG [main] - Target auth state: UNCHALLENGED
DEBUG [main] - Proxy auth state: UNCHALLENGED
DEBUG [main] - Attempt 1 to execute request
DEBUG [main] - Sending request: POST /solr/collection1/update?wt=javabin&version=2 HTTP/1.1
DEBUG [main] -  >> "POST /solr/collection1/update?wt=javabin&version=2 HTTP/1.1[\r][\n]"
DEBUG [main] -  >> "User-Agent: Solr[org.apache.solr.client.solrj.impl.HttpSolrServer] 1.0[\r][\n]"
DEBUG [main] -  >> "Transfer-Encoding: chunked[\r][\n]"
DEBUG [main] -  >> "Content-Type: application/xml; charset=UTF-8[\r][\n]"
DEBUG [main] -  >> "Host: 192.168.0.113:8080[\r][\n]"
DEBUG [main] -  >> "Connection: Keep-Alive[\r][\n]"
DEBUG [main] -  >> "[\r][\n]"
DEBUG [main] - >> POST /solr/collection1/update?wt=javabin&version=2 HTTP/1.1
DEBUG [main] - >> User-Agent: Solr[org.apache.solr.client.solrj.impl.HttpSolrServer] 1.0
DEBUG [main] - >> Transfer-Encoding: chunked
DEBUG [main] - >> Content-Type: application/xml; charset=UTF-8
DEBUG [main] - >> Host: 192.168.0.113:8080
DEBUG [main] - >> Connection: Keep-Alive
DEBUG [main] -  >> "91[\r][\n]"
DEBUG [main] -  >> "<add><doc boost="1.0"><field name="id">1823483</field><field name="item_title">LALALA</field><field name="item_price">1645600</field></doc></add>"
DEBUG [main] -  >> "[\r][\n]"
DEBUG [main] -  >> "0[\r][\n]"
DEBUG [main] -  >> "[\r][\n]"
DEBUG [main] -  << "HTTP/1.1 200 OK[\r][\n]"
DEBUG [main] -  << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG [main] -  << "Content-Type: application/octet-stream[\r][\n]"
DEBUG [main] -  << "Date: Sun, 30 Apr 2017 14:09:42 GMT[\r][\n]"
DEBUG [main] -  << "Content-Length: 40[\r][\n]"
DEBUG [main] -  << "[\r][\n]"
DEBUG [main] - Receiving response: HTTP/1.1 200 OK
DEBUG [main] - << HTTP/1.1 200 OK
DEBUG [main] - << Server: Apache-Coyote/1.1
DEBUG [main] - << Content-Type: application/octet-stream
DEBUG [main] - << Date: Sun, 30 Apr 2017 14:09:42 GMT
DEBUG [main] - << Content-Length: 40
DEBUG [main] - Connection can be kept alive indefinitely
DEBUG [main] -  << "[0x2][0xa1][0xe0].responseHeader[0xa2][0xe0]&status[0x6][0x0][0x0][0x0][0x0][0xe0]%QTimeB"
DEBUG [main] - Connection [id: 0][route: {}->http://192.168.0.113:8080] can be kept alive indefinitely
DEBUG [main] - Connection released: [id: 0][route: {}->http://192.168.0.113:8080][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 128]
DEBUG [main] - Connection request: [route: {}->http://192.168.0.113:8080][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 128]
DEBUG [main] - Connection leased: [id: 0][route: {}->http://192.168.0.113:8080][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 128]
DEBUG [main] - Stale connection check
DEBUG [main] - CookieSpec selected: best-match
DEBUG [main] - Auth cache not set in the context
DEBUG [main] - Target auth state: UNCHALLENGED
DEBUG [main] - Proxy auth state: UNCHALLENGED
DEBUG [main] - Attempt 1 to execute request
DEBUG [main] - Sending request: POST /solr/collection1/update HTTP/1.1
DEBUG [main] -  >> "POST /solr/collection1/update HTTP/1.1[\r][\n]"
DEBUG [main] -  >> "Content-Type: application/x-www-form-urlencoded; charset=UTF-8[\r][\n]"
DEBUG [main] -  >> "User-Agent: Solr[org.apache.solr.client.solrj.impl.HttpSolrServer] 1.0[\r][\n]"
DEBUG [main] -  >> "Content-Length: 67[\r][\n]"
DEBUG [main] -  >> "Host: 192.168.0.113:8080[\r][\n]"
DEBUG [main] -  >> "Connection: Keep-Alive[\r][\n]"
DEBUG [main] -  >> "[\r][\n]"
DEBUG [main] - >> POST /solr/collection1/update HTTP/1.1
DEBUG [main] - >> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DEBUG [main] - >> User-Agent: Solr[org.apache.solr.client.solrj.impl.HttpSolrServer] 1.0
DEBUG [main] - >> Content-Length: 67
DEBUG [main] - >> Host: 192.168.0.113:8080
DEBUG [main] - >> Connection: Keep-Alive
DEBUG [main] -  >> "commit=true&softCommit=false&waitSearcher=true&wt=javabin&version=2"
DEBUG [main] - Connection 0.0.0.0:57317<->192.168.0.113:8080 closed
DEBUG [main] - Closing the connection.
DEBUG [main] - Connection 0.0.0.0:57317<->192.168.0.113:8080 closed
DEBUG [main] - Connection 0.0.0.0:57317<->192.168.0.113:8080 shut down
DEBUG [main] - Connection 0.0.0.0:57317<->192.168.0.113:8080 closed
DEBUG [main] - Connection released: [id: 0][route: {}->http://192.168.0.113:8080][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 128]

 

 

返回顶部
顶部