MarkLogic : Persist and reuse a Xquery result - xquery

I was wondering if there is a way for me use a result from search function in MarkLogic and use it in multiple transformation queries that I have.
For eg.
let $uris := cts:uris(("/example/"),(),cts:element-query(xs:QName("cd:documentTitle"),cts:element-value-query(xs:QName("cd:id"),"abc")))
return (fn:count($uris), $uris)[1 to 20]
The above query say returns me URI for 20 documents. How can I save this result or re-use this result for multiple transformations that I have. All of them working on the same result set but performing different tasks.
Any help would be greatly appreciated.

I'm going to guess you're using CORB to process data based on that return.
You could instead run your query in QConsole or similar and write the following in your URIs module:
let $uris = ("/1.xml", "/2.xml", ...)
return (fn:count($uris), $uris)

Related

Select multiple edges and vertexes in a gremlin query

I am creating a vertex with multiple edges but need a bit of help writing a query to retrieve the data.
Creation query
g.addV("referral")
.as("r")
.property("createdAt", Date.now())
.addE("prospect")
.from_("r")
.to(__.V(user.id))
.addE("opportunity")
.from_("r")
.to(__.V(second_user.id))
.addE("referredBy")
.from_("r")
.to(__.V(business.id))
.select("r")
.next()
I want to run a query that gets data from the first and second user. So far I have
g.V(business.id) //business
.in_("opportunity")
.as("referral")
.outV("referredBy")
.as("referrer")
.inV("prospect")
.as("prospect")
.select("referral", "referrer", "prospect")
.toList()
.next()
I'm getting an error when running this query. I basically want an array of a referral, referrer and prospect in one object that I can iterate through. Also any help on making my initial query better would be helpful.
Please let me know if this makes sense or if you need any other info. Any help would be appreciated.
The errors are because you are using outV when you should be using out. You only need to use inV and outV after outE and inE. So your query should be
g.V(business.id) //business
.in("opportunity")
.as("referral")
.out("referredBy")
.as("referrer")
.in("prospect")
.as("prospect")
.select("referral", "referrer", "prospect")
.toList()
Also you don't need next as you already have toList.
Lastly rather than use as and select I would look at the path step instead.
g.V(business.id) //business
.in("opportunity")
.out("referredBy")
.in("prospect")
.path()
.toList()

Read documents in order

We have a list of URIs and which we need to read in the sequence in which it is passed.
Example1
doc(("/doc1", "/doc2", "/doc3"))
above function should return the content of "/doc1" then "/doc2" and then "/doc3" but it is not happening.
The same thing is happening is with the below query also.
Example2
cts:search(doc(), cts:document-query((("/doc1"),("/doc2"),("/doc3"))))
What will be the solution if I want to read the documents in which sequence I pass the URIs ?
Any help is appreciated..!
cts:search has its own ordering functionality, which you can trigger using functions like cts:index-order. fn:doc takes an array, and will return them in database order. Neither of them sounds like what you are trying to achieve.
I would suggest explicitly iterating over the uris, and fetching the uris one by one. You could use ! operator for this, for instance:
("/doc1", "/doc2", "/doc3") ! doc(.)
HTH!
#grtjn's answer is correct but you could reduce it to one database query and just return the docs in the order that you want with something like
let $uris := ("/doc2", "/doc1", "/doc3")
let $docs := map:new(
fn:doc($uris) ! map:entry(xdmp:node-uri(.), .)
)
for $i in $uris
return map:get($docs, $i)

Get execution time of XQuery in eXist

How can I get a reliable measure of the execution time of an XQuery in eXist-db?
It seems like eXide takes into account even the render of the results in the browser, am I wrong?
eXide measures only the time required to execute the query, not to render the results in the browser or serialize the results. (To confirm, see the eXide source where queries are executed and the duration is measured: https://github.com/wolfgangmm/eXide/blob/develop/controller.xql#L155-L193. The first timestamp taken on line 159 and the 2nd on 189-90.)
You can measure the duration of your own queries using this same technique:
xquery version "3.1";
let $start-time := util:system-time()
let $query-needing-measurement := (: insert query or function call here :)
let $end-time := util:system-time()
let $duration := $end-time - $start-time
let $seconds := $duration div xs:dayTimeDuration("PT1S")
return
"Query completed in " || $seconds || "s."
Another common approach is to log this message or send it to the Monex app's console. For this, use util:log() (built-in) or console:log() (requires Monex, which if not already installed can be installed from the Dashboard > Package Manager).
Also, see the XQuery Wikibook's entry, https://en.wikibooks.org/wiki/XQuery/Timing_a_Query.
Note: Updated with suggestion by Gunther from comments below.

Using cfs:http-publish

I'm trying to use the cfs:http-publish package at https://github.com/CollectionFS/Meteor-http-publish. While I've got the GET - /api/list functionality working, I don't know how to obtain a single document:
(GET - /api/list/:id - find one published document).
Can someone provide a curl example of this, assuming a certain collection of objections.
eg: {a:3, b:2}, {a:4, b:3}, and requiring to obtain the object with {a:3}.
Thanks.
You need to put it in the query function.
HTTP.publish({collection: myList},function( ){
return myList.find(this.query);
});
this.query contains the data you sent with your request.
curl http://localhost:3000/api/myList?a=3
I don't know enough about mongo to know if this is a potential security risk, if anyone can comment on that please do.

APIGEE querying data that DOESN'T match condition

I need to fetch from BaaS data store all records that doesn't match condition
I use query string like:
https://api.usergrid.com/<org>/<app>/<collection>?ql=location within 10 of 30.494697,50.463509 and Partnership eq 'Reject'
that works right (i don't url encode string after ql).
But any attempt to put "not" in this query cause "The query cannot be parsed".
Also i try to use <>, !=, NE, and some variation of "not"
How to configure query to fetch all records in the range but Partnership NOT Equal 'Reject' ?
Not operations are supported, but are not performant because it requires a full scan. When coupled with a geolocation call, it could be quite slow. We are working on improving this in the Usergrid core.
Having said that, in general, it is much better to inverse the call if possible. For example, instead of adding the property when the case is true, always write the property to every new entity (even when false), then edit the property when the case is true.
Instead of doing this:
POST
{
'name':'fred'
}
PUT
{
'name':'fred'
'had_cactus_cooler':true
}
Do this:
POST
{
'name':'fred'
'had_cactus_cooler':'no'
}
PUT
{
'name':'fred'
'had_cactus_cooler':'yes'
}
In general, try to put your data in the way you want to get it out. Since you know upfront that you want to query on whether this property exists, simply add it, but with a negative value. The update it when the condition becomes true.
You should be able to use this syntax:
https://api.usergrid.com/<org>/<app>/<collection>?ql=location within 10 of 30.494697,50.463509 and not Partnership eq 'Reject'
Notice that the not operator comes before the expression (as indicated in the docs).

Resources