Search code examples
xquerymarklogicmarklogic-8roxy

MarkLogic - Custom Search Snippet


I am using Roxy to manage my project. Also using MarkLogic 8.0-6.1

I am trying to submit a searchTerm, and return a custom formatted search:snippet

Here are the complete steps that I am taking:

./../roxy/ml new test-app --server-version=8 --app-type=rest

Configure my build.properties

cd test-app/ ./ml local bootstrap

Now I have my project Structure.

Create File - test-app/rest-api/ext/show-search.xqy

xquery version "1.0-ml";

module namespace ss = "http://marklogic.com/rest-api/resource/show-search";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy";




declare
function ss:get(
  $context as map:map,
  $params  as map:map
) as document-node()*
{

  map:put($context, "output-types", "application/json"),
  map:put($context, "output-status", (200, "OK")),

  let $search-term := map:get($params, "searchTerm")
  let $query := search:search($search-term,
      <options xmlns="http://marklogic.com/appservices/search">
        <transform-results apply="raw"/>
      </options>
      )

  return document {$query} 
};

(:
 :)
declare 
function ss:put(   
    $context as map:map,
    $params  as map:map,
    $input   as document-node()*
) as document-node()?
{
  map:put($context, "output-types", "application/xml"),
  map:put($context, "output-status", (201, "Created")),
  document { "PUT called on the ext service extension" }
};

(:
 :)
declare 
function ss:post(
    $context as map:map,
    $params  as map:map,
    $input   as document-node()*
) as document-node()*
{
  map:put($context, "output-types", "application/xml"),
  map:put($context, "output-status", (201, "Created")),
  document { "POST called on the ext service extension" }
};

(:
 :)
declare 
function ss:delete(
    $context as map:map,
    $params  as map:map
) as document-node()?
{
  map:put($context, "output-types", "application/xml"),
  map:put($context, "output-status", (200, "OK")),
  document { "DELETE called on the ext service extension" }
};

The above GET request uses the transform-results apply=raw option, deploys, and functions properly (I have some test documents).

However I do not want to return the whole document, I want to return a whole section of the json that had a match, no matter where in that seciton the match happened (lower levels)

So I try to write my own snipper

Create File - test-app/rest-api/ext/show-search-snipper.xqy

xquery version "1.0-ml";

module namespace sss = "http://marklogic.com/rest-api/resource/show-search-snipper";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy";

declare
function sss:my-snippet(
    $result as node(),
    $ctsquery as schema-element(cts:query),
    $options as element(search:transform-results)?
) as element(search:snippet)
{
    <search:snippet>

    </search:snippet>
};

I then update the search:search call to the following

  let $query := search:search($search-term,
      <options xmlns="http://marklogic.com/appservices/search">
        <transform-results apply="my-snippet" ns="http://marklogic.com/rest-api/resource/show-search-snipper" at="show-search-snipper.xqy"/>
      </options>
      )

Now I should have everything I need (I think)

I run the deploy ./ml local deploy rest

and get the following

Minty-linux test-app # ./ml local deploy rest Loading REST properties in /opt/this-is-a-test/test-app/rest-api/config/properties.xml Loading REST options in /opt/this-is-a-test/test-app/rest-api/config/options

Loading REST extensions from /opt/this-is-a-test/test-app/rest-api/ext

ERROR: 400 "Bad Request" ERROR: {"errorResponse":{"statusCode":400, "status":"Bad Request", "messageCode":"RESTAPI-INVALIDCONTENT", "message":"RESTAPI-INVALIDCONTENT: (err:FOER0000) Invalid content: invalid show-search-snipper extension: show-search-snipper either is not a valid module or does not provide extension functions (delete, get, put, post) in the http://marklogic.com/rest-api/resource/show-search-snipper namespace"}}

So I tried moving the show-search-snipper.xqy file up 1 level (to test-app/rest-api/show-search-snipper.xqy`

Run the deployment Deployment Works No errors Hit the URL and receive the following

500 Internal Server Error INTERNAL ERROR RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: Extension show-search does not exist. . See the MarkLogic server error log for further detail.

Although I know the extension was created properly, since it worked fine before the introduction of the custom snip function. (with apply="raw")

Any thoughts how I can apply my custom snip function or what I am doing wrong in deployment?


enter image description here


Solution

  • Should you decide to stick with a custom snippeter:

    It looks like Roxy is trying to treat it your snippeter module as a resource extension, which it is not. Your snippeter should be just a vanilla module in the modules db.

    IDK how to configure Roxy, unfortunately, but what you're aiming for is to get Roxy to either install it using PUT /v1/ext/directories/asset or a straight up insert (`PUT /v1/documents) on your modules db. See http://docs.marklogic.com/REST/PUT/v1/ext/[directories]/[asset].

    Assuming Roxy uses /ext, then the path to your snippeter would NOT be the unqualified path you have in your options. It would be an absolute path rooted at /ext/. See http://docs.marklogic.com/guide/rest-dev/search#id_72390.