solrj新增、删除、更新

版权声明:本文为博主原创文章,转载请留链接,非常感谢。 https://blog.csdn.net/fay462298322/article/details/52768801

新增

/**
    * 新增
    * @param client
    * @param fieldName
    * @param fieldValue
    * @throws SolrServerException
    * @throws IOException
 */
public static void addFields(CloudSolrClient client, String[] fieldName, Object[] fieldValue) throws SolrServerException, IOException {
    SolrInputDocument doc = new SolrInputDocument();
    for(int i = 0; i < fieldName.length; i++){
        doc.addField(fieldName[i], fieldValue[i]);
    }
    client.add(doc);
    client.commit();
}

删除

public void delete(long id) throws SolrServerException, IOException {
    CloudSolrClient client = SolrUtil.getPictureClient();
    client.deleteById(String.valueOf(id));
    client.commit();
}

更新

/**
  * 更新
  * @param client
  * @param id
  * @param fieldName
  * @param fieldValue
  * @throws SolrServerException
  * @throws IOException
  */
public static void updateFields(CloudSolrClient client, String id, String[] fieldName, Object[] fieldValue) throws SolrServerException, IOException {
//      System.out.println("======================updateField ===================");
//      HashMap<String, Object> oper = new HashMap<String, Object>();
        // 多值更新方法
        // List<String> mulitValues = new ArrayList<String>();
        // mulitValues.add(fieldName);
        // mulitValues.add((String)fieldValue);
//      oper.put("set", fieldValue);
    HashMap<String, Object> oper = null;
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", id);
    for(int i = 0; i < fieldName.length; i++){
        oper = new HashMap<String, Object>();
        oper.put("set", fieldValue[i]);
        doc.addField(fieldName[i], oper);
    }
//      UpdateResponse rsp = client.add(doc);
//      System.out.println("update doc id" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
    client.add(doc);
//      UpdateResponse rspCommit = client.commit();
//      System.out.println("commit doc to index" + " result:" + rspCommit.getStatus() + " Qtime:" + rspCommit.getQTime());
    client.commit();
}
展开阅读全文

没有更多推荐了,返回首页