Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

marklogic - How to use optic-fn, optic-json, optic-xdmp, optic-xs in Optic API

We are exploring Optic API and doing some POCs also to replace our existing code with Optic API query.

As part of our new requirement, we want to use optic-fn, optic-json, optic-xdmp, optic-xs, we spent so much time finding examples or sample code using optic-fn, optic-json, optic-xdmp, optic-xs but we could not find any sample code for reference.

Could anyone help us to give a sample code snippet for each(optic-fn, optic-json, optic-xdmp, optic-xs) so it will be very helpful for us?

Any help is appreciated.

question from:https://stackoverflow.com/questions/65911715/how-to-use-optic-fn-optic-json-optic-xdmp-optic-xs-in-optic-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In XQuery, you import not only the core Optic library

import module namespace op="http://marklogic.com/optic"
  at "/MarkLogic/optic.xqy";

but also the libraries that provide any expression functions that needed in the query. Use any combination of the following libraries:

import module namespace octs="http://marklogic.com/optic/expression/cts"
  at "/MarkLogic/optic/optic-cts.xqy"; 
import module namespace ofn="http://marklogic.com/optic/expression/fn"
  at "/MarkLogic/optic/optic-fn.xqy";
import module namespace ogeo="http://marklogic.com/optic/expression/geo"
  at "/MarkLogic/optic/optic-geo.xqy";
import module namespace ojson="http://marklogic.com/optic/expression/json"
  at "/MarkLogic/optic/optic-json.xqy";
import module namespace omap="http://marklogic.com/optic/expression/map"
  at "/MarkLogic/optic/optic-map.xqy";
import module namespace omath="http://marklogic.com/optic/expression/math"
  at "/MarkLogic/optic/optic-math.xqy";
import module namespace ordf="http://marklogic.com/optic/expression/rdf"
  at "/MarkLogic/optic/optic-rdf.xqy"; 
import module namespace osem="http://marklogic.com/optic/expression/sem"
  at "/MarkLogic/optic/optic-sem.xqy"; 
import module namespace ospell="http://marklogic.com/optic/expression/spell"
  at "/MarkLogic/optic/optic-spell.xqy";
import module namespace osql="http://marklogic.com/optic/expression/sql"
  at "/MarkLogic/optic/optic-sql.xqy"; 
import module namespace oxdmp="http://marklogic.com/optic/expression/xdmp"
  at "/MarkLogic/optic/optic-xdmp.xqy"; 
import module namespace oxs="http://marklogic.com/optic/expression/xs"
  at "/MarkLogic/optic/optic-xs.xqy";

Thereafter, you call the functions in the same way with the important difference that the arguments can include not only literals but op:col("NAME") to specify column values or the return values from other, nested expression functions as in:

=> op:where(
    ofn:starts-with(
       op:col("col1"),
       ofn:substring-before(op:col("col2"), "prefix")
       )
   )

Note that you only use the functions from the Optic expression libraries when operating on values during execution of the query. To operate on literal values passed as arguments when building the plan, use the ordinary functions.

In particular, cts:query() arguments are always constructed in the cts namespace and not the octs namespace.

For details about what functions are exposed as expression functions and about how expression functions are exposed in SJS, see also: https://stackoverflow.com/a/65924267/1091497

Hoping that helps,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...