solr索引提交方式

来源:互联网 发布:广发证券交易软件 编辑:程序博客网 时间:2024/05/17 20:22

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);


原创粉丝点击