Solrj API读取core 索引库数据
private static String zkHost = "ip:2181,ip:2181,ip:2181";
private static CloudSolrServer solrServer = new CloudSolrServer(zkHost);
public static Map<String, Object> solrCloudR() throws Exception { Map<String, Object> mapResult = new HashMap<String, Object>(); String zkHost = "ip:2181"; CloudSolrServer solrServer = new CloudSolrServer(zkHost); solrServer.setDefaultCollection("collection1"); SolrQuery solrQuery = new SolrQuery(); solrQuery.set("q", "*:*"); QueryResponse response = solrServer.query(solrQuery); SolrDocumentList results = response.getResults(); response.getResults().getNumFound(); mapResult.put("results:", results); mapResult.put("resultNums", response.getResults().getNumFound()); for (SolrDocument solrDocument: results){ Collection<String> fieldNames = solrDocument.getFieldNames();
for(String field:fieldNames){
System.out.println(field + "=" +solrDocument.get(field));
} } System.out.println(results.size()); return mapResult; }
public static Map<String, Object> solrSearch(String skey) throws SolrServerException{ Map<String, Object> mapResult = new HashMap<String, Object>(); solrServer.setDefaultCollection("collection1"); ModifiableSolrParams params = new ModifiableSolrParams(); params.set("q", skey); //params.set("defType", "edismax"); params.set("start", "0"); mapResult.put("results:", mapResult); QueryResponse response = solrServer.query(params); SolrDocumentList results = response.getResults(); for (int i = 0; i < results.size(); ++i) { System.out.println(results.get(i)); } return mapResult; }
输出结果:
SolrDocument{createtime=Fri Aug 14 09:16:35 PDT 2015, avgfeel=0, content=[台湾], id=10, articlenum=0, updatetime=Wed Aug 19 05:45:54 PDT 2015, sentencenum=11, state=1, _version_=1509912377408094208} SolrDocument{uid=2, feel=100, updatetime=Tue Aug 18 17:38:36 PDT 2015, state=1, createtime=Tue Aug 18 17:38:41 PDT 2015, id=7, content=[听说台湾明星更喜欢下雨天],
用URL的方式删除索引库数据:
http://192.168.0.157:8983/solr/update/?stream.body=%3Cdelete%3E%3Cquery%3E*:*%3C/query%3E%3C/delete%3E&stream.contentType=text/xml;charset=utf-8&commit=true