根据所需字段在数据库中查询出所需的List<>之后,就要涉及到如何向索引库中根据schema.xml中的域创建出自己的索引库
遍历这个List,然后对每一个对象进行添加
package com.lan.solrJ;
所需jar包,位于F:\test_dir\solr\solr-4.10.3\dist\solrj-lib 和F:\test_dir\solr\solr-4.10.3\dist下
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
public class SolrJTest {
public void addDocument() throws SolrServerException, IOException {
//创建一个SolrServer连接
SolrServer server = new HttpSolrServer("127.0.0.1:8080/solr/connection1");
//创建一个文档对象,solr的执行过程为原始文档→文档对象→解析文档→创建索引→索引库
//索引必须有一个文档对象,用来构建文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档对象中添加域,域相当于pojo类的属性,但必须包含一个id域,
//集合中的对象域已经在schema.xml文件中定义好
//通过addField方法添加域值
document.addField("id", "doc01");
document.addField("", "");
//将文档写入索引库,有异常,需要处理
server.add(document);
//提交
server.commit();
//关闭的方式,完成之后执行
//server.shutdown();
}