Reputation: 407
I have a MarkLogic database containing XML documents, and the associated metadata(properties) for each document. I would like to retrieve and show information from the properties of each document while displaying the search results in my application. It'd be great if anyone could shed light on how this could be done.
Thanks!
Upvotes: 0
Views: 522
Reputation: 20414
Next to using the already mentioned result transformations, you can also use the extract-metadata
keyword in search:search and REST api query options. You can find documentation here:
http://docs.marklogic.com/search:search#opt-extract-metadata
Note thought that these will only return the associated value, together with the name of the field or element.
That can sometimes be just a bit too limited, for instance if you would like to return entire html meta elements, where you would need all attributes as well to make use of it. In that case a result-decorator
is your answer. You can find a good example here:
http://docs.marklogic.com/guide/search-dev/query-options#id_35434
HTH!
Upvotes: 1
Reputation: 7335
Just a footnote on Michael Blakele's good suggestions -- if you're using the REST API or Java API, you can also lookup the document properties by installing and applying an XQuery or XSLT transform to the search results on the server:
http://docs.marklogic.com/guide/rest-dev/search#id_94556
Or, if you don't want to install anything on the server, you just use the category parameter to specify what you want to bring back for individual document requests:
http://docs.marklogic.com/guide/rest-dev/documents#id_49889 http://docs.marklogic.com/javadoc/client/com/marklogic/client/document/DocumentManager.html#readMetadata%28java.lang.String,%20T%29
The transform approach will be more efficient, however.
Hoping that helps,
Erik Hennum
Upvotes: 1
Reputation: 7840
If you are using https://docs.marklogic.com/search:search or https://docs.marklogic.com/search:resolve you can supply your own transform function to include whatever you like in each result element.
In the search options, specify a function:
<transform-results apply="snippet" ns="my-namespace" at="/my-library.xqy"/>
The specified function name has to exist in the specified namespace and library, and it must have the correct signature. Sample code:
declare function myns:snippet(
$doc as node(),
$query as schema-element(cts:query),
$options as element(search:transform-results)?)
as element(search:snippet)
{
(: Do whatever you like with each match. :)
element search:snippet {
xdmp:document-properties(xdmp:node-uri($doc)),
$doc }
};
The search API code will call your snippet function once per match, and add the output to the search results. The function signature is important: don't change it. If you want to pass any options to your snippet function, you can use the $options
parameter. It contains a copy of your search:transform-results
element, which can contain any XML you like.
For more about search options, read https://docs.marklogic.com/guide/search-dev/query-options#id_58295
Upvotes: 1