After creating a TDE to project the entity properties, the equivalent Optic query would resemble the following in 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.
In SJS, the op.fn
field provides the equivalent functions:
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:
fromView()
accesses the entity view
- The first
where()
filters on the value of the column during query execution
- The second
where()
constrains the entity rows to matching source documents
- The
joinDocUri()
joins the URI lexicon based on the source documents of the entity rows
- The
select()
projects the 'uri' column, ignoring the unneeded view columns.
joinDocUri()
is a convenience for
.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()
. In general, Optic expressions can use a function if it both
- 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
See also this list of expression functions:
https://docs.marklogic.com/guide/app-dev/OpticAPI#id_69308
Hoping that helps,
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…