solr索引提交方式

本文介绍了使用SolrJ进行索引维护时的不同提交方式,包括硬提交、在指定时间内完成硬提交及自动提交等,并详细阐述了这些提交方式的工作原理及优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. commit      

在使用solrj进行索引维护的时候,直接solrServer.commit();进行提交。

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/dtrace");

SolrInputDocument doc1 = new SolrInputDocument();

doc1.addField("id", i);

solrServer.add(doc1);

solrServer.commit();

这种提交叫硬提交(hard commit), 使用这种提交会把文档立即持久化到磁盘,并可以让你能立马查询到它,因为它会开启一个新的searcher,但是它缺点很明显,就是很耗性能,并会阻塞到提交任务完成,使用它是非常昂贵的操作。 查看solrj的源码发现commit()方法的实现是commit(true,true);及waitFlush=true 和 waitSearcher=true。

2. commitWithin

使用solrJ设置solr在多少毫秒内进行硬提交(commit),将会高速solr在10s内提交我的document。

UpdateRequest req = new UpdateRequest();

req.add(mySolrInputDocument);

req.setCommitWithin(10000);

req.process(server);

3. AutoCommit

  在solrconfig.xml配置文件的updateHandler节点,提供了autoCommit和autoSoftCommit两种提交索引的方式。

3.1 autoCommit

就是自动commit,这里的commit指的是hard commit,里面有几个参数:
maxDocs:add到solr缓冲区的文档数量超过这个值时,自动执行commit把结果写入到磁盘索引文件中
maxTime:最长多久执行一次commit,即如果文档一直没有通过commit沉淀到磁盘上,那么持久性就可能出问题,所以要隔一段时间就执行一次commit
openSearcher:要不要新开一个searcher,如果要新开,那么目前已有的cache就会失效,具体代码可以看SolrIndexSearcher,它里面有几个Cache成员变量,如果你新建立一个searcher那么cache失效也是正常的了,除非你有autoWarming机制。在hard commit的时候设置openSearcher=false,那么执行完hard commit之后,磁盘文件虽然改变了,但是searcher没有新开,需要reload或者重启,才能看到索引中的新文档。

3.2 autoSoftCommit

自动软提交只确保改变的索引是可见的,但不保证数据会同步到磁盘。该提交方式比应提交来说更快,更接近实时,就是所谓的支持near real-time (NRT) searching近实时查询。如果不想执行autoSoftCommit(autoSoftCommit的maxTime设置成-1),

    <!-- softAutoCommit is like autoCommit except it causes a
         'soft' commit which only ensures that changes are visible
         but does not ensure that data is synced to disk.  This is
         faster and more near-realtime friendly than a hard commit.
      -->
     <autoSoftCommit>
         <maxTime>${solr.autoSoftCommit.maxTime:-1}</maxTime>
     </autoSoftCommit>

  客户端只需要

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/dtrace");

SolrInputDocument doc1 = new SolrInputDocument();

doc1.addField("id", i);

solrServer.add(doc1);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值