53. /* find the document related to the team with nickname "El Verde" */
var sem = require("/MarkLogic/semantics.xqy");
var userInput = "La Verde" ;
var bindings = { "nicknameInput": userInput } ;
var team = sem.sparql("n
prefix dbo: <http://dbpedia.org/ontology/>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select ?teamName where{
?team a dbo:SportsTeam .
?team foaf:nick ?nicknameInput .
?team foaf:name ?teamName .
}",
bindings )
/* convert the valueIterator to an array, and grab the team name */
var teamName = team.toArray()[0].teamName ;
/* show the document with this team name (country name) in the id element */
cts.search(
cts.jsonPropertyWordQuery(
"id",
teamName )
)
56. /* find the documents related to the player David Villa */
var userInput = "David Villa"
var bindings = { "playerNameInput": userInput } ;
var page = sem.sparql('
prefix dbo: <http://dbpedia.org/ontology/>
prefix dbp: <http://dbpedia.org/property/>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix mlpred: <http://marklogic.com/semantics/predicates/>
select ?playerDocURI ?teamDocURI where {
# establish this players IRI
?playerIRI a dbo:SoccerPlayer ;
foaf:name ?playerNameInput .
# find the document describing this player
?playerIRI mlpred:hasDoc ?playerDocURI .
# find the document describing this players national team
?playerIRI a dbo:SoccerPlayer ;
dbp:nationalteam ?natTeamIRI ;
foaf:name ?playerNameInput .
?natTeamIRI mlpred:hasDoc ?teamDocURI .
# find other related documents …
}',
bindings
)
57. /* iterate over the valueIterator (from sem.sparql) and build an array of values
(for display by the search app) */
var docsArray = [] ;
for (var p of page) {
docsArray.push( p.playerDocURI ) ;
docsArray.push( p.teamDocURI ) ;
} ;
fn.doc( docsArray )
59. var sparql = 'select ?name ?p ?value
where {
?name ?p ?value
FILTER ( (?p=<http://example.com/earnings>) ||
?p=<http://example.com/earningsRank>) )
} order by ?name '
/* I'm only interested in a reliable source, where we have more than 70% confidence,
published after Jan 2015 */
var publication = ["forbes on-line", "WSJ", "Bloomberg"]
var date = xs.date("2015-01-01")
var confidence = 70
var ctsQuery =
cts.andQuery( [
cts.elementValueQuery( xs.QName("publication"), publication ),
cts.elementRangeQuery( xs.QName("reported-date"), ">", date ),
cts.elementRangeQuery( xs.QName("confidence"), ">", confidence ) ]
)
/* run a SPARQL query, restricted by a cts query (a document/metadata query). */
var result =
sem.sparql( sparql, [], [], ctsQuery )
result