Loading TOC...

op:where

op:where(
   $plan as map:map,
   $condition as item()
) as map:map

Summary

This method restricts the row set to rows matched by the boolean expression. Use boolean composers such as op:and and op:or to combine multiple expressions.

A constraining document query returns only the rows from the matched source documents. If the constraining document query is a node instead of a cts:query object, the implementation calls the cts:query parser on the node. The constraining document query applies to all upstream accessors. A constraining sem:store returns only the triples from the specified store (potentially expanded by inference using a ruleset). A constraining sem:store applies to all upstream triples accessors.

Parameters
$plan The Optic Plan. You can either use the XQuery => chaining operator or specify the variable that captures the return value from the previous operation.
$condition This can be a boolean expression, a cts:query to qualify the source documents that produced the rows set, or (where part of the row set was produced by the op:from-triples accessor) a sem:store to restrict or expand the triples that produce the row set.

Example

xquery version "1.0-ml";

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

let $employees := op:from-view("main", "employees")
let $expenses  := op:from-view("main", "expenses")
let $expenselimit  := op:from-view("main", "expenselimit")
return $employees
   => op:join-inner($expenses, op:on(
                op:view-col("employees", "EmployeeID"),
                op:view-col("expenses", "EmployeeID")))
   => op:join-inner($expenselimit, op:on(
                op:view-col("expenses", "Category"),
                op:view-col("expenselimit", "Category")))
   => op:where(op:gt(op:view-col("expenses", "Amount"),
                op:view-col("expenselimit", "Limit")))
   => op:select((op:view-col("employees", "EmployeeID"),
                "FirstName", "LastName",
                op:view-col("expenses", "Category"),
                op:view-col("expenses", "Amount"),
                op:view-col("expenselimit", "Limit")))
   => op:order-by(op:view-col("employees", "EmployeeID"))
   => op:result()
  

Example

(: Insert the template for view "docWhereExample" :)
xquery version "1.0-ml";
import module namespace tde = "http://marklogic.com/xdmp/tde"
        at "/MarkLogic/tde.xqy";

let $template :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>person</context>
  <collections>
    <collection>docWhereExample</collection>
  </collections>
  <rows>
    <row>
      <schema-name>docWhereExample</schema-name>
      <view-name>docWhereExample</view-name>
      <columns>
        <column>
          <name>id</name>
          <scalar-type>long</scalar-type>
          <val>id</val>
        </column>
        <column>
          <name>name</name>
          <scalar-type>string</scalar-type>
          <val>name</val>
        </column>
      </columns>
    </row>
  </rows>
</template>
return tde:template-insert("docWhereTemplate.xml", $template)

(: Insert documents :)
xquery version "1.0-ml";
let $docs := (<person><id>1</id><name>john</name><sentence>John is there</sentence></person>,
             <person><id>1</id><name>joe</name><sentence>Joe was there</sentence></person>,
             <person><id>2</id><name>alice</name><sentence>Alice was there</sentence></person>)
return
  for $doc at $i in $docs
  return xdmp:document-insert(concat("doc", $i, ".xml"),
                              $doc,
                              <options xmlns="xdmp:document-insert">
                                <collections>
                                  <collection>docWhereExample</collection>
                                </collections>
                              </options>)

(: Below query returns "joe" :)
xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

op:from-view("docWhereExample", "docWhereExample")
  => op:where(op:eq(op:col("id"), 1))
  => op:where(cts:word-query("was"))
  => op:select("name")
  => op:result()
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.