Skip to content

Instantly share code, notes, and snippets.

@dmcassel
Last active September 15, 2015 14:49
Show Gist options
  • Save dmcassel/c985979daaa214b9cad1 to your computer and use it in GitHub Desktop.
Save dmcassel/c985979daaa214b9cad1 to your computer and use it in GitHub Desktop.
/*
* Find the active users who mention "sell out" in their profile. Of the three
* most recent registrants, order by most number of tags.
*/
var query = cts.andQuery([
cts.orQuery([
cts.jsonPropertyValueQuery("isActive", true),
cts.jsonPropertyWordQuery("about", "sell out") // The text "sell out" or its stems
]),
cts.collectionQuery("fake data")
]);
// Order by registration date, newest to oldest. Note that the
// jsonPropertyReference requires a range index on the "registered" property
// (see "Import XQuery"). If that index doesn't exist, disregard the ordering.
// Checking for indexes at runtime is not a best practice for production code,
// but is illustrative here.
var order = null;
try {
order = cts.indexOrder(cts.jsonPropertyReference("registered"), ["descending"]);
} catch(err) {
if("XDMP-ELEMRIDXNOTFOUND" !== err.name) { throw err; }
}
// Create an empty Array to accumulate the results.
var results = [];
for(
// Use subsequence for efficient pagination (limit/skip)
var item of fn.subsequence(
// Run the search out of the indexes, using the order specification above
cts.search(query, ["unfiltered", order]),
// Just get the top 3.
1, 3
)
) {
// Synthetic wrapper to hold projected fields and metadata from the search results.
var obj = {
// The Node instance reports the relevance score of each search result.
relevanceScore: item.score
};
// Search returns a document node. Turn this into a JavaScript object.
item = item.toObject();
// Add properties we care about to the result object
["guid", "name", "isActive", "about"].forEach(function(prop) {
obj[prop] = item[prop];
});
// Get the count of the tags rather than the list.
obj.tagCount = item.tags.length;
results.push(obj);
}
results;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment