solr中如果想更改其中的某些字段,可以采用solr的原子更新。下面贴上一段简洁的代码。
如果solr中某些字段是集合类型,在schema.xml配置文件中需要将相应字段添加multiValued="true"属性
public static List<SolrInputDocument> atmoUpdate(List<Map<String, Object>> solrMapList)
throws SolrServerException, IOException {
List<SolrInputDocument> inputDocList = new ArrayList<SolrInputDocument>();
for (Map<String, Object> map : solrMapList) {
SolrInputDocument inputDoc = new SolrInputDocument();
for (String field : map.keySet()) {
Map<String, Object> fieldMap = new HashMap<String, Object>();
if (field.equals("id")) {
inputDoc.setField("id", map.get(field));
} else {//map.get(field)获取更新的值
fieldMap.put("set", map.get(field));//set更新一个字段
inputDoc.setField(field, fieldMap);//当发现设置的字段值是Map类型时就认为是原子更新
}
}
inputDocList.add(inputDoc);
}
return inputDocList;
}