javascriptmarklogicsearch-suggestionmarklogic-10

MarkLogic - search.suggest does not return any values


I'm trying to use search.suggest to return me some suggestions based on a multiple range indexes in my database. So far I've tried with single string element range index, but can't figure out proper syntax I guess.

const search = require('/MarkLogic/appservices/search/search');
const options = 
fn.head(xdmp.unquote('<search:options xmlns:search="http://marklogic.com/appservices/search">' +
 '<default-suggestion-source>' +
   '<range collation="http://marklogic.com/collation//S1" type="xs:string">' +
      '<json-property>title</json-property>' +
   '</range>' +
 '</default-suggestion-source>' +
'</search:options>')).root;
search.suggest("Taiw", options)

I've changed a bit a code snippet from here: https://docs.marklogic.com/10.0/search.suggest but it does not produce any results: enter image description here

I have element range index in my database with data enter image description here enter image description here

I've also tried to use JSON:

const search = require('/MarkLogic/appservices/search/search');
const options = {
  "search:options":{
   "default-suggestion-source":{
      "range":{
         "range":{
            "type":"xs:string",
            "collation":"http://marklogic.com/collation//S1",
            "element":{
               "name":"title"
            }
         }
      }
   }
}}
search.suggest("Taiw", options)

But I end up with:

[javascript] XDMP-AS: (err:XPTY0004) $options as element(search:options)? -- Invalid coercion: object-node{"search:options":object-node{"default-suggestion-source":object-node{...}}} as element(search:options)

What am I doing wrong? Is there any setting I need to set on my database". I'm using JSON data


Solution

  • The example code is wrong.

    The issue is that each of the search option elements should be in the search namespace. So, you either need to apply the search namespace-prefix to all of the elements:

    const search = require('/MarkLogic/appservices/search/search');
    const options = 
    fn.head(xdmp.unquote('<search:options xmlns:search="http://marklogic.com/appservices/search">' +
     '<search:default-suggestion-source>' +
       '<search:range collation="http://marklogic.com/collation//S1" type="xs:string" facet="true">' +
         '<search:element ns="" name="title"/>' +
       '</search:range>' +
     '</search:default-suggestion-source>' +
    '</search:options>')).root;
    search.suggest("Ta", options);
    

    Or, change the declaration of the namespace not to use the search prefix, and then remove the search prefix from all of the elements:

    const search = require('/MarkLogic/appservices/search/search');
    const options = 
    fn.head(xdmp.unquote('<options xmlns="http://marklogic.com/appservices/search">' +
     '<default-suggestion-source>' +
       '<range collation="http://marklogic.com/collation//S1" type="xs:string" facet="true">' +
         '<element ns="" name="title"/>' +
       '</range>' +
     '</default-suggestion-source>' +
    '</options>')).root;
    search.suggest("Ta", options);
    

    In the XQuery equivalent, the example shows declaring the default (no prefix) namespace with the search namespace, and the search prefix was already declared when importing the module and was in-scope, so both with and without a prefix the options elements were bound to that namespace.