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 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更新索引数据相关推荐

  1. mysql 索引 数据页_数据库索引数据页

    索引的好处 索引带来的益处可能很多读者会认为只是"能够提高数据检索的效率,降低数据库的IO成本". 确实,在数据库中表的某个字段创建索引,所带来的最大益处就是将该字段作为检索条件时 ...

  2. Solr 16 - Solr中添加、更新、删除数据的几种方式 (在URL上或Web页面中操作)

    目录 1 添加/更新索引数据 1.1 JSON格式的操作 1.2 XML格式的操作 2 删除索引数据 2.1 删除符合特定条件的数据 2.2 删除指定ID的数据 2.3 删除全部索引数据 3 在doc ...

  3. Oracle数据库面试题 精选 Oracle 面试题

    Oracle数据库面试题 1.解释冷备份和热备份的不同点以及各自的优点 冷备份 发生在数据库已经正常关闭的情况下,将关键性文件拷贝到另外位置的一种说法.适用于所有模式的数据库. 优点 1. 是非常快速 ...

  4. solrj mysql_solr7.4.0+mysql+solrj(简而优美)

    目录: 1 solr7部署+创建核心 2 solr mysql 连接 2.1 导入相关 jar包 2.2 配置连接信息 2.3 配置中文分析器 3 solrj JAVA客户端应用 3.1 solrj ...

  5. solr java 客户端_Solr JAVA客户端SolrJ的使用

    一.Solrj简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底 ...

  6. solor mysql_solr7.4.0+mysql+solrj(简而优美)

    目录: 1 solr7部署+创建核心 2 solr mysql 连接 2.1 导入相关 jar包 2.2 配置连接信息 2.3 配置中文分析器 3 solrj JAVA客户端应用 3.1 solrj ...

  7. 利用外部命令Oralce数据库导入导出

    1--数据库导出(exp) 首先进入命令行 导出数据库 在命令行中输入如下命令: exp   c2j/c2j@c2j file=c:/table.dmp tables=jbitaku,jbitakum ...

  8. 千万级游标_在一个千万级的数据库查寻中,如何提高查询效率

    在一个千万级的数据库查寻中,如何提高查询效率? 1)数据库设计方面: a.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. b.应尽量避免在 w ...

  9. 在一个千万级的数据库查寻中,如何提高查询效率?

    1)数据库设计方面:  a. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.  b. 应尽量避免在 where 子句中对字段进行 null 值 ...

最新文章

  1. python中异常的姓名
  2. C++与MATLAB数组的存储结构
  3. 要嫁就嫁程序员!原因很简单:五成表示工资愿交给另一半!
  4. eclipse lib中包不能打开_Eclipse环境搭建
  5. ansible(2)——基本命令
  6. SSL 多线程通信 linux openSSL C API编程
  7. 79.纯 CSS 创作单元素麦当劳金拱门 Logo(自创)
  8. Effective C++ Notebook
  9. Vulkan Tutorial
  10. [初级-详细]新大陆NewLand云平台Android离线程序开发(离线导入Moudle)
  11. 生活四大勤,让老人延年益寿
  12. 【微信小程序】本地服务页面案例实现
  13. Android开发固定app图标大小,Android和IOS开发图标、启动页尺寸
  14. VS x86 x64 anycpu 编译运行对照表
  15. 记录一下pageX,offsetX,clientX,offsetLeft,offsetWidth,pageYoffset,scrollTop,scrollY,等。原文摘自MDN文档库。保证正确。
  16. ubuntu安装pandas
  17. MATLAB(完备)之图像.tif到真彩色图像、索引色图像、灰度图像、 真彩色图像RGB、YIQ图像、HSV图像、YCbCr图像转换代码
  18. canvas画地图运动轨迹【自己定位】
  19. 可以刷新页面的随机php接口,随机一言 API 接入方法,每次刷新都会带来一个新的语句...
  20. 讲解Guitar Pro 7使用向导的技巧

热门文章

  1. php redis redis server went away,php连接redis出现Redis server went away,rediswent
  2. python爬虫技术如何挣钱?教你爬虫月入三万!
  3. 软件测试工程师一分钟自我介绍?
  4. UG NX 12 类选择器
  5. 浅谈人力外派与校企合作的可能性,或许是另一条新的出路,收藏
  6. 使用Fireworks和Icofx制作一个ICO图标
  7. 在mini6410嵌入式linux上使用QT4和mitab显示mapinfo或mif格式电子地图
  8. 潘爱民老师聊天精彩摘录
  9. lattice diamond烧写问题汇总
  10. ROS routerOS 软路由