Step 1 - Create Child Documents from Organization with an Aggregation Processor
-
Add an aggregation processor:
- Select Groovy as format
- For Name, enter Organization_UC_7
- Click Accept
-
Replace the default code by the following one:
// Process nodes having the “organization” type process("organization") { // Log the content of the document passing through this processor log.info "Child creation for organization: " + it // Find the top country for import trade per year trades = // Get all paths to related country nodes match(it, "-isMemberOf[country].import[trade]") *.last() // fetch last node year_top = [:].withDefault() { [:].withDefault() {0} } // Big Integer def bInt = 0G; year_top_volume = [:].withDefault() { bInt } // Build the child collection trades.each { trade -> if (year_top[trade.metas.getValue("year")]["volume"] < trade.metas.getValue("volume") .toInteger()) { year_top[trade.metas.getValue("year")]["volume"] = trade.metas.getValue("volume").toInteger(); year_top[trade.metas.getValue("year")]["country"] = trade.metas.getValue("country_id"); year_top_volume[trade.metas.getValue("year")] += trade.metas.getValue("volume").toInteger(); } } // Caution! Before pushing any new document, remove existing child documents, if any. // This operation is yielded automatically. deleteDocumentChildren(it, "/year_import/"); // create child documents year_top.each { key, value -> log.info "Year:" + key + " - " + value["volume"] + " - " + value["country"] + " - " + year_top_volume[key] ; child = createChildDocument( it, // root '/year_import/' + key, // child URI "ico_trade" // type ); // Add metas to the child document child.metas.parent_identifier = it.getUri(); child.directives.datamodel_class = "ico_trade"; // directly set with the type defined in createDocument but it can be overridden if needed child.metas.org_id = it.metas["org_id"]; child.metas.year = key; child.metas.country_id = value["country"]; child.metas.volume = value["volume"]; child.metas.globalvolume = year_top_volume[key]; yield child; } }
- Save and apply the configuration.