SolrJ更新索引数据

SolrJ更新索引数据

SolrJ可以看做是JDBC,有增删改查操作,对Solr中存储的索引数据进行增删改查操作。

Model类

JDBC操作需要Model类,SolrJ操作数据也需要model类。可以使用SolrJ提供的SolrInputDocument类,也可以自己定义bean。我这里场景需要,所以自定义JavaBean,为下面solrJ操作数据做准备。 @Field(value=”UNIQUEKEY”) 对应solr文档中的字段。

public class QydaInfo {
    @Field(value="UNIQUEKEY")
    private String uniquekey;
    @Field(value="QYDA_QYID")
    private String qyid;
    @Field(value="QYDA_QYMC")
    private String qymc;
    @Field(value="QYDA_SHXYDM")
    private String shxydm;
    @Field(value="QYDA_SSZCH")
    private String sszch;
    @Field(value="QYDA_ZZJGDM")
    private String zzjgdm;
    @Field(value="QYDA_QYBM")
    private String qybm;
    @Field(value="QYDA_ZCDZ")
    private String zcdz;
    @Field(value="UPDATE_TIME")
    private Date updatime;
    @Field(value="QYDA_YWY")
    private Set<String> ywy;
    @Field(value="QYDA_XDR")
    private List<String> xdr;
    @Field(value="QYDA_RYXX")
    private List<String> ryxx;
    @Field(value="QYDA_XKXX")
    private List<String> xkxx;
    @Field(value="QYDA_RCJG")
    private List<String> rcjg;
    @Field(value="QYDA_CYCJ")
    private List<String> cycj;
    @Field(value="QYDA_TSJB")
    private List<String> tsjb;
    @Field(value="QYDA_XZZF")
    private List<String> xzzf;
    @Field(value="QYDA_LHDA")
    private List<String> lhda;
    @Field(value="QYDA_CPXX")
    private List<String> cpxx;

    public QydaInfo(){}

    public QydaInfo(String qyid, String qymc, String shxydm, String sszch, String zzjgdm, String qybm,
            String zcdz, Date updatime) {
        this(qyid,qyid,qymc,shxydm,sszch,zzjgdm,qybm,zcdz,updatime);
    }

    public QydaInfo(String uniquekey, String qyid, String qymc, String shxydm, String sszch, String zzjgdm, String qybm,
            String zcdz, Date updatime) {
        this.uniquekey = uniquekey;
        this.qyid = qyid;
        this.qymc = qymc;
        this.shxydm = shxydm;
        this.sszch = sszch;
        this.zzjgdm = zzjgdm;
        this.qybm = qybm;
        this.zcdz = zcdz;
        this.updatime = updatime;
    }
    //省略get、set方法

编写数据服务类

对solr数据进行操作时,需要借助SolrServer抽象类,类似于Spring Jdbctemplate定义基本的功能,提供增删改查操作。

获取SolrServer

可以通过它的实现子类CloudSolrServer、ConcurrentUpdateSolrServer、HttpSolrServer、LBHttpSolrServer获取,我这里使用HttpSolrServer 获取。

public SolrServer getSolrServer(){
    //HttpSolrServer构造方法需要传solr节点url+索引集collection;如果是CloudSolrServer,则需要传zookeeper字节信息。
    SolrServer solr=new HttpSolrServer(SOLR_URL+"gzyj_qyda_core/");
    return solr;
}

编写基本操作

增加和更新操作是一起的、删除是根据uniquekey

/**
 * 添加或更新
 * @param qyda
 */
public void addOrUpdateIndexData(QydaInfo qyda){
        SolrServer solr=this.getSolrServer();
        try {
            solr.addBean(qyda);
            solr.optimize();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }finally{
            try {
                solr.commit(true,true,true);
            } catch (SolrServerException | IOException e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * 批量添加或更新
     * @param qydaList
     */
    public void addOrUpdateIndexData(List<QydaInfo> qydaList){
        SolrServer solr=this.getSolrServer();
        try {
            solr.addBeans(qydaList);
            solr.optimize();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }finally{
            try {
                solr.commit();
            } catch (SolrServerException | IOException e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * 删除
     * @param uniquekey
     */
    public void deleteIndexData(String uniquekey){
        SolrServer solr=this.getSolrServer();
        try {
            solr.deleteById(uniquekey);
            solr.optimize();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }finally{
            try {
                solr.commit();
            } catch (SolrServerException | IOException e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * 批量删除
     * @param uniquekeys
     */
    public void deleteIndexData(List<String> uniquekeys){
        SolrServer solr=this.getSolrServer();
        try {
            solr.deleteById(uniquekeys, 3000);

        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                solr.commit();
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

SolrServer方法

Modifier and TypeMethod and Description
UpdateResponseadd(Collection docs)
Adds a collection of documents
UpdateResponseadd(Collection docs, int commitWithinMs)
Adds a collection of documents, specifying max time before they become committed
UpdateResponseadd(SolrInputDocument doc)Adds a single document
UpdateResponseadd(SolrInputDocument doc, int commitWithinMs)
Adds a single document specifying max time before it becomes committed
UpdateResponseaddBean(Object obj)
Adds a single bean
UpdateResponseaddBean(Object obj, int commitWithinMs)
Adds a single bean specifying max time before it becomes committed
UpdateResponseaddBeans(Collection beans)
Adds a collection of beans
UpdateResponseaddBeans(Collection beans, int commitWithinMs)
Adds a collection of beans specifying max time before they become committed
UpdateResponsecommit()
Performs an explicit commit, causing pending documents to be committed for indexing
UpdateResponsecommit(boolean waitFlush, boolean waitSearcher)
Performs an explicit commit, causing pending documents to be committed for indexing
UpdateResponsecommit(boolean waitFlush, boolean waitSearcher, boolean softCommit)
Performs an explicit commit, causing pending documents to be committed for indexing
UpdateResponsedeleteById(List ids)
Deletes a list of documents by unique ID
UpdateResponsedeleteById(List ids, int commitWithinMs)
Deletes a list of documents by unique ID, specifying max time before commit
UpdateResponsedeleteById(String id)
Deletes a single document by unique ID
UpdateResponsedeleteById(String id, int commitWithinMs)
Deletes a single document by unique ID, specifying max time before commit
UpdateResponsedeleteByQuery(String query)
Deletes documents from the index based on a query
UpdateResponsedeleteByQuery(String query, int commitWithinMs)
Deletes documents from the index based on a query, specifying max time before commit
DocumentObjectBindergetBinder()
UpdateResponseoptimize()
Performs an explicit optimize, causing a merge of all segments to one.
UpdateResponseoptimize(boolean waitFlush, boolean waitSearcher)
Performs an explicit optimize, causing a merge of all segments to one.
UpdateResponseoptimize(boolean waitFlush, boolean waitSearcher, int maxSegments)
Performs an explicit optimize, causing a merge of all segments to one.
SolrPingResponseping()
Issues a ping request to check if the server is alive
QueryResponsequery(SolrParams params)
Performs a query to the Solr server
QueryResponsequery(SolrParams params, SolrRequest.METHOD method)
Performs a query to the Solr server
QueryResponsequeryAndStreamResponse(SolrParams params, StreamingResponseCallback callback)
Query solr, and stream the results.
abstract NamedListrequest(SolrRequest request)
SolrServer implementations need to implement how a request is actually processed
UpdateResponserollback()
Performs a rollback of all non-committed documents pending.
abstract voidshutdown()
Release allocated resources.

参考:SolrJ wiki

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值