Mark Needham

Thoughts on Software Development

Archive for the ‘Mark Logic’ Category

MarkLogic: Customising a result set

with one comment

One of the stories we worked on last week had us needing to be able to customise the output of a MarkLogic search query to include some elements which aren’t included in the default view.

We started off with this:

search.xqy

xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
 
declare variable $term as xs:string := xdmp:get-request-field("query", "");
 
search:search($term)

Which gives us back a list of results showing where in the documents the search term appeared.

We wanted to be able to get the title of the document and some other meta data about it though so we needed to make use of the transform-results option to do this.

The original query changes to look like this:

search.xqy

xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
 
declare variable $term as xs:string := xdmp:get-request-field("query", "");
 
search:search($term,
   <options xmlns="http://marklogic.com/appservices/search">
     <transform-results apply="transformed-result" ns="http://www.markhneedham.com/search" at="transform.xqy" />
   </options>)

And then we have another xquery file which we use to do the transformation:

transfom.xqy

xquery version "1.0-ml";
module namespace custom-search = "http://markhneedham.com/search";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
 
declare function custom-search:transformed-result(
         $result as node(),
         $ctsquery as schema-element(cts:query),
         $options as element(search:transform-results)?
) as element(search:snippet)
{
<search:snippet>
{
(
         search:snippet($result, $ctsquery, $options)/*,
         <extra>{$result//*:Id}{$result//*:Title}</extra>
)
}
</search:snippet>
};

We put the extra elements that we wanted to include in our search result in the tag but we can name that anything we want.

The code snippets assume that both search.xqy and transform.xqy are both at the top level of the documents database.

Written by Mark Needham

June 20th, 2011 at 10:36 pm

Posted in Mark Logic

Tagged with

MarkLogic: Deleting all the documents in a database

with 4 comments

We’re using the MarkLogic database on my current project and something that we wanted to do recently was delete all the documents as part of a deployment script.

Getting all of the documents is reasonably easy – we just need to make a call to the doc() function.

We can then iterate through the documents like so:

for $doc in doc() return $doc

We wanted to make use of the xdmp:document-delete function to tear down all of the modules but that needs a uri representing the location of the document in the database which isn’t available in $doc:

xdmp:document-delete

xdmp:document-delete(
$uri as xs:string
) as empty-sequence()

Summary:

Deletes a document from the database.

A colleague pointed out that what we needed to do was pass the document to xdmp:node-uri and then our troubles would be over!

The final solution therefore looks like this:

for $doc in doc() return xdmp:document-delete(xdmp:node-uri($doc))

I was expecting that we would delete the documents by some sort of identifier but I guess this approach makes more sense given the way the data is stored.

It all seems a bit esoteric at the moment!

Written by Mark Needham

June 18th, 2011 at 4:08 pm

Posted in Mark Logic

Tagged with