SolrUpdateDao implements UpdateDao { @Override public Map<Document, Optional<String>> batchUpdate(Map<Document, Optional<String>> updates) throws IOException { Map<String, Collection<SolrInputDocument>> solrCollectionUpdates = new HashMap<>(); Set<String> collectionsUpdated = new HashSet<>(); for (Entry<Document, Optional<String>> entry : updates.entrySet()) { SolrInputDocument solrInputDocument = SolrUtilities.toSolrInputDocument(entry.getKey()); Optional<String> index = SolrUtilities .getIndex(config.getIndexSupplier(), entry.getKey().getSensorType(), entry.getValue()); if (index.isPresent()) { Collection<SolrInputDocument> solrInputDocuments = solrCollectionUpdates .getOrDefault(index.get(), new ArrayList<>()); solrInputDocuments.add(solrInputDocument); solrCollectionUpdates.put(index.get(), solrInputDocuments); collectionsUpdated.add(index.get()); } else { String lookupIndex = config.getIndexSupplier().apply(entry.getKey().getSensorType()); Collection<SolrInputDocument> solrInputDocuments = solrCollectionUpdates .getOrDefault(lookupIndex, new ArrayList<>()); solrInputDocuments.add(solrInputDocument); solrCollectionUpdates.put(lookupIndex, solrInputDocuments); collectionsUpdated.add(lookupIndex); } } try { for (Entry<String, Collection<SolrInputDocument>> entry : solrCollectionUpdates .entrySet()) { this.client.add(entry.getKey(), entry.getValue()); } for (String collection : collectionsUpdated) { this.client.commit(collection); } } catch (SolrServerException e) { throw new IOException(e); } return updates; } SolrUpdateDao(SolrClient client, SolrRetrieveLatestDao retrieveLatestDao, AccessConfig config); @Override Document update(Document update, Optional<String> rawIndex); @Override Map<Document, Optional<String>> batchUpdate(Map<Document, Optional<String>> updates); @Override Document addCommentToAlert(CommentAddRemoveRequest request); @Override Document addCommentToAlert(CommentAddRemoveRequest request, Document latest); @Override Document removeCommentFromAlert(CommentAddRemoveRequest request); @Override Document removeCommentFromAlert(CommentAddRemoveRequest request, Document latest); void convertCommentsToRaw(Map<String,Object> source);  }
@Test public void batchUpdateShouldProperlyUpdateDocuments() throws Exception { Document broDocument1 = new Document(new HashMap<String, Object>(){{ put("broField1", "value"); put("guid", "broGuid1"); }}, "broGuid1", "bro", 0L); Document broDocument2 = new Document(new HashMap<String, Object>(){{ put("broField2", "value"); put("guid", "broGuid2"); }}, "broGuid2", "bro", 0L); Map<Document, Optional<String>> updates = new HashMap<Document, Optional<String>>(){{ put(broDocument1, Optional.of("bro")); put(broDocument2, Optional.of("bro")); }}; SolrInputDocument broSolrInputDocument1 = new SolrInputDocument(); broSolrInputDocument1.addField("broField1", "value"); broSolrInputDocument1.addField("guid", "broGuid1"); SolrInputDocument broSolrInputDocument2 = new SolrInputDocument(); broSolrInputDocument2.addField("broField2", "value"); broSolrInputDocument2.addField("guid", "broGuid2"); solrUpdateDao.batchUpdate(updates); verify(client).add(eq("bro"), argThat(new SolrInputDocumentListMatcher(Arrays.asList(broSolrInputDocument1, broSolrInputDocument2)))); }
@Test public void batchUpdateShouldProperlyUpdateDocumentsWithoutIndex() throws Exception { Document snortDocument1 = new Document(new HashMap<String, Object>(){{ put("snortField1", "value"); put("guid", "snortGuid1"); }}, "snortGuid1", "snort", 0L); Document snortDocument2 = new Document(new HashMap<String, Object>(){{ put("snortField2", "value"); put("guid", "snortGuid2"); }}, "snortGuid2", "snort", 0L); Map<Document, Optional<String>> updates = new HashMap<Document, Optional<String>>(){{ put(snortDocument1, Optional.empty()); put(snortDocument2, Optional.empty()); }}; SolrInputDocument snortSolrInputDocument1 = new SolrInputDocument(); snortSolrInputDocument1.addField("snortField1", "value"); snortSolrInputDocument1.addField("guid", "snortGuid1"); SolrInputDocument snortSolrInputDocument2 = new SolrInputDocument(); snortSolrInputDocument2.addField("snortField2", "value"); snortSolrInputDocument2.addField("guid", "snortGuid2"); solrUpdateDao.batchUpdate(updates); verify(client).add(eq("snort"), argThat(new SolrInputDocumentListMatcher(Arrays.asList(snortSolrInputDocument1, snortSolrInputDocument2)))); }