Skip to content

Instantly share code, notes, and snippets.

@damonfeldman
Created November 11, 2021 14:53
Show Gist options
  • Save damonfeldman/3ceb439faf7b3d933f6db7ff2c505164 to your computer and use it in GitHub Desktop.
Save damonfeldman/3ceb439faf7b3d933f6db7ff2c505164 to your computer and use it in GitHub Desktop.
'use strict';
const op = require('/MarkLogic/optic');
/**
* relationIRIString the string representation of the predicate in a relationship triple
* The relationship triple must be of the form <leftIRI> <relationIRIPredicateString> <rightIRI> and there must
* be RDF triples relating each <iri> to the doc URI via <hasURI>.
* side1CTS - a cts.query that consntrains documents on the left of the overall join
*/
function joinTwoWithQueries(relationIRIPredicateString, leftSideCts, rightSideCts) {
let joinSparql =
` SELECT ?joinURI1 ?joinURI2
WHERE {
?side1Item <${relationIRIPredicateString}> ?side2Item.
?side1Item <hasURI> ?joinURI1.
?side2Item <hasURI> ?joinURI2.} `
let uriJoinView = op.fromSPARQL(joinSparql)
let item1Plan = op.fromSearch(leftSideCts) // returns ['fragmentId', 'score', 'quality']
.select(op.as("FID1", op.col("fragmentId")))
.joinDocUri('URI1', op.fragmentIdCol('FID1'))
let item2Plan = op.fromSearch(rightSideCts)
.select(op.as("FID2", op.col("fragmentId")))
.joinDocUri('URI2', op.fragmentIdCol('FID2'))
let uriPairs =
uriJoinView
.joinInner(item1Plan, op.on("joinURI1", "URI1"))
.joinInner(item2Plan, op.on("joinURI2", "URI2"))
.select([op.col("URI1"), op.col("URI2")])
.result()
return uriPairs
}
let claimCts = cts.jsonPropertyValueQuery("type", "medical")
let memberCts = cts.jsonPropertyValueQuery("given", "alfonse")
joinTwoWithQueries("isForMember", claimCts, memberCts)
@damonfeldman
Copy link
Author

This function is intended to allow a simple join between two document types, with a separate constraining cts.query on each type. The join relies on a particular pattern of (minimal) RDF relating the graph IRIs for each entity with the document URIs.

The goal is to be able to minimally enrich documents with RDF (just three triples) and then easily join documents without getting into the specifics of Optic, using a simple function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment