/**
* 将对象集合添加至索引[测试通过]
* @author pudongping
* @param server
* @param lists 自定义转换机制
*/
public static void addConvertBeans(SolrServer server, List lists){
try {
server.add(EntityConvert.entityList2SolrInputDocument(lists));
server.commit(false, false);
LOG.info("Add convert object list to index finished. ");
} catch (Exception e) {
LOG.error("Add convert object list to index error, " + e.getMessage(), e);
}
}
/**
* 将对象集合添加至索引[测试通过]
* @author pudongping
* @param server
* @param lists 自定义转换机制
*/
public static void addConvertBeans(SolrServer server, List lists){
try {
server.add(EntityConvert.entityList2SolrInputDocument(lists));
server.commit(false, false);
LOG.info("Add convert object list to index finished. ");
} catch (Exception e) {
LOG.error("Add convert object list to index error, " + e.getMessage(), e);
}
}
测试自定义转换对象建立索引.
@Test
public void addConvertBean(){
Article article1 = new Article();
article1.setId(UUID.randomUUID().toString());
List title = new ArrayList();
title.add("我在门后假装你人还没走");
article1.setTitle(title);
article1.setAuthor("周杰伦");
SolrEngineHandler.addConvertBean(server, article1);
}
@Test
public void addConvertBeans(){
List lists = new ArrayList();
Article art1 = new Article();
art1.setId(UUID.randomUUID().toString());
List atlts1 = new ArrayList();
atlts1.add("谁言别后终无悔,寒月清宵绮梦回");
atlts1.add("深知身在情长在,前尘不共彩云飞");
art1.setTitle(atlts1);
art1.setAuthor("柳梦璃");
lists.add(art1);
Article art2 = new Article();
art2.setId(UUID.randomUUID().toString());
List atlts2 = new ArrayList();
atlts2.add("萧萧衷曲无处诉;为伊故,乐所苦。");
atlts2.add("锦色芳华,岂堪人虚度?欲寄相思情万缕,捎不到,君心处。");
art2.setTitle(atlts2);
art2.setAuthor("柳梦璃");
lists.add(art2);
SolrEngineHandler.addConvertBeans(server, lists);
} 方法二: 使用solr提供的注解机制
说明: @Field无参数时,匹配当前字段,也可以自定义,字段必须在schema.xml 中的Filed中存在.
package com.test.model;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.solr.client.solrj.beans.Field;
public class Article implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4017316764889231758L;
@Field("id")
private String id;
@Field("title")
private List title;
@Field
private String author; //@Field无参数时,匹配当前字段
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public List getTitle() {
return title;
}
public void setTitle(List title) {
this.title = title;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
package com.test.model;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.solr.client.solrj.beans.Field;
public class Article implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4017316764889231758L;
@Field("id")
private String id;
@Field("title")
private List title;
@Field
private String author; //@Field无参数时,匹配当前字段
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public List getTitle() {
return title;
}
public void setTitle(List title) {
this.title = title;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
使用solrJ提供的addBean(..)与addBeans(…)相关的方法添加对象
/**
* 将单个对象添加至索引
* @author pudongping
* @param server
* @param object solr机制进行对象转换
*/
public static void addBean(SolrServer server, Object object){
try {
server.addBean(object);
server.commit(false,false);
LOG.info("Add object to index finished.");
} catch (Exception e) {
LOG.error("Add object to index error, " + e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 添加集合对象至索引 [测试通过]
* @author pudongping
* @param
* @param server
* @param lists 使用solr内部转换机制
*/
public static void addBeans(SolrServer server, List lists){
try {
server.addBeans(lists);
server.commit(false, false);
LOG.info("Add object list to index finished. ");
} catch (Exception e) {
LOG.error("Add object list to index finished. ");
}
}
编写junit测试
@Test
public void addBean(){
Article article1 = new Article();
article1.setId(UUID.randomUUID().toString());
List title = new ArrayList();
title.add("地图青岛,末世孤岛.");
article1.setTitle(title);
article1.setAuthor("苏若年");
SolrEngineHandler.addBean(server, article1);
}
@Test
public void addBeans(){
List lists = new ArrayList();
Article art1 = new Article();
art1.setId(UUID.randomUUID().toString());
List atlts1 = new ArrayList();
atlts1.add("谁言别后终无悔,寒月清宵绮梦回");
atlts1.add("深知身在情长在,前尘不共彩云飞");
art1.setTitle(atlts1);
art1.setAuthor("苏若年");
lists.add(art1);
Article art2 = new Article();
art2.setId(UUID.randomUUID().toString());
List atlts2 = new ArrayList();
atlts2.add("萧萧衷曲无处诉;为伊故,乐所苦。");
atlts2.add("锦色芳华,岂堪人虚度?欲寄相思情万缕,捎不到,君心处。");
art2.setTitle(atlts2);
art2.setAuthor("柳梦璃");
lists.add(art2);
SolrEngineHandler.addBeans(server, lists);
}