1.首先在创建好的CORE中添加自己需要的Field.打开
E:\Solr2016-05-03\SolrHome\solr\MySolr\conf\schema.xml
2.用JAVA程序进行添加
2.1 需要引入的Jar包
2.2 程序代码
package com.wiimedia.mryl.connection.solr;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
public class SolrRepository {
public String insertToSolr() throws MalformedURLException {
CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://192.168.0.216:8080/solr/MySolr");
SolrInputDocument document = new SolrInputDocument ();
UUID uuid = UUID.randomUUID();
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
document.addField("id", uuid);
document.addField("Tag", "测试数据标签");
document.addField("ResourceInfo", "微媒");
document.addField("PreviousTag", "测试");
document.addField("DeleteController", "1");
document.addField("ArticleTime", date);
document.addField("Like", "10");
document.addField("Dislike", "2");
document.addField("ArticleTitle", "测试数据标题");
document.addField("CommonCount", "5");
document.addField("ViewCount", "70");
try {
server.add(document);
server.commit();
return "success";
} catch (SolrServerException e) {
e.printStackTrace();
return "failed";
} catch (IOException e) {
e.printStackTrace();
return "failed";
}
}
public static void main(String[] args) throws MalformedURLException {
SolrRepository repository = new SolrRepository();
String result = repository.insertToSolr();
System.out.println(result );
}
}
3.
3.1 可能会出现的错误
出现这个错误的原因是在schema.xml 中添加了多个Field或者有重复的Field。
3.2 可能会出现的错误
出现这个错误的原因是缺少Commons-logging.jar包!