简体   繁体   English

如何在光学 API 中使用 optic-fn、optic-json、optic-xdmp、optic-xs

[英]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.我们正在探索光学 API 并做一些 POC 以用光学 API 查询替换我们现有的代码。

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.作为我们新要求的一部分,我们想要使用 optic-fn、optic-json、optic-xdmp、optic-xs,我们花了很多时间寻找使用 optic-fn、optic-json、optic-xdmp 的示例或示例代码, optic-xs 但我们找不到任何示例代码供参考。

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?谁能帮助我们为每个(optic-fn、optic-json、optic-xdmp、optic-xs)提供一个示例代码片段,这样对我们很有帮助吗?

Any help is appreciated.任何帮助表示赞赏。

In XQuery, you import not only the core Optic library在 XQuery 中,您不仅可以导入核心光学库

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:此后,您以相同的方式调用函数,其重要区别在于 arguments 不仅可以包含文字,还可以包含op:col("NAME")来指定列值或来自其他嵌套表达式函数的返回值,如下所示:

=> 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.要在构建计划时对作为 arguments 传递的文字值进行操作,请使用普通函数。

In particular, cts:query() arguments are always constructed in the cts namespace and not the octs namespace.特别是,cts:query() arguments 总是在cts命名空间而不是octs命名空间中构造。

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有关哪些函数作为表达式函数公开以及如何在 SJS 中公开表达式函数的详细信息,另请参阅: https://stackoverflow.com/a/65924267/1091497

Hoping that helps,希望有帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM