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 ywy;
    @Field(value="QYDA_XDR")
    private List xdr;
    @Field(value="QYDA_RYXX")
    private List ryxx;
    @Field(value="QYDA_XKXX")
    private List xkxx;
    @Field(value="QYDA_RCJG")
    private List rcjg;
    @Field(value="QYDA_CYCJ")
    private List cycj;
    @Field(value="QYDA_TSJB")
    private List tsjb;
    @Field(value="QYDA_XZZF")
    private List xzzf;
    @Field(value="QYDA_LHDA")
    private List lhda;
    @Field(value="QYDA_CPXX")
    private List 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 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 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 Type Method and Description
UpdateResponse add(Collection docs)
Adds a collection of documents
UpdateResponse add(Collection docs, int commitWithinMs)
Adds a collection of documents, specifying max time before they become committed
UpdateResponse add(SolrInputDocument doc)Adds a single document
UpdateResponse add(SolrInputDocument doc, int commitWithinMs)
Adds a single document specifying max time before it becomes committed
UpdateResponse addBean(Object obj)
Adds a single bean
UpdateResponse addBean(Object obj, int commitWithinMs)
Adds a single bean specifying max time before it becomes committed
UpdateResponse addBeans(Collection beans)
Adds a collection of beans
UpdateResponse addBeans(Collection beans, int commitWithinMs)
Adds a collection of beans specifying max time before they become committed
UpdateResponse commit()
Performs an explicit commit, causing pending documents to be committed for indexing
UpdateResponse commit(boolean waitFlush, boolean waitSearcher)
Performs an explicit commit, causing pending documents to be committed for indexing
UpdateResponse commit(boolean waitFlush, boolean waitSearcher, boolean softCommit)
Performs an explicit commit, causing pending documents to be committed for indexing
UpdateResponse deleteById(List ids)
Deletes a list of documents by unique ID
UpdateResponse deleteById(List ids, int commitWithinMs)
Deletes a list of documents by unique ID, specifying max time before commit
UpdateResponse deleteById(String id)
Deletes a single document by unique ID
UpdateResponse deleteById(String id, int commitWithinMs)
Deletes a single document by unique ID, specifying max time before commit
UpdateResponse deleteByQuery(String query)
Deletes documents from the index based on a query
UpdateResponse deleteByQuery(String query, int commitWithinMs)
Deletes documents from the index based on a query, specifying max time before commit
DocumentObjectBinder getBinder()
UpdateResponse optimize()
Performs an explicit optimize, causing a merge of all segments to one.
UpdateResponse optimize(boolean waitFlush, boolean waitSearcher)
Performs an explicit optimize, causing a merge of all segments to one.
UpdateResponse optimize(boolean waitFlush, boolean waitSearcher, int maxSegments)
Performs an explicit optimize, causing a merge of all segments to one.
SolrPingResponse ping()
Issues a ping request to check if the server is alive
QueryResponse query(SolrParams params)
Performs a query to the Solr server
QueryResponse query(SolrParams params, SolrRequest.METHOD method)
Performs a query to the Solr server
QueryResponse queryAndStreamResponse(SolrParams params, StreamingResponseCallback callback)
Query solr, and stream the results.
abstract NamedList request(SolrRequest request)
SolrServer implementations need to implement how a request is actually processed
UpdateResponse rollback()
Performs a rollback of all non-committed documents pending.
abstract void shutdown()
Release allocated resources.

参考:SolrJ wiki

你可能感兴趣的:(搜索,SolrJ更新操作,SolrServer)