zoukankan      html  css  js  c++  java
  • solr学习三(测试类,含普通与ExtractingRequestHandler测试)

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

    Java代码  收藏代码
    1. import java.io.IOException;    
    2. import java.util.ArrayList;    
    3. import java.util.Collection;    
    4. import java.util.Date;  
    5. import org.apache.solr.client.solrj.SolrQuery;    
    6. import org.apache.solr.client.solrj.SolrServer;    
    7. import org.apache.solr.client.solrj.SolrServerException;    
    8. import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;    
    9. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;    
    10. import org.apache.solr.client.solrj.request.UpdateRequest;    
    11. import org.apache.solr.client.solrj.response.QueryResponse;    
    12. import org.apache.solr.common.SolrInputDocument;    
    13.     
    14. public class SolrTest {  
    15.     
    16.     public static void main(String[] args) throws IOException,    
    17.             SolrServerException {    
    18.     
    19.         String urlString = " http://localhost:8393/keyPlace";    
    20.         SolrServer server = new CommonsHttpSolrServer(urlString);    
    21.         testAdd(server);  
    22.         testQuery(server);  
    23.           
    24.     }  
    25.     static void testAdd(SolrServer server) throws IOException,    
    26.        SolrServerException {  
    27.         SolrInputDocument doc1 = new SolrInputDocument();    
    28.         doc1.addField("id", 456);    
    29.         doc1.addField("orgId", "33030300310");   
    30.         doc1.addField("name", "张三");   
    31.         doc1.addField("key", "1");  
    32.         doc1.addField("createDate", new Date());  
    33.         SolrInputDocument doc2 = new SolrInputDocument();    
    34.         doc2.addField("id", 123);  
    35.         doc2.addField("orgId", "33030300310");   
    36.         doc2.addField("name", "李四");  
    37.         doc2.addField("key", "2");  
    38.         doc2.addField("createDate", new Date());  
    39.         Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();    
    40.         docs.add(doc1);    
    41.         docs.add( doc2 );    
    42.         server.add(docs);    
    43.         UpdateRequest req = new UpdateRequest();    
    44.         req.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);    
    45.         req.add(docs);    
    46.         req.process(server);    
    47.     }  
    48.       
    49.     static void testQuery(SolrServer server) throws IOException,    
    50.        SolrServerException {  
    51.         SolrQuery query = new SolrQuery();    
    52.         
    53.         query.setQuery("name:张三");    
    54.         query.setHighlight(true).setHighlightSnippets(1);                                                       
    55.     
    56.         QueryResponse ret = server.query(query);    
    57.     
    58.         System.out.println(ret);    
    59.           
    60.     }  
    61.   
    62.    


    如果服务端配置了ExtractingRequestHandler,可使用下面的类进行测试: 

    Java代码  收藏代码
      1. import java.io.File;  
      2. import java.io.IOException;  
      3.   
      4. import org.apache.solr.client.solrj.SolrServer;  
      5. import org.apache.solr.client.solrj.SolrServerException;  
      6.   
      7. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;  
      8. import org.apache.solr.client.solrj.response.QueryResponse;  
      9. import org.apache.solr.client.solrj.SolrQuery;  
      10. import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;  
      11. import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;  
      12. import org.apache.solr.common.params.ModifiableSolrParams;  
      13. import org.apache.solr.handler.extraction.ExtractingParams;  
      14.   
      15. public class ExtractTest {  
      16.   
      17.         public static void main(String[] args) {  
      18.             try {  
      19.               String urlString = "http://localhost:8393/sourcePool/";   
      20.               SolrServer solr = new CommonsHttpSolrServer(urlString);  
      21.               String fileName = "c:/slor1.doc";   
      22.               String solrId = "2";   
      23.               String resoucePoolid = "2";  
      24.                 
      25.               indexFilesSolrCell(fileName, solrId , resoucePoolid , solr);  
      26.               testQuery(solr);  
      27.             } catch (Exception ex) {  
      28.               System.out.println(ex.toString());  
      29.             }  
      30.           }  
      31.             
      32.           /** 
      33.            * Method to index all types of files into Solr.  
      34.            * @throws IOException 
      35.            * @throws SolrServerException 
      36.            */  
      37.           public static void indexFilesSolrCell(String fileName, String solrId , String resoucePoolid , SolrServer solr)   
      38.             throws IOException, SolrServerException {  
      39.               
      40.             ContentStreamUpdateRequest up   
      41.               = new ContentStreamUpdateRequest("/update/extract");  
      42.               
      43.             ModifiableSolrParams p = new ModifiableSolrParams();  
      44.             p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033001");  
      45.             p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033002");  
      46.             p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033003");  
      47.             up.setParams(p);  
      48.               
      49.             up.addFile(new File(fileName));  
      50.             up.setParam(ExtractingParams.LITERALS_PREFIX + "id", solrId);  
      51.             up.setParam(ExtractingParams.LITERALS_PREFIX + "resoucepoolid", resoucePoolid);  
      52.             up.setParam(ExtractingParams.LITERALS_PREFIX + "orgid", "33010033001");  
      53.             up.setParam(ExtractingParams.LITERALS_PREFIX + "name", "33010033001");  
      54.             up.setParam(ExtractingParams.LITERALS_PREFIX + "releaseunit", "33010033001");  
      55.             up.setParam(ExtractingParams.LITERALS_PREFIX + "releasetime", "2011-02-12");  
      56.   
      57.             up.setParam(ExtractingParams.UNKNOWN_FIELD_PREFIX, "attr_");  
      58.               
      59.             //up.setParam("fmap.content", "filestream");  
      60.               
      61.             up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);  
      62.               
      63.             solr.request(up);  
      64.               
      65.               
      66.           }  
      67.             
      68.           static void testQuery(SolrServer solr)  throws IOException, SolrServerException {  
      69.               String fileStream = "filestream:老婆的老公";  
      70.               String field = "orgid:33010033001";  
      71.               QueryResponse rsp = solr.query(new SolrQuery(field));  
      72.                 System.out.println(rsp);   
      73.           }  
  • 相关阅读:
    数字证书原理(转)
    python update from 2.6 to 2.7
    python uwsgi 部署以及优化
    uWSGI配置(转 )
    Python程序的性能分析指南(转)
    mysql 暴力破解 root账号密码
    VMware虚拟机实用经验总结十一条
    小技巧之指定refer
    cookie和session
    web相关
  • 原文地址:https://www.cnblogs.com/cxhfuujust/p/7754653.html
Copyright © 2011-2022 走看看