package com.gs.index.solr;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class TestSolr {

	@Test
	public void test() throws SolrServerException, IOException {
		String url = "http://localhost:8888/solr";
		SolrServer server = new HttpSolrServer(url);
		SolrInputDocument doc1 = new SolrInputDocument();
		doc1.addField("id", "1");
		doc1.addField("title", "习近平亲民广获赞誉 美媒称领导中共魅力攻势");
		doc1.addField("cat", "最近一段时间，国家主席习近平排队吃包子、办公室发表新年贺词引起了广泛关注。人们发现，国家最高领导人吃的是寻常百姓饭，办公室里摆的也是家人照片。");
		server.add(doc1);
		server.commit();
	}
	
	@Test
	public void testSearch() throws SolrServerException{
		String url = "http://localhost:8888/solr";
		  SolrServer server = new HttpSolrServer(url);
		  SolrQuery query = new SolrQuery("国家");
		  query.setStart(0);
		  query.setRows(100);
		   QueryResponse response = server.query(query);
		   SolrDocumentList docs = response.getResults();
		   System.out.println("文档个数：" + docs.getNumFound());
		   System.out.println("查询时间：" + response.getQTime());
		   for (SolrDocument doc : docs) {
		    System.out.println("id: " + doc.getFieldValue("id"));
		    System.out.println("title: " + doc.getFieldValue("title"));
		    System.out.println("cat: "+doc.getFieldValue("cat"));
		   }
	}

}

@@ -51,16 +51,66 @@
 eption;%0D
+%0Aimport java.util.HashSet;%0D%0Aimport java.util.Set;%0D
 %0A%0D%0Aimpor
@@ -548,16 +548,130 @@
 est;%0D%0A%0D%0A
+import com.gs.indexer.solr.SolrIndex;%0D%0Aimport com.gs.indexer.solr.SolrSearcher;%0D%0Aimport com.gs.model.PagePOJO;%0D%0A%0D%0A
 public c
@@ -1844,14 +1844,411 @@
   %7D%0D%0A%09%7D%0D
+%0A%09%0D%0A%09@Test%0D%0A%09public void testIndex() throws SolrServerException, IOException%7B%0D%0A%09%09Set%3CPagePOJO%3E set = new HashSet%3CPagePOJO%3E();%0D%0A%09%09set.add(new PagePOJO(%22http://news.qq.com%22,1,%22%E5%93%88%E5%93%88%E5%93%88%22,%22hhh%22));%0D%0A%09%09SolrIndex.index(set, %22http://localhost:8888/solr%22);%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void testSearch2() throws SolrServerException%7B%0D%0A%09%09System.out.println(SolrSearcher.search(%22%E5%93%88%22, %22http://localhost:8888/solr%22));%0D%0A%09%7D%0D
 %0A%0D%0A%7D%0D%0A

添加了几个Solr的索引和搜索的测试用例.