Search code examples
marklogicmarklogic-optic-api

Constraining values using wildcards with the MarkLogic Optic API


Using the MarkLogic 10 Optic API, Is it possible to do wildcarded constraining with op:where()?

In the below table I'm trying to return all rows that start with a "HELLO" value, ONLY in the Table.Message column.

Test.Table.Message
HELLO123
HELLO1234
HELLO12345
HELLO1123123123123
BYE1234
    
version "1.0-ml";
import module namespace op="http://marklogic.com/optic" at "/MarkLogic/optic.xqy";
        
op:from-view("Test", "Table")
  =>op:where(op:eq(op:view-col('Table', 'Message'), 'HELLO*'))
  =>op:result()

Adding an asterisk or % as a trailer to the op:where(op:eq(op:view-col('Table', 'Column'), 'HELLO*')) clause does not return results.

In the optic API guide, there is also an example of a cts:word-query() being used within an op:eq, but this does not work as well: op:where(op:eq(op:view-col('Table', 'Column'), cts:word-query('HELLO')))


Solution

  • One way to do it is with op:sql-condition() using the SQL LIKE operator

    import module namespace op="http://marklogic.com/optic" at "/MarkLogic/optic.xqy";
    
    (:op:from-view("Test", "Table"):)
    op:from-literals((
      map:entry("Message", "HELLO123"),
      map:entry("Message", "HELLO1234"),
      map:entry("Message", "HELLO12345"),
      map:entry("Message", "HELLO1123123123123"),
      map:entry("Message", "BYE1234")
    ), "Table")
    => op:where(op:sql-condition("Message LIKE 'HELLO%'"))
    => op:result()