marklogicmarklogic-10marklogic-optic-api

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.


Solution

  • 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,