简体   繁体   English

如何在光学 API 中使用 fn:contains()、fn:starts-with() 和 fn:ends-with

[英]How to use fn:contains(), fn:starts-with() and fn:ends-with in Optic API

Earlier we used the FLOWR query to satisfy our search requirement, since data is getting increased day by day so we decided to use Indexing for better search performance.之前我们使用 FLOWR 查询来满足我们的搜索需求,因为数据每天都在增加,所以我们决定使用索引来获得更好的搜索性能。

Working FLOWR Query (Just Sample)工作流查询(只是示例)

for $doc in collection("col1")
where fn:contains($doc//entityName/text(), "USA")
return document-uri($doc)

above query is working and it returns a document URI, Now we are trying to use Optic API to satisfy the same requirement.上面的查询正在运行,它返回一个文档 URI,现在我们正在尝试使用光学 API 来满足相同的要求。

We have created an element range index for entityName but not sure how to convert the above FLOWR query into Optic Query.我们为entityName创建了一个元素范围索引,但不确定如何将上述 FLOWR 查询转换为 Optic Query。

What will be equivalent Optic Query for the above FLOWR query?上述 FLOWR 查询的等效光学查询是什么? , also in future we are planning to use fn:starts-with() and fn:ends-with() functions too. ,未来我们也计划使用 fn:starts-with() 和 fn:ends-with() 函数。

We are using MarkLogic 10.0-2.1我们正在使用 MarkLogic 10.0-2.1

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

After creating a TDE to project the entity properties, the equivalent Optic query would resemble the following in XQuery:在创建 TDE 以投影实体属性之后,等效的 Optic 查询将类似于 XQuery 中的以下内容:

op:from-view(null, VIEW_NAME, '', op:fragment-id-col('docId'))
=> op:where(ofn:contains(op:col('entityName', 'USA'))
=> op:where(cts:collection-query(COLLECTION_NAME))
=> op:join-doc-uri('uri', op:fragment-id-col('docId'))
=> op:select('uri')
=> op:result()

In XQuery, the ofn library must be imported.在 XQuery 中,必须导入ofn库。

In SJS, the op.fn field provides the equivalent functions:在 SJS 中, op.fn字段提供了等效的功能:

op.fromView(null, VIEW_NAME, '', op.fragmentIdCol('docId'))
  .where(op.fn.contains(op.col('entityName', 'USA'))
  .where(cts.collectionQuery(COLLECTION_NAME))
  .joinDocUri('uri', op.fragmentIdCol('docId'))
  .select('uri')
  .result()

The operations used:使用的操作:

  1. fromView() accesses the entity view fromView()访问实体视图
  2. The first where() filters on the value of the column during query execution第一个where()在查询执行期间过滤列的值
  3. The second where() constrains the entity rows to matching source documents第二个where()将实体行限制为匹配源文档
  4. The joinDocUri() joins the URI lexicon based on the source documents of the entity rows joinDocUri()根据实体行的源文档连接 URI 词典
  5. The select() projects the 'uri' column, ignoring the unneeded view columns. select()投影“uri”列,忽略不需要的视图列。

joinDocUri() is a convenience for joinDocUri()是一个方便

.joinInner(
    op.fromLexicons({'uri':cts.uriReference()}, '', op.fragmentIdCol('uriDocId')),
    op.on(op.fragmentIdCol('docId'), op.fragmentIdCol('uriDocId'))
    )

The Optic expression functions also include op.fn.startsWith() and op.fn.endsWith() .光学表达式函数还包括op.fn.startsWith()op.fn.endsWith() In general, Optic expressions can use a function if it both一般来说,光学表达式可以使用 function 如果它同时

  • is a builtin - in other words, doesn't require an import or require是内置的 - 换句话说,不需要导入或需要
  • only transforms its input to its output - in other words, is purely functional without side effects or environment sensitivity仅将其输入转换为其 output - 换句话说,纯粹是功能性的,没有副作用或环境敏感性

See also this list of expression functions:另请参阅此表达式函数列表:

https://docs.marklogic.com/guide/app-dev/OpticAPI#id_69308 https://docs.marklogic.com/guide/app-dev/OpticAPI#id_69308

Hoping that helps,希望有帮助,

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

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