solr开发,提交索引数据的几种方式

今天抽空学习了一下solr,有新东西学习就是哈皮! 期待能有机会与实战.实例仅为个人理解学习实例.
提交到Solr服务器上的数据必须是 SolrInputDocument 类型.

方案一:利用反射,自定义转换机制

JavaBean对象转换成SolrInputDocument的方式我们可以自定义.如果我们的Model类符合JavaBean的规范,那么我们可以使用反射获取其内部信息.

    
    /**
     * @author dennisit@163.com
     * 
     * 实体类与SolrInputDocument转换 [测试通过]
     * 
     * @param obj
     *                         实体对象
     * @return SolrInputDocument
     *                         SolrInputDocument对象
     */
    public static SolrInputDocument entity2SolrInputDocument(Object obj) {
        if (obj != null) {
            Class<?> cls = obj.getClass();
            Field[] filedArrays = cls.getDeclaredFields();                        //获取类中所有属性
            Method m = null;
            SolrInputDocument sid = new SolrInputDocument();
            for (Field f : filedArrays) {
                //因为如果对象序列化之后,会增加该属性,不用对该属性进行反射
                if(!f.getName().equals("serialVersionUID")){                        
                    try {
                        //跟进属性xx构造对应的getXx()方法
                        String dynamicGetMethod = dynamicMethodName(f.getName(), "get");
                        //调用构造的getXx()方法
                        m = cls.getMethod(dynamicGetMethod);    
                        //属性名,与对应的属性值 get方法获取到的值
                        LOG.info(f.getName() + ":" + m.invoke(obj));
                        sid.addField(""+ f.getName(), m.invoke(obj));
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace(); 
                    }
                }
                
            }
            return sid;
        }
        LOG.warn("Object to convert is null.");
        return null;
    }

此时我们可以使用server中提供的添加solrInputDocument对象的方法进行数据建立索引.

    
    /**
     * 将对象集合添加至索引[测试通过]
     * @author pudongping
     * @param server
     * @param lists    自定义转换机制
     */
    public static <T> void addConvertBeans(SolrServer server, List<T> 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 <T> void addConvertBeans(SolrServer server, List<T> 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<String> title = new ArrayList<String>();
        title.add("我在门后假装你人还没走");
        article1.setTitle(title);
        article1.setAuthor("周杰伦");
        SolrEngineHandler.addConvertBean(server, article1);
    
    }
    
    @Test
    public void addConvertBeans(){
        List<Article> lists = new ArrayList<Article>();
        Article art1 = new Article();
        art1.setId(UUID.randomUUID().toString());
        List<String> atlts1 = new ArrayList<String>();
        atlts1.add("谁言别后终无悔,寒月清宵绮梦回");
        atlts1.add("深知身在情长在,前尘不共彩云飞");
        art1.setTitle(atlts1);
        art1.setAuthor("柳梦璃");
        lists.add(art1);
        
        Article art2 = new Article();
        art2.setId(UUID.randomUUID().toString());
        List<String> atlts2 = new ArrayList<String>();
        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<String> 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<String> getTitle() {
        return title;
    }
    public void setTitle(List<String> 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<String> 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<String> getTitle() {
        return title;
    }
    public void setTitle(List<String> 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 <T>
     * @param server
     * @param lists    使用solr内部转换机制
     */
    public static <T> void addBeans(SolrServer server, List<T> 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<String> title = new ArrayList<String>();
        title.add("地图青岛,末世孤岛.");
        article1.setTitle(title);
        article1.setAuthor("苏若年");
        SolrEngineHandler.addBean(server, article1);
    }
    
    @Test
    public void addBeans(){
        List<Article> lists = new ArrayList<Article>();
        Article art1 = new Article();
        art1.setId(UUID.randomUUID().toString());
        List<String> atlts1 = new ArrayList<String>();
        atlts1.add("谁言别后终无悔,寒月清宵绮梦回");
        atlts1.add("深知身在情长在,前尘不共彩云飞");
        art1.setTitle(atlts1);
        art1.setAuthor("苏若年");
        lists.add(art1);
        
        Article art2 = new Article();
        art2.setId(UUID.randomUUID().toString());
        List<String> atlts2 = new ArrayList<String>();
        atlts2.add("萧萧衷曲无处诉;为伊故,乐所苦。");
        atlts2.add("锦色芳华,岂堪人虚度?欲寄相思情万缕,捎不到,君心处。");
        art2.setTitle(atlts2);
        art2.setAuthor("柳梦璃");
        lists.add(art2);
        SolrEngineHandler.addBeans(server, lists);
    }
    

 转载请注明出处:[http://www.cnblogs.com/dennisit/p/3621717.html]

posted @ 2014-03-24 20:03  苏二  阅读(6207)  评论(2编辑  收藏  举报