user1660340
user1660340

Reputation: 131

Marklogic cts:or query in Loop

I want to create a cts:or-query in a for loop. How can I do this?

An example of my logic:

let $query := for $tag in (1,2,3,4,5)
return myquery

I would like to get final queries such as:

let $query := cts:or-query(
    (
    cts:element-query(xs:QName("ts:tag"),'1'),
    cts:element-query(xs:QName("ts:tag"),'2'),
    cts:element-query(xs:QName("ts:tag"),'3'),
    cts:element-query(xs:QName("ts:tag"),'4'),
    cts:element-query(xs:QName("ts:tag"),'5')
    )
)

Upvotes: 2

Views: 823

Answers (2)

mblakele
mblakele

Reputation: 7840

For this particular example it would be better to write a shotgun-OR:

cts:element-value-query(xs:QName("ts:tag"), xs:string(1 to 5))

This will behave like an or-query, but will be a little more efficient. Note that I changed your cts:element-query to an element-value query. That may or may not be what you want, but each query term should be as precise as possible.

You can also use a FLWOR expression to generate queries. This is useful for and-query semantics, where the previous technique doesn't help.

let $query := cts:and-query(
  for $i in ('dog', 'cat', 'rat')
  return cts:word-query($i))
return cts:search(collection(), $query)[1 to 20]

Upvotes: 6

sinemetu1
sinemetu1

Reputation: 1726

This will work:

let $query := cts:or-query(
  for $val in ('1', '2', '3', '4', '5')
  return cts:element-query(xs:QName("ts:tag"), $val)
)

The FLWOR loop returns a sequence of cts:element-query's.

Upvotes: 0

Related Questions