一樣在公司工做中發現了一個現象,app
1.我用/solr/admin/collections?action=CREATE&name=collection&numShards=3&replicationFactor=2建立collectionui
2. delete其中的一個shardthis
3. 使用如下命令增長shard,/admin/collections?action=CREATESHARD&shard=shardName&collection=namespa
如此就會報如下錯誤:shards can be added only to ‘implicit’ collections。code
那麼是什麼緣由呢?問題就出在Solr的兩種ROUTER方式ImplicitDocRouter和CompositeIdRouter。在SolrCloud的開發文檔中有這句話:router
Shards can only created with this API for collections that use the ‘implicit’ router. Use SPLITSHARD for collections using the ‘compositeId’ router. A new shard with a name can be created for an existing ‘implicit’ collection.
也就是說在建立collections時候(若是指定參數numshards參數會自動切換到router=」compositeId」),若是採 用compositeId方式,那麼就不能動態增長shard。若是採用的是implicit方式,就能夠動態的增長shard。進一步來說:blog
ImplicitDocRouter就是和 uniqueKey無關,能夠在請求參數或者SolrInputDocument中添加_route_(_shard_已廢棄,或者指定field參數)參數獲取slice ,(router=」implicit」) .ci
CompositeIdRouter就是根據uniqueKey的hash值獲取slice, (在指定numshards參數會自動切換到router=」compositeId」) .開發
綜上所述,CompositeIdRouter在建立的時候因爲指定了numshards參數即已經固定了hash區間,那麼在update的時 候,根據uniqueid的hash坐落在那個hash區間來決定這份document數據發送至哪一個shard。而ImplicitDocRouter 則是在建立的時候並不選定每一個shard的hash區間,而是在須要update的document中增長_route_字段來存放須要發送的shard 名字,以此shard的名字來決定發送至哪一個shard。不難看出,相對來講ImplicitDocRouter更加靈活。rem
在http://stackoverflow.com/questions/15678142/how-to-add-shards- dynamically-to-collection-in-solr中很好的介紹了動態建立/刪除shard的好處 (ImplicitDocRouter)
1 One solution to the problem is to use the 「implicit router」 when creating your Collection. 2 3 Solr does supports the ability to add New Shards (or DELETE existing shards) to your index (whenever you want) via the 「implicit router」 configuration (CREATE COLLECTION API). 4 5 Lets say – you have to index all 「Audit Trail」 data of your application into Solr. New Data gets added every day. You might most probably want to shard by year. 6 7 You could do something like the below during the initial setup of your collection: 8 9 admin/collections?10 action=CREATE&11 name=AuditTrailIndex&12 router.name=implicit&13 shards=2010,2011,2012,2013,2014&14 router.field=year15 The above command: a) Creates 5 shards – one each for the current and the last 4 years 2010,2011,2012,2013,2014 b) Routes data to the correct shard based on the value of the 「year」 field (specified as router.field)16 17 In December 2014, you might add a new shard in preparation for 2015 using the CREATESHARD API (part of the Collections API) – Do something like:18 19 /admin/collections?20 action=CREATESHARD&21 shard=2015&22 collection=AuditTrailIndex23 The above command creates a new shard on the same collection.24 25 When its 2015, all data will get automatically indexed into the 「2015」 shard assuming your data has the 「year」 field populated correctly to 2015.26 27 In 2015, if you think you don’t need the 2010 shard (based on your data retention requirements) – you could always use the DELETESHARD API to do so:28 29 /admin/collections?30 action=DELETESHARD&31 shard=2015&32 collection=AuditTrailIndex
轉載請註明地址http://www.cnblogs.com/rcfeng/