Solr添加文档

Solr添加文档

注意,添加的文档必定有一个域fieds是ID,这个是唯一的,相当于主键,当主键存在的时候直接覆盖原来的

1、直接添加

SolrInputDocument doc = new SolrInputDocument();
//   doc.addField("id", "1");
//   doc.addField("msg_title", "我爱中国");
//   doc.addField("msg_comtent", "我的祖国是中国,我爱他!");
//   doc.addField("msg_url", "www.baidu.com");
//   server.add(doc);
   doc.addField("id", "2");
   doc.addField("msg_title", "my love china");
   doc.addField("msg_comtent", "i love it");
   //doc.addField("text", "field for the QueryParser to use when an explicit fieldname is absent DEPRECATED: specify 'df' in your request handler instead.");
   doc.addField("msg_url", "www.baidu.com");
   server.add(doc);
   server.commit();

 

2、基于javabean添加
Message类

package org.itat.lucene.solr.test;

import org.apache.solr.client.solrj.beans.Field;

public class Message {
    private String id;
    private String title;
    private String content;
    
    public Message() {
        super();
    }
    public Message(String id, String title, String content) {
        super();
        this.id = id;
        this.title = title;
        this.content = content;
    }
    public String getId() {
        return id;
    }
    @Field
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    @Field("msg_title")
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    @Field("msg_comtent")
    public void setContent(String content) {
        this.content = content;
    }


添加方法:


List<Message> msgs = new ArrayList<Message>();
msgs.add(new Message("4", "basdf","sadfasdfsaf"));
server.addBeans(msgs);
server.commit();

 

posted @ 2012-11-13 22:05  浪浪仔  阅读(1844)  评论(0编辑  收藏  举报