`
xinyangwjb
  • 浏览: 79829 次
  • 性别: Icon_minigender_1
  • 来自: 信阳
社区版块
存档分类
最新评论

solr学习三(测试类,含普通与ExtractingRequestHandler测试)

 
阅读更多

solr客户端基本是配置出来的,服务端可以对其进行测试,我使用的是solrj服务端。
如果初学solr,先使用普通的测试类:

	import java.io.IOException;  
	import java.util.ArrayList;  
	import java.util.Collection;  
	import java.util.Date;
	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.CommonsHttpSolrServer;  
	import org.apache.solr.client.solrj.request.AbstractUpdateRequest;  
	import org.apache.solr.client.solrj.request.UpdateRequest;  
	import org.apache.solr.client.solrj.response.QueryResponse;  
	import org.apache.solr.common.SolrInputDocument;  
	  
	public class SolrTest {
	  
	    public static void main(String[] args) throws IOException,  
	            SolrServerException {  
	  
	        String urlString = " http://localhost:8393/keyPlace";  
	        SolrServer server = new CommonsHttpSolrServer(urlString);  
	        testAdd(server);
	        testQuery(server);
	        
	    }
	    static void testAdd(SolrServer server) throws IOException,  
        SolrServerException {
	    	SolrInputDocument doc1 = new SolrInputDocument();  
	        doc1.addField("id", 456);  
	        doc1.addField("orgId", "33030300310"); 
	        doc1.addField("name", "张三"); 
	        doc1.addField("key", "1");
	        doc1.addField("createDate", new Date());
	        SolrInputDocument doc2 = new SolrInputDocument();  
	        doc2.addField("id", 123);
	        doc2.addField("orgId", "33030300310"); 
	        doc2.addField("name", "李四");
	        doc2.addField("key", "2");
	        doc2.addField("createDate", new Date());
	        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();  
	        docs.add(doc1);  
	        docs.add( doc2 );  
	        server.add(docs);  
	        UpdateRequest req = new UpdateRequest();  
	        req.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);  
	        req.add(docs);  
	        req.process(server);  
	    }
	    
	    static void testQuery(SolrServer server) throws IOException,  
        SolrServerException {
	    	SolrQuery query = new SolrQuery();  
	  	  
	        query.setQuery("name:张三");  
	        query.setHighlight(true).setHighlightSnippets(1);                                                     
	  
	        QueryResponse ret = server.query(query);  
	  
	        System.out.println(ret);  
	    	
	    }

} 

如果服务端配置了ExtractingRequestHandler,可使用下面的类进行测试:
import java.io.File;
import java.io.IOException;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;

import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.handler.extraction.ExtractingParams;

public class ExtractTest {

		public static void main(String[] args) {
		    try {
		      String urlString = "http://localhost:8393/sourcePool/"; 
			  SolrServer solr = new CommonsHttpSolrServer(urlString);
		      String fileName = "c:/slor1.doc"; 
		      String solrId = "2"; 
		      String resoucePoolid = "2";
		      
		      indexFilesSolrCell(fileName, solrId , resoucePoolid , solr);
		      testQuery(solr);
		    } catch (Exception ex) {
		      System.out.println(ex.toString());
		    }
		  }
		  
		  /**
		   * Method to index all types of files into Solr. 
		   * @throws IOException
		   * @throws SolrServerException
		   */
		  public static void indexFilesSolrCell(String fileName, String solrId , String resoucePoolid , SolrServer solr) 
		    throws IOException, SolrServerException {
		    
		    ContentStreamUpdateRequest up 
		      = new ContentStreamUpdateRequest("/update/extract");
		    
		    ModifiableSolrParams p = new ModifiableSolrParams();
		    p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033001");
		    p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033002");
		    p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033003");
		    up.setParams(p);
		    
		    up.addFile(new File(fileName));
		    up.setParam(ExtractingParams.LITERALS_PREFIX + "id", solrId);
		    up.setParam(ExtractingParams.LITERALS_PREFIX + "resoucepoolid", resoucePoolid);
		    up.setParam(ExtractingParams.LITERALS_PREFIX + "orgid", "33010033001");
		    up.setParam(ExtractingParams.LITERALS_PREFIX + "name", "33010033001");
		    up.setParam(ExtractingParams.LITERALS_PREFIX + "releaseunit", "33010033001");
		    up.setParam(ExtractingParams.LITERALS_PREFIX + "releasetime", "2011-02-12");

		    up.setParam(ExtractingParams.UNKNOWN_FIELD_PREFIX, "attr_");
		    
		    //up.setParam("fmap.content", "filestream");
		    
		    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
		    
		    solr.request(up);
		    
		    
		  }
		  
		  static void testQuery(SolrServer solr)  throws IOException, SolrServerException {
			  String fileStream = "filestream:老婆的老公";
			  String field = "orgid:33010033001";
			  QueryResponse rsp = solr.query(new SolrQuery(field));
			    System.out.println(rsp); 
		  }
}

测试类如果出错,基本都是jar包引入的问题,jar如果引入可参见我的另一篇博客:http://xinyangwjb.iteye.com/blog/1405713
分享到:
评论
2 楼 xinyangwjb 2012-05-05  
solr官网例子默认编码是UTF-8,你的项目是不是UTF-8,如果是,就全文搜索下solr的xml文件看有没有encode或者encoding字段,看看是什么编码格式,好久没弄了,忘了哪里配置编码格式了。
1 楼 wjx 2012-05-05  
up.setParam设置的内容如果有中文的话会乱码,不知lz是否解决了此问题?

相关推荐

Global site tag (gtag.js) - Google Analytics