|
String url = "http://xapp10-017.i.ajkdns.com:8081/solr/"; //替换成服务器地址
SolrServer server = new HttpSolrServer(url);
List brokerList =new ArrayList();
Integer brokerId=0;
Integer size=10;
for (;;) {
HashMap map= new HashMap();
map.put("size", size);
map.put("brokerId", brokerId);
brokerList=crmPhoneVistorStatisticsMapper.selectList(map);
if (brokerList.size()>0) {
List docs = new ArrayList();
List ids = new ArrayList();
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内容
id
理论上把上面部分代码替换成你自己的schema.xml中的内容
这个是放在D:\tomcat\conf\Catalina\localhost下添加solr.xml
然后把下载下来的solr安装包中example中的solr.war中放到tomcat的webapps中,然后修改解压出来的solr中的web-inf下web.xml文件中的索引存放位置
solr/home
D:\tomcat\solr
java.lang.String
以上配置完成基本就能够运行起来了
|
|
|