How to enable `relevance-trace` using MarkLogic Java API?

Viewed 74

I'm implementing quite a complex search using MarkLogic Java API. I would like to enable relevance-trace (Relavance trace) to see how my results are scored. Unfortunately, I don't know how to enable it in Java API. I have tried something like:

    DatabaseClient client = initClient();
    var qmo = client.newServerConfigManager().newQueryOptionsManager();
    var searchOptions = "<search:options xmlns=\"http://marklogic.com/appservices/search\">\n"
        + "    <search-option>relevance-trace</search-option>\n"
        + "  </search:options>";
    qmo.writeOptions("searchOptions", new StringHandle(searchOptions).withFormat(Format.XML));
    QueryManager qm = client.newQueryManager();
    StructuredQueryBuilder qb = qm.newStructuredQueryBuilder("searchOptions");
    
    // query definition

    qm.search(query, new SearchHandle())

Unfortunately it ends up with following error:

   "Local message: /config/query write failed: Internal Server Error. Server Message: XDMP-DOCNONSBIND: 
   xdmp:get-request-body(\"xml\") -- No namespace binding for prefix search at  line 1 . See the 
   MarkLogic server error log for further detail."

My question is how to use search options in MarkLogic API, especially I'm interested in relevance-trace and simple-score

Update 1

As suggested by @Jamess Kerr I have change my options to

    var searchOptions = "<options xmlns=\"http://marklogic.com/appservices/search\">\n"
        + "    <search-option>relevance-trace</search-option>\n"
        + "  </options>";

but unfortunately, it still doesn't work. After that change I get error:

   Local message: /config/query write failed: Internal Server Error. Server Message: XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:apply(function() as item()*) -- Cannot apply an update function from a query . See the MarkLogic server error log for further detail.
2 Answers

Your search options XML uses the search: namespace prefix but you don't define that prefix. Since you are setting the default namespace, just drop the search: prefix from the search:options open and close tags.

The original Java Query contains both syntactical and semantic issues:

First of all, it is an invalid MarkLogic XQuery in the sense that it has only query option(s) portion. Bypassing the namespace binding prefix is another wrong end of the stick.

To tweak your original query, please replace a search text in between the search:qtext tag ( the pink line ) and run the query. enter image description here

Result:

Matched and Listing 2 documents:

Matched 1 locations in /medals/coin_1333113127296.xml with 94720 score:
73. …pulsating maple leaf coin another world-first, the [Royal Canadian Mint]is proud to launch a numismatic breakthrough from its ambitious and creative R&D team...
Matched 1 locations in /medals/coin_1333078361643.xml with 94720 score:
71. ...the [Royal Canadian Mint]and Royal Australian Mint have put an end to the dispute relating to the circulation coin colouring process...

Without a semantic criterion, to put it into context, your original query will be an equivalent of removing the search:qtext and performing a fuzzy search.

Note:

If you use serialised term search or search constraints instead of text search, you should get higher score results.

MarkLogic Java API operates in unfiltered mode by default, while the cts:search operates in filtered mode by default. Just be mindful of how you construct the query and the expected score in Java API. And it is really intended for bulk data write/extract/transformation. qconsole is, in my opinion, more befitting to tune specific query and gather search score, relevance and computation details.

Related