/** * Copy constructor * @param other The document to be copied. */ public Document(Document other) { this(new HashMap<>(other.getDocument()), other.getGuid(), other.getSensorType(), other.getTimestamp(), other.getDocumentID().orElse(null)); }
private IndexRequest createRequest(D document, String index) { if(document.getTimestamp() == null) { throw new IllegalArgumentException("Document must contain the timestamp"); } // if updating an existing document, the doc ID should be defined. // if creating a new document, set the doc ID to null to allow Elasticsearch to generate one. String docId = document.getDocumentID().orElse(null); if(LOG.isDebugEnabled() && document.getDocumentID().isPresent()) { LOG.debug("Updating existing document with known doc ID; docID={}, guid={}, sensorType={}", docId, document.getGuid(), document.getSensorType()); } else if(LOG.isDebugEnabled()) { LOG.debug("Creating a new document, doc ID not yet known; guid={}, sensorType={}", document.getGuid(), document.getSensorType()); } return new IndexRequest() .source(document.getDocument()) .type(document.getSensorType() + "_doc") .index(index) .id(docId) .index(index) .timestamp(document.getTimestamp().toString()); }
default Document getPatchedDocument(RetrieveLatestDao retrieveLatestDao, PatchRequest request, Optional<Long> optionalTimestamp ) throws OriginalNotFoundException, IOException { String guid = request.getGuid(); String sensorType = request.getSensorType(); String documentID = null; Long timestamp = optionalTimestamp.orElse(System.currentTimeMillis()); Map<String, Object> originalSource = request.getSource(); if (originalSource == null) { // no document source provided, lookup the latest Document toPatch = retrieveLatestDao.getLatest(guid, sensorType); if(toPatch != null && toPatch.getDocument() != null) { originalSource = toPatch.getDocument(); documentID = toPatch.getDocumentID().orElse(null); } else { String error = format("Document does not exist, but is required; guid=%s, sensorType=%s", guid, sensorType); throw new OriginalNotFoundException(error); } } Map<String, Object> patchedSource = JSONUtils.INSTANCE.applyPatch(request.getPatch(), originalSource); return new Document(patchedSource, guid, sensorType, timestamp, documentID); } }
@Test public void testWrite() throws Exception { // create some documents to write List<Document> documents = new ArrayList<>(); for(int i=0; i<10; i++) { Document document = Document.fromJSON(createMessage()); documents.add(document); } // write the documents for(Document doc: documents) { writer.addDocument(doc, "bro_index"); } writer.write(); // ensure the documents were written for(Document expected: documents) { Document actual = retrieveDao.getLatest(expected.getGuid(), expected.getSensorType()); assertNotNull("No document found", actual); assertEquals(expected.getGuid(), actual.getGuid()); assertEquals(expected.getSensorType(), actual.getSensorType()); assertEquals(expected.getDocument(), actual.getDocument()); assertTrue(actual.getDocumentID().isPresent()); // the document ID and GUID should not be the same, since the document ID was auto-generated assertNotEquals(actual.getDocument(), actual.getGuid()); } }