看你的是更新單個數據還是多個數據了,下面分別給出兩個的解決方案.
單個數據:
public static void updateSingleData(String id,String fieldName,Object fieldValue) throws SolrServerException,IOException{ HashMap<String, Object> oper = new HashMap<String, Object>(); oper.put("set", fieldValue); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", id); doc.addField(fieldName, oper); UpdateResponse rsp = solr.add(doc); System.out.println("update doc id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime()); UpdateResponse rspCommit = solr.commit(); System.out.println("commit doc to index" + " result:" + rspCommit.getStatus() + " Qtime:" + rspCommit.getQTime()); }
多個數據:
public static void updateMultiData(String id,Map<String, String> maps ) throws SolrServerException,IOException{ Set<String> keys = maps.keySet(); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", id); for(String key : keys) { HashMap<String, Object> oper = new HashMap<String, Object>(); oper.put("set", maps.get(key)); doc.addField(key, oper); } UpdateResponse rsp = solr.add(doc); System.out.println("update doc id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime()); UpdateResponse rspCommit = solr.commit(); System.out.println("commit doc to index" + " result:" + rspCommit.getStatus() + " Qtime:" + rspCommit.getQTime()); }
看你的是更新單個數據還是多個數據了,下面分別給出兩個的解決方案.
單個數據:
public static void updateSingleData(String id,String fieldName,Object fieldValue) throws SolrServerException,IOException{ HashMap<String, Object> oper = new HashMap<String, Object>(); oper.put("set", fieldValue); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", id); doc.addField(fieldName, oper); UpdateResponse rsp = solr.add(doc); System.out.println("update doc id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime()); UpdateResponse rspCommit = solr.commit(); System.out.println("commit doc to index" + " result:" + rspCommit.getStatus() + " Qtime:" + rspCommit.getQTime()); }
多個數據:
public static void updateMultiData(String id,Map<String, String> maps ) throws SolrServerException,IOException{ Set<String> keys = maps.keySet(); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", id); for(String key : keys) { HashMap<String, Object> oper = new HashMap<String, Object>(); oper.put("set", maps.get(key)); doc.addField(key, oper); } UpdateResponse rsp = solr.add(doc); System.out.println("update doc id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime()); UpdateResponse rspCommit = solr.commit(); System.out.println("commit doc to index" + " result:" + rspCommit.getStatus() + " Qtime:" + rspCommit.getQTime()); }