1
votes

Is it possible to use pre-defined, persistent Search Options within JSearch, similar to the Search REST API? Looking through the documentation, I was not able to find it out.

JSearch: https://docs.marklogic.com/guide/search-dev/javascript

Query Options: https://docs.marklogic.com/guide/search-dev/query-options

Thank you!

1

1 Answers

1
votes

Not directly. You would have to traverse the options, and construct the search with facets and all yourself. It would not be too hard though. Below a quick attempt. I downloaded existing search options via GET /v1/config/query/all?format=json, and isolated a few path index facets. Here some code that traverses them, and produces facet values:

'use strict';

const jsearch = require('/MarkLogic/jsearch.sjs');

function reference(c) {
  if (c.range) {
    if (c.range['path-index']) {
      return cts.pathReference(c.range['path-index'].text)
    }
  }
}

const options = {
    "options": {
        "constraint": [{
            "name": "Auteur",
            "range": {
                "type": "xs:string",
                "facet": true,
                "collation": "http://marklogic.com/collation/codepoint",
                "facet-option": ["limit=10", "frequency-order", "descending"],
                "path-index": {
                    "text": "*:meta[@name = 'Author']/@content"
                }
            }
        }, {
            "name": "ContentType",
            "range": {
                "type": "xs:string",
                "facet": true,
                "collation": "http://marklogic.com/collation/codepoint",
                "facet-option": ["limit=10", "frequency-order", "descending"],
                "path-index": {
                    "text": "*:meta[@name = 'content-type']/@content"
                }
            }
        }]
    }
};

const facets = options.options.constraint.filter(c => c.range);

jsearch.facets(
  facets.map(f => {
    let ref = reference(f);
    if (ref) {
      return jsearch.facet(f.name, ref);
    }
  }).filter(f => f)
).result('iterator');

HTH!