tomcat+solr搭建solrj开发环境

 String url = "http://xapp10-017.i.ajkdns.com:8081/solr/"; //替换成服务器地址
          SolrServer server = new HttpSolrServer(url);
          List<CrmPhoneVistorStatistics> brokerList =new ArrayList<CrmPhoneVistorStatistics>();
          Integer      brokerId=0;
          Integer      size=10;
          for (;;) {
              HashMap<String, Object> map= new HashMap<String, Object>();
              map.put("size", size);
              map.put("brokerId", brokerId);
              brokerList=crmPhoneVistorStatisticsMapper.selectList(map);
              if (brokerList.size()>0) {
                   List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
                   List<String> ids = new ArrayList<String>();
                    for (CrmPhoneVistorStatistics crmPhoneVistorStatistics : brokerList) {
                        brokerId=crmPhoneVistorStatistics.getBrokerId();
                        SolrInputDocument doc = new SolrInputDocument(); 
                          doc.addField("id", crmPhoneVistorStatistics.getBrokerId());
                            ids.add(""+crmPhoneVistorStatistics.getBrokerId());
                          doc.addField("BrokerName", crmPhoneVistorStatistics.getBrokerName());
                          doc.addField("UserPhone", crmPhoneVistorStatistics.getUserPhone());
                          doc.addField("brokerLife", crmPhoneVistorStatistics.getBrokerLife());
                          doc.addField("BrokerMobile", crmPhoneVistorStatistics.getBrokerMobile());
                          
                          doc.addField("AreaCode", crmPhoneVistorStatistics.getAreaCode());
                          doc.addField("StoreName", crmPhoneVistorStatistics.getStoreName());
                          doc.addField("totalHouse", crmPhoneVistorStatistics.getTotalHouse());
                          doc.addField("remainMoney", crmPhoneVistorStatistics.getRemainMoney());
                          
                          doc.addField("cclastContactTime", crmPhoneVistorStatistics.getCclastContactTime());
                          doc.addField("telvisitCount", crmPhoneVistorStatistics.getTelvisitCount());
                          doc.addField("storeCount", crmPhoneVistorStatistics.getStoreCount());
                          doc.addField("visitime", crmPhoneVistorStatistics.getVisitime());
                          doc.addField("ccPhone", crmPhoneVistorStatistics.getCcPhone());
                          
                          docs.add(doc);
                            
                    }
                    try {
                        server.deleteById(ids);
                        server.commit(true, true);

                        server.add(docs);
                        server.commit();
                    } catch (SolrServerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    docs=null;
                    ids=null;
              }else {
                break;
              }
          }
           
         

这个是添加索引文件

下面的是查询方法

 public  void query() {
		  ConfigUtil config = new  ConfigUtil();
		  String url = config.getConfig("JOB_SOLR_URL");//替换服务器地址
		  SolrServer server = new HttpSolrServer(url); 
		  SolrQuery query = new SolrQuery("*:*");//做主查询
		  query.addFilterQuery("id:[* TO 100024]");//条件过滤
		//  query.add("BrokerMobile", "15918005844");//15918005844
				   QueryResponse response;
				try {
					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("name:------------------ " + doc.getFieldValue("BrokerName")); 
					   } 
				} catch (SolrServerException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 

	             
	    
	  }

 然后是在solr索引文件中例如我的目录为D:\tomcat\solr\collection1\conf中的schema.xml进行上面对应字段的添加

下面是部分schema.xml内容

 <fields>
  
        
   <field name="id" type="string" indexed="true" stored="true" required="true"  multiValued="false" /> 
   <field name="BrokerName" type="string" indexed="true" stored="true" multiValued="false"  /> 
   <field name="brokerLife" type="string" indexed="true" stored="true" multiValued="false" /> 
   <field name="UserPhone" type="string" indexed="true" stored="true"  multiValued="false"  /> 
   

   <field name="BrokerMobile" type="string" indexed="true" stored="true"   multiValued="false" />
   <field name="AreaCode" type="string" indexed="true" stored="true"   multiValued="false" />
   <field name="StoreName" type="string" indexed="true" stored="true"   multiValued="false" />
   <field name="totalHouse" type="int" indexed="true" stored="true"   multiValued="false" />
   <field name="remainMoney" type="double" indexed="true" stored="true"   multiValued="false" />
   <field name="cclastContactTime" type="date" indexed="true" stored="true"  multiValued="false"  /> 
         
    <field name="telvisitCount" type="int" indexed="true" stored="true"   multiValued="false" />
    <field name="storeCount" type="int" indexed="true" stored="true"  multiValued="false"  />
    <field name="visitime" type="string" indexed="true" stored="true"   multiValued="false" />
    <field name="ccPhone" type="int" indexed="true" stored="true"  multiValued="false"  />
    <field name="_version_" type="long" indexed="true" stored="true"/>     
    <field name="simple" type="textSimple" indexed="true" stored="true" multiValued="true"/> 
    <field name="complex" type="textComplex" indexed="true" stored="true" multiValued="true"/> 


    <field name="text" type="text_general" indexed="true" stored="true" multiValued="true"/>              
   </fields> 
  <uniqueKey>id</uniqueKey>

理论上把上面部分代码替换成你自己的schema.xml中的内容

<?xml version="1.0" encoding="UTF-8"?>

<Context docBase="D:\tomcat\webapps\solr.war" debug="0" crossContext="true" >
   <Environment name="solr/home" type="java.lang.String" value="D:\tomcat\solr" override="true" />
</Context>

这个是放在D:\tomcat\conf\Catalina\localhost下添加solr.xml

 

然后把下载下来的solr安装包中example中的solr.war中放到tomcat的webapps中,然后修改解压出来的solr中的web-inf下web.xml文件中的索引存放位置

 <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>D:\tomcat\solr</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>

以上配置完成基本就能够运行起来了

 

posted @ 2013-07-19 12:13  美铝  阅读(501)  评论(1)    收藏  举报