List<SolrInputDocument> docList = new LinkedList<SolrInputDocument>();for (int i = 0; i < DOC_NUM; i++) {SolrInputDocument doc2 = new SolrInputDocument();doc2.addField("id", "way2" + i);Set set = new HashSet();for (String s : "abc,edf,kkk,lll".split(",")) {set.add(s);}Map map = new HashMap();map.put("set", set);doc2.addField("key_ss", map);docList.add(doc2);}client.add(docList);client.commit();
(二)AutoCommit
可以在solrConfig.xml中的updateHandler設置自動提交機制:
<!-- Enables a transaction log, used for real-time get, durability, andand solr cloud replica recovery. The log can grow as big asuncommitted changes to the index, so use of a hard autoCommitis recommended (see below)."dir" - the target directory for transaction logs, defaults to thesolr data directory."numVersionBuckets" - sets the number of buckets used to keeptrack of max version values when checking for re-orderedupdates; increase this value to reduce the cost ofsynchronizing access to version buckets during high-volumeindexing, this requires 8 bytes (long) * numVersionBucketsof heap space per Solr core.
-->
<updateLog><str name="dir">${solr.ulog.dir:}</str><int name="numVersionBuckets">${solr.ulog.numVersionBuckets:65536}</int>
</updateLog><!-- AutoCommitPerform a hard commit automatically under certain conditions.Instead of enabling autoCommit, consider using "commitWithin"when adding documents. http://wiki.apache.org/solr/UpdateXmlMessagesmaxDocs - Maximum number of documents to add since the lastcommit before automatically triggering a new commit.maxTime - Maximum amount of time in ms that is allowed to passsince a document was added before automaticallytriggering a new commit. openSearcher - if false, the commit causes recent index changesto be flushed to stable storage, but does not cause a newsearcher to be opened to make those changes visible.If the updateLog is enabled, then it's highly recommended tohave some sort of hard autoCommit to limit the log size.--><autoCommit> <maxTime>${solr.autoCommit.maxTime:15000}</maxTime> <openSearcher>false</openSearcher> </autoCommit><!-- softAutoCommit is like autoCommit except it causes a'soft' commit which only ensures that changes are visiblebut does not ensure that data is synced to disk. This isfaster and more near-realtime friendly than a hard commit.--><autoSoftCommit> <maxTime>${solr.autoSoftCommit.maxTime:-1}</maxTime> </autoSoftCommit>
List<SolrInputDocument> docList = new LinkedList<SolrInputDocument>();for (int i = 0; i < DOC_NUM; i++) {SolrInputDocument doc2 = new SolrInputDocument();doc2.addField("id", "way2" + i);Set set = new HashSet();for (String s : "abc,edf,kkk,lll".split(",")) {set.add(s);}Map map = new HashMap();map.put("set", set);doc2.addField("key_ss", map);docList.add(doc2);}client.add(docList);client.commit();